java容器類只有兩個主要類型:Collection和Map。java
Collection容器每一個槽只有一個元素。安全
Map中持有鍵值對關聯。函數
Collection子接口有List和Set。性能
List:以特定次序存儲元素。之類有ArrayList和LinkedList線程
ArrayList:擅長隨機訪問。code
LinkedList:擅長插入、刪除和移動元素。接口
Vector:同步,安全,性能較低。其他基本和ArrayList同樣。同步
Set:不含重複元素。it
HashSet:使用散列函數。io
TreeSet:使用紅黑樹。數據有序排列
LinkedHashSet:使用鏈表結合散列函數。
Map子類有HashMap、HashTable和TreeMap
HashMap:容許一個null鍵和多個null值。
HashTable:不容許null鍵和null值。線程安全
容器類輸出方式:Iterator、ListIterator、Enumeration和foreach
Iterator:
List<String> all = new ArrayList<String>(); all.add("hello"); Iterator<String> iter = all.iterator(); while(iter.hasNext()){ System.out.print(iter.next()); }
foreach :
List<String> all = new ArrayList<String>(); all.add("hello"); for(String str:all) System.out.print(str);