map, list常用遍历, pattern匹配, 文件读取, ThreadLocal
每次用时,老忘~~就此mark,看你还忘,哈哈哈~~
哎,老了,越来越不中用了~~
---------------------------------------------------------------------------------------------
private static Map<String, String> projectIPInfo = new HashMap<String, String>(); //如果key相同,后加的会覆盖前加的,put(), get(), contains()
private static Map<String, String> projectIPMaskedInfo = new HashMap<String, String>();
private static Map<String, Pattern> projectIPMaskedPatternInfo = new HashMap<String, Pattern>();
private static Map<String, List<String>> projectIPBlackInfo = new HashMap<String, List<String>>();
for(Iterator it = projectIPBlackInfo.get("project1").iterator(); it.hasNext(); ){
System.out.println(it.next());
}
for(Iterator it = projectIPMaskedInfo.entrySet().iterator(); it.hasNext(); ){
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
---------------------------------------------------------------------------------------------
for (Iterator it = projectIPMaskedInfo.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
Pattern p = projectIPMaskedPatternInfo.get(entry.getKey());
// 如果IP匹配上了这个正则表达式
if (p.matcher(ip).matches()) {
String tem = "-" + ip;
// 如果匹配上了正则式,但该IP却位于黑名单
if (checkBlackList(entry.getValue().toString(), tem)) {
continue;
} else {// 如果匹配上的正则式,且该IP不在黑名单中
projName2 = entry.getValue().toString();
break;
}
}
}
---------------------------------------------------------------------------------------------
InputStream in = null;
File file = new File("C:/hello7.txt");
try {
in = new FileInputStream(file);
}catch(IOException e){
System.out.println("read file exception......................");
}
List<List<String>> outputs = new ArrayList<List<String>>();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line = null;
line = br.readLine();
while((line)!=null && !line.equals("-----")){
line = br.readLine();
}
----------------------------------------------------------------------------------------------
threadLocal使用方法:引自(http://lavasoft.blog.51cto.com/62575/51926/)
实例来自:(http://blog.csdn.net/qjyong/article/details/2158097)
import java.util.Collections; import java.util.HashMap; import java.util.Map; public class SimpleThreadLocal { private Map valueMap = Collections.synchronizedMap(new HashMap()); public void set(Object newValue) { valueMap.put(Thread.currentThread(), newValue);// ①键为线程对象,值为本线程的变量副本 } public Object get() { Thread currentThread = Thread.currentThread(); Object o = valueMap.get(currentThread);// ②返回本线程对应的变量 if (o == null && !valueMap.containsKey(currentThread)) {// ③如果在Map中不存在,放到Map中保存起来。 o = initialValue(); valueMap.put(currentThread, o); } return o; } public void remove() { valueMap.remove(Thread.currentThread()); } public Object initialValue() { return null; } }
public class SequenceNumber { // ①通过匿名内部类覆盖ThreadLocal的initialValue()方法,指定初始值 private static ThreadLocal<Integer> seqNum = new ThreadLocal<Integer>() { public Integer initialValue() { return 0; } }; // ②获取下一个序列值 public int getNextNum() { seqNum.set(seqNum.get() + 1);//seqNum Map: <thread1, value1> return seqNum.get(); } public static void main(String[] args) { SequenceNumber sn = new SequenceNumber();//一个SequenceNumber对象 // ③ 3个线程共享sn,各自产生序列号 TestClient t1 = new TestClient(sn); //放到不同的线程中,线程间共享这个对象,但一个线程却维护着一个单独的副本 TestClient t2 = new TestClient(sn); TestClient t3 = new TestClient(sn); t1.start(); t2.start(); t3.start(); } private static class TestClient extends Thread { private SequenceNumber sn; public TestClient(SequenceNumber sn) { this.sn = sn; } public void run() { for (int i = 0; i < 3; i++) { // ④每个线程打出3个序列值 System.out.println("thread[" + Thread.currentThread().getName() + "] sn[" + sn.getNextNum() + "]"); } } } }
日期的获取:
// 今天周五,查询本周一一天的量
Date now = new Date();
Date start = DateUtil.addDays(now, MONDAY);//周一
start = DateUtil.setHoursToEmpty(start);// 周一0点
Date end = DateUtil.addDays(now, MONDAY+1);//周二
end = DateUtil.setHoursToEmpty(end);//周二0点
end = DateUtil.addSeconds(end, -1);// 周二23点59分59秒
//查询上周周一的量
Date lastWeekNow = DateUtil.addDays(now, LASTMONDAY);
Date lastWeekStart = DateUtil.addDays(lastWeekNow, 0);//上周一
lastWeekStart = DateUtil.setHoursToEmpty(lastWeekStart);// 上周一0点
Date lastWeekEnd = DateUtil.addDays(now, LASTMONDAY+1) ;//上周二
lastWeekEnd = DateUtil.setHoursToEmpty(lastWeekEnd);// 上周二0点
lastWeekEnd = DateUtil.addSeconds(lastWeekEnd, -1);// 上周二23点59分59秒
new BigDecimal(mvUsedMem.avgValue).setScale(2,BigDecimal.ROUND_HALF_UP).toEngineeringString()