Map 遍历:
Map<Integer , String> hs = new HashMap<Integer , String>();
....
//遍历方式一:for each遍历HashMap的entryset,注意这种方式在定义的时候就必须写成
//Map<Integer , String> hs,不能写成Map hs;
for(Entry<Integer , String> entry : hs.entrySet()) {
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
}
//遍历方式二:使用EntrySet的Iterator
Iterator<Map.Entry<Integer , String>> iterator = hs.entrySet().iterator();
while(iterator.hasNext()) {
Entry<Integer , String> entry = iterator.next();
System.out.println("key:"+entry.getKey()+" value:"+entry.getValue());
}
//遍历方式三:for each直接使用HashMap的keyset
for(Integer key : hs.keySet()) {
System.out.println("key:"+key+" value:"+hs.get(key));
}
//遍历方式四:使用keyset的Iterator
Iterator keyIterator = hs.keySet().iterator();
while(keyIterator.hasNext()) {
Integer key = (Integer)keyIterator.next();
System.out.println("key:"+key+" value:"+hs.get(key));
}
利用set的不可重复性,只需三步走:
//第一步:用HashSet的特性去重
HashSet tempSet = new HashSet(arrayList);
//第二步:将arrayList清除
arrayList.clear();
//第三步:将去重后的重新赋给List
arrayList.addAll(tempSet);
//A example of 去重
https://blog.csdn.net/ya1la2/article/details/48106601