HashMap是无序的,TreeMap是有序的
HashMap添加和删除映射关系效率更高代码片
.
public class Demo3 {
public static void main(String[] args) {
Map<String, String> map=new HashMap<String, String>();
for (int i =10 ; i < 15; i++) {
map.put(i + "", i + "");
}
Set<String> set =map.keySet();
Iterator<String> it=set.iterator();
//关于Map的for 循环
for (String key : map.keySet()) {
System.out.println(key);
}
/*while(it.hasNext()){
String str=it.next();
System.out.println(str);
//System.out.println(map.get(str));
}*/
/*map.remove("01");
it=map.keySet().iterator();
while(it.hasNext()){
System.out.println(it.next());
}*/
System.out.println("------");
TreeMap<String, String> tree = new TreeMap<String, String>();
tree.putAll(map);
it=tree.keySet().iterator();
while(it.hasNext()){
String str=it.next();
System.out.println(str);
//System.out.println(map.get(str));
}
}
}