hashmap,hashtable,treemap总结的一个例子

import java.util.ArrayList;
import java.util.Collections;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Hashtable;
import java.util.TreeMap;

public class Test {
public static void main(String[] args) {
//hashmap,它是无序的。
Map map = new HashMap();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
map.put("d", "ddd");

Iterator iterator = map.keySet().iterator();
while (iterator.hasNext()) {
Object key = iterator.next();
System.out.println("map.get(key) is :" + map.get(key));
}
//hashtale,它也是无序的
Hashtable tab = new Hashtable();
tab.put("a", "aaa");
tab.put("b", "bbb");
tab.put("c", "ccc");
tab.put("d", "ddd");
Iterator iterator_1 = tab.keySet().iterator();
while (iterator_1.hasNext()) {
Object key = iterator_1.next();
System.out.println("tab.get(key) is :" + tab.get(key));
}
//treemap,它是有序的
TreeMap tmp = new TreeMap();
tmp.put("a", "aaa");
tmp.put("b", "bbb");
tmp.put("c", "ccc");
tmp.put("d", "ddd");
Iterator iterator_2 = tmp.keySet().iterator();
while (iterator_2.hasNext()) {
Object key = iterator_2.next();
System.out.println("tmp.get(key) is :" + tmp.get(key));
}
//arrayList,它是有序的
   ArrayList arrayList=new ArrayList();
   arrayList.add("aaa");
   arrayList.add("bbb");
   arrayList.add("ccc");
   arrayList.add("ddd");
   Collections.sort(arrayList);
   for(int i=0;i<arrayList.size();i++){
   System.out.println(arrayList.get(i));
   }

}

}

你可能感兴趣的:(java,C++,c,C#)