使用Java SE5以前存在的一個問題是: 編譯器容許你向容器中插入不正確的類型. 考慮下例:java
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; import java.util.*; class Apple { private static long counter; private final long id = counter++; public long id() { return id; } } class Orange {} public class ApplesAndOrangesWithoutGenerics { @SuppressWarnings("unchecked") public static void main(String[] args) { ArrayList apples = new ArrayList(); for (int i = 0; i < 3; i++) { apples.add(new Apple()); } apples.add(new Orange()); for (int i = 0; i < apples.size(); i++) { System.out.println(((Apple)apples.get(i)).id()); } } }
1. 因爲ArrayList保存的是Object類型, 因此能夠存儲Apple, Orange類型.數組
2. 在具體使用時候執行強制類型轉換, 因爲Orange類型非Apple類型, 因此強制類型轉換失敗, 致使運行時候拋出異常.安全
解決方式是使用泛型類型:app
import jdk.nashorn.internal.runtime.arrays.ArrayLikeIterator; import java.util.*; class Apple { private static long counter; private final long id = counter++; public long id() { return id; } } class Orange {} public class ApplesAndOrangesWithoutGenerics { @SuppressWarnings("unchecked") public static void main(String[] args) { ArrayList<Apple> apples = new ArrayList(); for (int i = 0; i < 3; i++) { apples.add(new Apple()); } // 在編譯階段就直接報錯 // apples.add(new Orange()); for(Apple c: apples) { System.out.println(c.id()); } } }
Collection: 一個獨立元素的序列, 這些元素都服從一條或多條規則.函數
List: 順序存儲; Set: 不能重複元素; Queue: 排隊規則來肯定對象產生的順序.spa
ArrayList: 使用數組生成的列表; LinkedList: 使用鏈表生成的列表.code
HashSet: 使用哈希生成的Set集合; TreeSet: 使用紅黑樹生成的Set集合, 按照比較的順序升序存儲; LinkedHashSet: 按照添加的順序存儲.對象
PriorityQueue: 優先級隊列.索引
Map: 關聯數組, 鍵值對. 隊列
HashMap: 哈希關聯數組; TreeMap: 樹形關聯數組, 使用紅黑樹生成, 按照比較的順序升序存儲. LinkedHashMap: 按照添加的順序存儲.
Iterator: 迭代器, 標準有next(), hasNext(), remove()三個基本函數.
Arrays.asList: 接受一個數組或是一個用逗號分割的元素列表, 將其轉換爲一個List對象.
Collection.addAll: 接受一個數組或者Collection對象.
import java.util.*; public class Test { public static void main(String[] args) { Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); Integer[] moreInts = {6, 7, 8, 9}; collection.addAll(Arrays.asList(moreInts)); for (Integer i: collection) { System.out.print(i + ", "); } System.out.println(); } }
size(): 返回其長度
isEmpty(): 判斷是否爲空
contains(o): 是否包含元素o
toArray: 返回一個Array數組.
add(e)/add(index, e): 增長元素, 或在索引index處增長元素.
remove(index): 在索引index處刪除元素.
addAll(Collection): 增長一個Collection.
removeAll(Collection): 刪除基於equals()方法的全部Collection中的元素.
hashCode(): 返回其哈希值.
get(index): 獲取索引index的值.
indexOf(Object o): 返回其對象o的索引值.
迭代器有三個基本的方法:
1. next()獲取序列中的下一個元素.
2. 使用hasNext()檢查序列中是否還有元素.
3. 使用remove()將迭代器新近返回的元素刪除(即調用remove以前必須調用next)
import java.util.*; public class Test { public static void print(Object o) { System.out.print(o + ", "); } public static void main(String[] args) { List<Integer> l = new ArrayList<>(Arrays.asList(1, 2, 3)); Iterator<Integer> iter = l.iterator(); while (iter.hasNext()) { print(iter.next()); } } }
Iterator只能向前移動, 使用ListIterator則能夠雙向移動.
import java.util.*; public class Test { public static void print(Object o) { System.out.print(o + ", "); } public static void main(String[] args) { List<Integer> l = new ArrayList<>(Arrays.asList(1, 2, 3)); Iterator<Integer> iter = l.iterator(); while (iter.hasNext()) { print(iter.next()); } ListIterator<Integer> list = l.listIterator(); while (list.hasNext()) { print(list.next()); } while (list.hasPrevious()) { print(list.previous()); } } }
因爲LinkedList是鏈表實現的列表, 因此它能夠對列表的頭尾進行操做.
例如addFirst()/addLast(), removeFirst()/removeLast(), getFirst()/getLast().
Set
集合主要用於存儲不重複的元素, 耶能夠使用contains來判斷是否存在於集合中, 或者containAll來判斷集合A是否存在於集合B中.
HashSet使用散列來生成集合, 而TreeSet使用紅黑樹來生成有序集合.
import java.util.*; public class Test { public static void main(String[] args) { Set<Integer> s1 = new HashSet<>(Arrays.asList(111, 13, 2)); Set<Integer> s2 = new TreeSet<>(Arrays.asList(1, 3, 2)); System.out.println(s1); System.out.println(s2); } }
Map
import java.util.*; public class Test { public static void main(String[] args) { Map<Integer, Integer> m = new HashMap<>(); m.put(1, 2); m.put(5, 3); m.put(3, 0); m.put(7, 9); for (Integer i: m.keySet()) { System.out.println(i + ":" + m.get(i)); } System.out.println(m.values()); for (Map.Entry entry: m.entrySet()) { System.out.println(entry.getKey() + ":" + entry.getValue()); } } }