1. HashMap遍历的两种方式
From Evernote: |
1. HashMap遍历的两种方式Clipped from: http://www.cnblogs.com/KiloNet/articles/1873731.html |
1. HashMap遍历的两种方式
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
2. byte[] 转换为InputStream:
第一种:
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
效率高,以后一定要使用此种方式!
第二种:
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
效率低,以后尽量少使用!
2. byte[] 转换为InputStream:
InputStream in = new StringBufferInputStream(new String(bytes));
InputStream in = new ByteArrayInputStream(bytes);
InputStream in = new ByteArrayInputStream(bytes);
3. 文件读成流:
File dataFile = new File("data.xml");
System.out.println("filepath:" + dataFile.getAbsolutePath());
InputStream in = new FileInputStream(dataFile);
System.out.println("filepath:" + dataFile.getAbsolutePath());
InputStream in = new FileInputStream(dataFile);
4. 锁的应用
锁定: synchronized (iMsgListener) {
iMsgListener.wait(20000);
}
iMsgListener.wait(20000);
}
解锁:
synchronized (iMsgListener) {
iMsgListener.notify();
}
iMsgListener.notify();
}