20130222!
----------------------------------------------------------map 统计字符/单词个数-----------------------------------------------------
/**
* map char count
*
* @param str
* @return
*/
public Map<Character, Integer> getCharCnt(String str) {
char[] chrs = str.toCharArray();
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char ch : chrs) {
if (map.containsKey(ch)) {
int count = map.get(ch);
map.put(ch, ++count);
} else {
map.put(ch, 1);
}
}
return map;
}
/**
* map word cnt
*
* @param strs
* @return
*/
public Map<String, Integer> getWordCnt(String[] strs) {
Map<String, Integer> map = new HashMap<String, Integer>();
for (String str : strs) {
if (map.containsKey(str)) {
int count = map.get(str);
map.put(str, ++count);
} else {
map.put(str, 1);
}
}
return map;
}
/**
* map keySet
*
* @param map
*/
public void printMapkeySet(Map<?, ?> map) {
for (Object o : map.keySet()) {
System.out.println("key: " + o + " " + "value: " + map.get(o));
}
}
/**
* map entrySet
*
* @param map
*/
public void printMapEntrySet(Map<Character, Integer> map) {
Set<Entry<Character, Integer>> entrySet = map.entrySet();
for (Entry<Character, Integer> entry : entrySet) {
System.out.println("key: " + entry.getKey() + " " + "value: "
+ entry.getValue());
}
}
public void print(Object o) {
System.out.println(o);
}
private static StringUtil instance = new StringUtil();
private StringUtil() {
}
public static StringUtil getInstance() {
return instance;
}
public void isvalidStr(String str) {
}
/**
* about word.....replace
*
* @param str
* @return
*/
public String getSplitWord(String str) {
if (str == null) {
throw new NullPointerException();
}
if (str.isEmpty()) {
return "";
}
if (!str.matches("^[a-zA-Z,。 ]+$")) {
throw new IllegalArgumentException();
}
StringBuffer msg = new StringBuffer("");
String[] strs = str.replaceAll(",", " ").replaceAll("。", " ")
.split("\\W");
for (String s : strs) {
msg = msg.append(s);
}
return msg.toString();
}
public String[] getSplitWordArray(String str) {
if (str == null) {
throw new NullPointerException();
}
if (str.isEmpty()) {
return new String[] { "" };
}
if (!str.matches("^[a-zA-Z,。 ]{1,}")) {
throw new IllegalArgumentException();
}
return str.replaceAll(",", " ").replaceAll("。", " ").split("\\W");
}
-------------------------------------------string,stringBuffer,StringBuilder---------------------------------------
/**
* string,stringBuffer,stringBuilder性能比较
* 数据较小时用StringBuffer
* 数据庞大时用StringBuilder
*
* @return
*/
public long getTimeString() {
String str = "";
long start = System.currentTimeMillis();
for (int i = 0; i < Const.COUNT_COMPARE_STR; i++) {
str += String.valueOf(i);
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* StringBuffer
*
* @return
*/
public long getStringBuffer() {
StringBuffer sb = new StringBuffer("");
long start = System.currentTimeMillis();
for (int i = 0; i < Const.COUNT_COMPARE_STR; i++) {
sb.append(String.valueOf(i));
}
long end = System.currentTimeMillis();
return end - start;
}
/**
* StringBuilder
*
* @return
*/
public long getStringBuilder() {
long start = System.currentTimeMillis();
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < Const.COUNT_COMPARE_STR; i++) {
sb.append(String.valueOf(i));
}
long end = System.currentTimeMillis();
return end - start;
}
--------------------------------------------------------------Timer 定时器-----------------------
/**
* 定时器每隔1s执行一次,任务之间的时间间隔是1s
*
* 6s之后结束定时器
*
*/
public void timer() {
final Timer time = new Timer();
time.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("boom!");
}
}, 1000,1000);
time.schedule(new TimerTask() {
@Override
public void run() {
time.cancel();
}
}, 6000);
}
/**
* 定时器---倒计时
*/
public void timeCnt(){
final Timer time = new Timer();
final int min = 1;
final long start = System.currentTimeMillis();
final long end = start+min*60*1000;
time.schedule(new TimerTask() {
@Override
public void run() {
long show = end - System.currentTimeMillis();
long h = show/1000/60/60;
long m = show/1000/60%60;
long s = show/1000%60;
System.out.println(h+":"+m+":"+s);
}
}, 0,1000);
time.schedule(new TimerTask() {
@Override
public void run() {
time.cancel();
}
}, new Date(end));
}
--------------------------------------------奇数:取余,位运算------------------------------------
public boolean isOdd(int i) {
return i % 2 != 0;
}
public boolean isOddBit(int i) {
return (i & 1) != 0;
}
------------------------------------------number or letter-------------------------------------
/**
* is number
*
* @param ch
* @return
*/
public boolean isNum(char ch) {
return "0123456789".indexOf(ch) >= 0;
}
/**
* is letter
*
* @param ch
* @return
*/
public boolean isLetter(char ch) {
return "abcdefjhijklmnopqrstuvwsyz".indexOf(ch) >= 0;
}
----------------------------------------DBUtils------------------------------------------------
package com.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;
public class DBUtil {
private Connection conn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
private static final String DB_FILE_PATH = "demo_db";
private static String drive;
private static String url;
private static String username;
private static String password;
static {
ResourceBundle resb = ResourceBundle.getBundle(DB_FILE_PATH);
drive = resb.getString("DRIVERS").trim();
url = resb.getString("URL").trim();
username = resb.getString("USER").trim();
password = resb.getString("PASSWORD").trim();
}
private static DBUtil instance = new DBUtil();
private DBUtil() {
}
public static DBUtil getInstance() {
return instance;
}
public Connection getConnection() {
try {
Class.forName(drive);
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public void insert(String sql) {
try {
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setString(2, "");
ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(ps);
this.close(conn);
}
}
public void update(String sql) {
try {
conn = this.getConnection();
ps = conn.prepareStatement(sql);
ps.setString(1, "");
rs = ps.executeQuery();
while (rs.next()) {
rs.getString("");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
this.close(rs);
this.close(ps);
this.close(conn);
}
}
public void close(Connection conn) {
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(Statement ps) {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void close(ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}