System.out.println("## Hashtable ##");
Hashtable<String, String> ht = new Hashtable<String, String>();
ht.put("a", "OOO");
ht.put("c", "OOO");
ht.put("b", "OOO");
ht.put("e", "OOO");
ht.put("d", "OOO");
Iterator<String> it = ht.keySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
// ///////////////////
// TEST
// ///////////////////////
System.out.println("## TreeMap ##");
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("a", "OOO");
tm.put("c", "OOO");
tm.put("b", "OOO");
tm.put("e", "OOO");
tm.put("d", "OOO");
Iterator<String> it2 = tm.keySet().iterator();
while (it2.hasNext()) {
System.out.println(it2.next());
}
// ///////////////////
// TEST
// ///////////////////////
System.out.println("## HashMap ##");
Map<String, String> hm = new HashMap<String, String>();
hm.put("a", "OOO");
hm.put("c", "OOO");
hm.put("b", "OOO");
hm.put("e", "OOO");
hm.put("d", "OOO");
Iterator<String> it3 = hm.keySet().iterator();
while (it3.hasNext()) {
System.out.println(it3.next());
}
// ///////////////////
// TEST
// ///////////////////////
System.out.println("## LinkedHashMap ##");
LinkedHashMap<String, String> lhm = new LinkedHashMap<String, String>();
lhm.put("a", "OOO");
lhm.put("c", "OOO");
lhm.put("b", "OOO");
lhm.put("e", "OOO");
lhm.put("d", "OOO");
Iterator<String> it4 = lhm.keySet().iterator();
while (it4.hasNext()) {
System.out.println(it4.next());
}
结果如下 treeMap hashmap是升序的 hashtable乱序 LinkedHashMap 放入顺序
不知道这个结果和JDK版本有关没有~ 我的是1.7.0 64位版本
## Hashtable ##
b
a
e
d
c
## TreeMap ##
a
b
c
d
e
## HashMap ##
a
b
c
d
e
## LinkedHashMap ##
a
c
b
e
d