import java.util.*; class StringAddress { private String s; public StringAddress(String s) { this.s = s; } public String toString() { return super.toString() + " " + s; } } public class FillingLists { public static void main(String[] args) { List<StringAddress> list= new ArrayList<StringAddress>( Collections.nCopies(4, new StringAddress("Hello")));//建立並填充 System.out.println(list); Collections.fill(list, new StringAddress("World!"));//只能用於替換已有的元素 System.out.println(list); } } /* Output: (Sample) [StringAddress@82ba41 Hello, StringAddress@82ba41 Hello, StringAddress@82ba41 Hello, StringAddress@82ba41 Hello] [StringAddress@923e30 World!, StringAddress@923e30 World!, StringAddress@923e30 World!, StringAddress@923e30 World!] *///:~
全部的Collection子類型都有一個接受另外一個Collection對象的構造器,用所接受的Collection對象種的元素來填充新的容器。java
import java.util.*; import net.mindview.util.*; class Government implements Generator<String> { String[] foundation = ("strange women lying in ponds " + "distributing swords is no basis for a system of " + "government").split(" "); private int index; public String next() { return foundation[index++]; } } public class CollectionDataTest { public static void main(String[] args) { Set<String> set = new LinkedHashSet<String>( new CollectionData<String>(new Government(), 15)); System.out.println(set); // Using the convenience method: List<String> list=new ArrayList<>(); list.addAll(CollectionData.list(new Government(), 15)); System.out.println(list); } } /* Output: [strange, women, lying, in, ponds, distributing, swords, is, no, basis, for, a, system, of, government] *///:~
Map適配器如今均可以使用各類不一樣的Generator、Iterator和常量值的組合來填充Map初始化對象:算法
package net.mindview.util; import java.util.Iterator; import java.util.LinkedHashMap; public class MapData<K, V> extends LinkedHashMap<K, V> { public MapData(Generator<Pair<K, V>> gen, int quantity) { for(int i = 0; i < quantity; ++i) { Pair<K, V> p = (Pair)gen.next(); this.put(p.key, p.value); } } public MapData(Generator<K> genK, Generator<V> genV, int quantity) { for(int i = 0; i < quantity; ++i) { this.put(genK.next(), genV.next()); } } public MapData(Generator<K> genK, V value, int quantity) { for(int i = 0; i < quantity; ++i) { this.put(genK.next(), value); } } public MapData(Iterable<K> genK, Generator<V> genV) { Iterator var4 = genK.iterator(); while(var4.hasNext()) { K key = (Object)var4.next(); this.put(key, genV.next()); } } public MapData(Iterable<K> genK, V value) { Iterator var4 = genK.iterator(); while(var4.hasNext()) { K key = (Object)var4.next(); this.put(key, value); } } public static <K, V> MapData<K, V> map(Generator<Pair<K, V>> gen, int quantity) { return new MapData(gen, quantity); } public static <K, V> MapData<K, V> map(Generator<K> genK, Generator<V> genV, int quantity) { return new MapData(genK, genV, quantity); } public static <K, V> MapData<K, V> map(Generator<K> genK, V value, int quantity) { return new MapData(genK, value, quantity); } public static <K, V> MapData<K, V> map(Iterable<K> genK, Generator<V> genV) { return new MapData(genK, genV); } public static <K, V> MapData<K, V> map(Iterable<K> genK, V value) { return new MapData(genK, value); } }
下面是一個使用MapData的示例。LettersGenerator經過產生一個Iterator還實現了Iterable,經過這種方式,它能夠被用來測試MapData.map()方式,而這些方法都須要用到Iterable:編程
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; class Letters implements Generator<Pair<Integer,String>>, Iterable<Integer> { private int size = 9; private int number = 1; private char letter = 'A'; public Pair<Integer,String> next() { return new Pair<Integer,String>( number++, "" + letter++); } public Iterator<Integer> iterator() { return new Iterator<Integer>() { public Integer next() { return number++; } public boolean hasNext() { return number < size; } public void remove() { throw new UnsupportedOperationException(); } }; } } public class MapDataTest { public static void main(String[] args) { // Pair Generator: print(MapData.map(new Letters(), 11)); // Two separate generators: print(MapData.map(new CountingGenerator.Character(), new RandomGenerator.String(3), 8)); // A key Generator and a single value: print(MapData.map(new CountingGenerator.Character(), "Value", 6)); // An Iterable and a value Generator: print(MapData.map(new Letters(), new RandomGenerator.String(3))); // An Iterable and a single value: print(MapData.map(new Letters(), "Pop")); } } /* Output: {1=A, 2=B, 3=C, 4=D, 5=E, 6=F, 7=G, 8=H, 9=I, 10=J, 11=K} {a=YNz, b=brn, c=yGc, d=FOW, e=ZnT, f=cQr, g=Gse, h=GZM} {a=Value, b=Value, c=Value, d=Value, e=Value, f=Value} {1=mJM, 2=RoE, 3=suE, 4=cUO, 5=neO, 6=EdL, 7=smw, 8=HLG} {1=Pop, 2=Pop, 3=Pop, 4=Pop, 5=Pop, 6=Pop, 7=Pop, 8=Pop} *///:~
下面展現了Collection全部這些方法:api
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class CollectionMethods { public static void main(String[] args) { Collection<String> c = new ArrayList<String>(); c.addAll(Countries.names(6)); c.add("ten"); c.add("eleven"); print(c); // Make an array from the List: Object[] array = c.toArray(); // Make a String array from the List: String[] str = c.toArray(new String[0]);//將list轉換爲須要的數組 System.out.println("str="+Arrays.deepToString(str)); // Find max and min elements; this means // different things depending on the way // the Comparable interface is implemented: print("Collections.max(c) = " + Collections.max(c)); print("Collections.min(c) = " + Collections.min(c)); // Add a Collection to another Collection Collection<String> c2 = new ArrayList<String>(); c2.addAll(Countries.names(6)); c.addAll(c2); print(c); c.remove(Countries.DATA[0][0]); print(c); c.remove(Countries.DATA[1][0]); print(c); // Remove all components that are // in the argument collection: c.removeAll(c2); print(c); c.addAll(c2); print(c); // Is an element in this Collection? String val = Countries.DATA[3][0]; print("c.contains(" + val + ") = " + c.contains(val)); // Is a Collection in this Collection? print("c.containsAll(c2) = " + c.containsAll(c2)); Collection<String> c3 = ((List<String>)c).subList(3, 5); // Keep all the elements that are in both // c2 and c3 (an intersection of sets): c2.retainAll(c3); print(c2); // Throw away all the elements // in c2 that also appear in c3: c2.removeAll(c3); print("c2.isEmpty() = " + c2.isEmpty()); c = new ArrayList<String>(); c.addAll(Countries.names(6)); print(c); c.clear(); // Remove all elements print("after c.clear():" + c); } }
執行各類不一樣的添加和移除的方法在Collection接口中都是可選操做。這意味着實現類並不須要爲這些方法提供功能定義。
接口是面向對象設計中的契約,它聲明"不管你選擇如何實現該接口,我保證你能夠向該接口發送這些消息"。可是可選操做違反這個很是基本的原則,它聲明調用某些方法將不會執行有意義的行爲。
"未獲支持的操做"這種方式能夠實現Java容器類庫一個重要目標:容器應該易學易用。未獲支持的操做是一種特例,能夠延遲到須要時在實現。
爲了讓你種方式可以工做:數組
當你用Arrays.aslist()將數組轉換未List時,就會獲得這樣的容器。你能夠經過使用Collections類中"不可修改"的方法,選擇建立任何會拋出 UnsupportedOperationException的容器。安全
import java.util.*; public class Unsupported { static void test(String msg, List<String> list) { System.out.println("--- " + msg + " ---"); Collection<String> c = list; Collection<String> subList = list.subList(1,8); // Copy of the sublist: Collection<String> c2 = new ArrayList<String>(subList); try { c.retainAll(c2); } catch(Exception e) { System.out.println("retainAll(): " + e); } try { c.removeAll(c2); } catch(Exception e) { System.out.println("removeAll(): " + e); } try { c.clear(); } catch(Exception e) { System.out.println("clear(): " + e); } try { c.add("X"); } catch(Exception e) { System.out.println("add(): " + e); } try { c.addAll(c2); } catch(Exception e) { System.out.println("addAll(): " + e); } try { c.remove("C"); } catch(Exception e) { System.out.println("remove(): " + e); } // The List.set() method modifies the value but // doesn't change the size of the data structure: try { list.set(0, "X"); } catch(Exception e) { System.out.println("List.set(): " + e); } } public static void main(String[] args) { List<String> list = Arrays.asList("A B C D E F G H I J K L".split(" "));//返回固定尺寸的List test("Modifiable Copy", new ArrayList<String>(list)); test("Arrays.asList()", list);// test("unmodifiableList()", Collections.unmodifiableList( new ArrayList<String>(list)));//產生不可修改的列表,任何狀況下都不可修改 } } /* Output: --- Modifiable Copy --- --- Arrays.asList() --- retainAll(): java.lang.UnsupportedOperationException removeAll(): java.lang.UnsupportedOperationException clear(): java.lang.UnsupportedOperationException add(): java.lang.UnsupportedOperationException addAll(): java.lang.UnsupportedOperationException remove(): java.lang.UnsupportedOperationException --- unmodifiableList() --- retainAll(): java.lang.UnsupportedOperationException removeAll(): java.lang.UnsupportedOperationException clear(): java.lang.UnsupportedOperationException add(): java.lang.UnsupportedOperationException addAll(): java.lang.UnsupportedOperationException remove(): java.lang.UnsupportedOperationException List.set(): java.lang.UnsupportedOperationException *///:~
Arrays.asList()會生成一個List,它基於一個固定大小的數組,僅支持那些不會改變數組大小的操做。任何會引發對底層數據結構尺寸進行修改的方法都會產生 UnsupportedOperationException異常,以表示對未獲取支持操做的調用。數據結構
基本的LIst很容易使用:add()添加對象,get()一次取出一個元素,iterator()獲取用於該序列的Iterator。
下面的例子每一個方法都涵蓋了不一樣動做:併發
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class Lists { private static boolean b; private static String s; private static int i; private static Iterator<String> it; private static ListIterator<String> lit; public static void basicTest(List<String> a) {//包含每一個List均可執行的操做 a.add(1, "x"); // Add at location 1 a.add("x"); // Add at end // Add a collection: a.addAll(Countries.names(25)); // Add a collection starting at location 3: a.addAll(3, Countries.names(25)); b = a.contains("1"); // Is it in there? // Is the entire collection in there? b = a.containsAll(Countries.names(25)); // Lists allow random access, which is cheap // for ArrayList, expensive for LinkedList: s = a.get(1); // Get (typed) object at location 1 i = a.indexOf("1"); // Tell index of object b = a.isEmpty(); // Any elements inside? it = a.iterator(); // Ordinary Iterator lit = a.listIterator(); // ListIterator lit = a.listIterator(3); // Start at loc 3 i = a.lastIndexOf("1"); // Last match a.remove(1); // Remove location 1 a.remove("3"); // Remove this object a.set(1, "y"); // Set location 1 to "y" // Keep everything that's in the argument // (the intersection of the two sets): a.retainAll(Countries.names(25)); // Remove everything that's in the argument: a.removeAll(Countries.names(25)); i = a.size(); // How big is it? a.clear(); // Remove all elements } public static void iterMotion(List<String> a) {//使用Iterator遍歷元素 ListIterator<String> it = a.listIterator(); b = it.hasNext(); b = it.hasPrevious(); s = it.next(); i = it.nextIndex(); s = it.previous(); i = it.previousIndex(); } public static void iterManipulation(List<String> a) {//使用Iterator修改元素 ListIterator<String> it = a.listIterator(); it.add("47"); // Must move to an element after add(): it.next(); // Remove the element after the newly produced one: it.remove(); // Must move to an element after remove(): it.next(); // Change the element after the deleted one: it.set("47"); } public static void testVisual(List<String> a) {//查看List的操做效果 print(a); List<String> b = Countries.names(25); print("b = " + b); a.addAll(b); a.addAll(b); print(a); // Insert, remove, and replace elements // using a ListIterator: ListIterator<String> x = a.listIterator(a.size()/2); x.add("one"); print(a); print(x.next()); x.remove(); print(x.next()); x.set("47"); print(a); // Traverse the list backwards: x = a.listIterator(a.size()); while(x.hasPrevious()) printnb(x.previous() + " "); print(); print("testVisual finished"); } // There are some things that only LinkedLists can do: public static void testLinkedList() {//LinkedList專用的操做 LinkedList<String> ll = new LinkedList<String>(); ll.addAll(Countries.names(25)); print(ll); // Treat it like a stack, pushing: ll.addFirst("one"); ll.addFirst("two"); print(ll); // Like "peeking" at the top of a stack: print(ll.getFirst()); // Like popping a stack: print(ll.removeFirst()); print(ll.removeFirst()); // Treat it like a queue, pulling elements // off the tail end: print(ll.removeLast()); print(ll); } public static void main(String[] args) { // Make and fill a new list each time: basicTest( new LinkedList<String>(Countries.names(25))); basicTest( new ArrayList<String>(Countries.names(25))); iterMotion( new LinkedList<String>(Countries.names(25))); iterMotion( new ArrayList<String>(Countries.names(25))); iterManipulation( new LinkedList<String>(Countries.names(25))); iterManipulation( new ArrayList<String>(Countries.names(25))); testVisual( new LinkedList<String>(Countries.names(25))); testLinkedList(); } } /* (Execute to see output) *///:~
類型 | 描述 |
---|---|
Set(interface) | 存入Set的每一個元素都必須是惟一的,由於Set不保存重複元素。加入Set的元素必須定義equals()方法以確保對象的惟一性。 |
HashSet | 爲快速查找而設計的Set。存入HashSet的元素必須定義hashCode() |
TreeSet | 保持次序的Set,底層爲樹結構。使用它能夠從set中提取有序的序列。元素必須實現Comparable接口 |
LinkedHashSet | 具備HashSet的查詢速度,且內部使用鏈表維護元素的順序。 |
使用特定的Set實現類型而必須定義的方法:app
import java.util.*; class SetType { int i; public SetType(int n) { i = n; } public boolean equals(Object o) { return o instanceof SetType && (i == ((SetType)o).i); } public String toString() { return Integer.toString(i); } } class HashType extends SetType { public HashType(int n) { super(n); } public int hashCode() { return i; } } class TreeType extends SetType implements Comparable<TreeType> { public TreeType(int n) { super(n); } public int compareTo(TreeType arg) { return (arg.i < i ? -1 : (arg.i == i ? 0 : 1)); } } public class TypesForSets { static <T> Set<T> fill(Set<T> set, Class<T> type) { try { for(int i = 0; i < 10; i++) set.add( type.getConstructor(int.class).newInstance(i)); } catch(Exception e) { throw new RuntimeException(e); } return set; } static <T> void test(Set<T> set, Class<T> type) { fill(set, type); fill(set, type); // Try to add duplicates fill(set, type); System.out.println(set); } public static void main(String[] args) { test(new HashSet<HashType>(), HashType.class);//實現hashCode確保排序而且不會重複 test(new LinkedHashSet<HashType>(), HashType.class); test(new TreeSet<TreeType>(), TreeType.class);//實現Comparable接口排序並不會重複 // Things that don't work: test(new HashSet<SetType>(), SetType.class); test(new HashSet<TreeType>(), TreeType.class); test(new LinkedHashSet<SetType>(), SetType.class); test(new LinkedHashSet<TreeType>(), TreeType.class); try { test(new TreeSet<SetType>(), SetType.class); } catch(Exception e) { System.out.println(e.getMessage()); } try { test(new TreeSet<HashType>(), HashType.class); } catch(Exception e) { System.out.println(e.getMessage()); } } } /* Output: (Sample) [2, 4, 9, 8, 6, 1, 3, 7, 5, 0] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] [9, 9, 7, 5, 1, 2, 6, 3, 0, 7, 2, 4, 4, 7, 9, 1, 3, 6, 2, 4, 3, 0, 5, 0, 8, 8, 8, 6, 5, 1] [0, 5, 5, 6, 5, 0, 3, 1, 9, 8, 4, 2, 3, 9, 7, 3, 4, 4, 0, 7, 1, 9, 6, 2, 1, 8, 2, 8, 6, 7] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9] java.lang.ClassCastException: SetType cannot be cast to java.lang.Comparable java.lang.ClassCastException: HashType cannot be cast to java.lang.Comparable *///:~
SortedSet能夠確保元素處於排序狀態下:框架
import java.util.*; import static net.mindview.util.Print.*; public class SortedSetDemo { public static void main(String[] args) { SortedSet<String> sortedSet = new TreeSet<String>(); Collections.addAll(sortedSet, "one two three four five six seven eight" .split(" ")); print(sortedSet); String low = sortedSet.first();//返回第一個元素 String high = sortedSet.last();//返回最後一個元素 print(low); print(high); Iterator<String> it = sortedSet.iterator(); for(int i = 0; i <= 6; i++) { if(i == 3) low = it.next(); if(i == 6) high = it.next(); else it.next(); } print(low); print(high); print(sortedSet.subSet(low, high));//生成此Set子集,從low到high print(sortedSet.headSet(high));//生成此Set子集,小於high print(sortedSet.tailSet(low));//生成此Set子集,大於等於low } }
Queue在Java SE5中僅有的兩個實現LinkedList和PriorityQueue,它們的差別在於排序行爲而不是性能。
Queue實現的大部分操做的基本示例:
import java.util.concurrent.*; import java.util.*; import net.mindview.util.*; public class QueueBehavior { private static int count = 10; static <T> void test(Queue<T> queue, Generator<T> gen) { for(int i = 0; i < count; i++) queue.offer(gen.next()); while(queue.peek() != null) System.out.print(queue.remove() + " "); System.out.println(); } static class Gen implements Generator<String> { String[] s = ("one two three four five six seven " + "eight nine ten").split(" "); int i; public String next() { return s[i++]; } } public static void main(String[] args) { test(new LinkedList<String>(), new Gen()); test(new PriorityQueue<String>(), new Gen()); test(new ArrayBlockingQueue<String>(count), new Gen()); test(new ConcurrentLinkedQueue<String>(), new Gen()); test(new LinkedBlockingQueue<String>(), new Gen()); test(new PriorityBlockingQueue<String>(), new Gen()); } } /* Output: one two three four five six seven eight nine ten eight five four nine one seven six ten three two one two three four five six seven eight nine ten one two three four five six seven eight nine ten one two three four five six seven eight nine ten eight five four nine one seven six ten three two *///:~
import java.util.*; class ToDoList extends PriorityQueue<ToDoList.ToDoItem> { static class ToDoItem implements Comparable<ToDoItem> { private char primary; private int secondary; private String item; public ToDoItem(String td, char pri, int sec) { primary = pri; secondary = sec; item = td; } public int compareTo(ToDoItem arg) { if(primary > arg.primary) return +1; if(primary == arg.primary) if(secondary > arg.secondary) return +1; else if(secondary == arg.secondary) return 0; return -1; } public String toString() { return Character.toString(primary) + secondary + ": " + item; } } public void add(String td, char pri, int sec) { super.add(new ToDoItem(td, pri, sec)); } public static void main(String[] args) { ToDoList toDoList = new ToDoList(); toDoList.add("Empty trash", 'C', 4); toDoList.add("Feed dog", 'A', 2); toDoList.add("Feed bird", 'B', 7); toDoList.add("Mow lawn", 'C', 3); toDoList.add("Water lawn", 'A', 1); toDoList.add("Feed cat", 'B', 1); while(!toDoList.isEmpty())System.out.println(toDoList.remove()); } } /* Output: A1: Water lawn A2: Feed dog B1: Feed cat B7: Feed bird C3: Mow lawn C4: Empty trash *///:~
雙向隊列就像是一個隊列,可是以能夠在任何一端添加或移除元素。
實現一個雙向隊列:
package net.mindview.util; import java.util.LinkedList; public class Deque<T> { private LinkedList<T> deque = new LinkedList(); public Deque() { } public void addFirst(T e) { this.deque.addFirst(e); } public void addLast(T e) { this.deque.addLast(e); } public T getFirst() { return this.deque.getFirst(); } public T getLast() { return this.deque.getLast(); } public T removeFirst() { return this.deque.removeFirst(); } public T removeLast() { return this.deque.removeLast(); } public int size() { return this.deque.size(); } public String toString() { return this.deque.toString(); } }
測試Deque:
import net.mindview.util.*; import static net.mindview.util.Print.*; public class DequeTest { static void fillTest(Deque<Integer> deque) { for(int i = 20; i < 27; i++) deque.addFirst(i); for(int i = 50; i < 55; i++) deque.addLast(i); } public static void main(String[] args) { Deque<Integer> di = new Deque<Integer>(); fillTest(di); print(di); while(di.size() != 0) printnb(di.removeFirst() + " "); print(); fillTest(di); while(di.size() != 0) printnb(di.removeLast() + " "); } } /* Output: [26, 25, 24, 23, 22, 21, 20, 50, 51, 52, 53, 54] 26 25 24 23 22 21 20 50 51 52 53 54 54 53 52 51 50 20 21 22 23 24 25 26 *///:~
映射表(關聯數組)的基本思想是它維護的是鍵-值對關聯,所以你可使用建來查找值。標準的Java類庫中包含了Map的幾種基本實現,包括:HashMap,TreeMap,LinkedHashMap,WeakHashMap,ConcurrentHashMap,IdentityHashMap。它們都有一樣的基本接口Map。
下面對Map簡單實現:
import static net.mindview.util.Print.*; public class AssociativeArray<K,V> { private Object[][] pairs; private int index; public AssociativeArray(int length) { pairs = new Object[length][2]; } public void put(K key, V value) { if(index >= pairs.length) throw new ArrayIndexOutOfBoundsException(); pairs[index++] = new Object[]{ key, value }; } @SuppressWarnings("unchecked") public V get(K key) { for(int i = 0; i < index; i++) if(key.equals(pairs[i][0])) return (V)pairs[i][1]; return null; // Did not find key } public String toString() { StringBuilder result = new StringBuilder(); for(int i = 0; i < index; i++) { result.append(pairs[i][0].toString()); result.append(" : "); result.append(pairs[i][1].toString()); if(i < index - 1) result.append("\n"); } return result.toString(); } public static void main(String[] args) { AssociativeArray<String,String> map = new AssociativeArray<String,String>(6); map.put("sky", "blue"); map.put("grass", "green"); map.put("ocean", "dancing"); map.put("tree", "tall"); map.put("earth", "brown"); map.put("sun", "warm"); try { map.put("extra", "object"); // Past the end } catch(ArrayIndexOutOfBoundsException e) { print("Too many objects!"); } print(map); print(map.get("ocean")); } }
HashMap使用了特殊的值,稱做散列碼,來取代對健的緩慢搜索。
|名稱|描述|
|:--:|:--|
|HashMap|Map基於散列表的實現。插入和查詢鍵值對的開銷是固定的。能夠i經過構造器設置容量和負載因子,以調整容器的性能|
|LinkedHashMap|相似於HashMap,可是迭代遍歷它時,取得鍵值對的順序是其插入次序,或者是最近最少使用(LRU)次序。只比HashMap慢一點。而在迭代訪問時反而更快,由於它使用鏈表維護內部次序|
|TreeMap|基於紅黑樹的實現,是惟一帶有subMap()的Map|
|WeakHashMap|弱鍵映射,容許釋放映射所指向的對象|
|ConcurrentHashMap|一種線程安全的Map,不涉及同步鎖|
|IdentityHashMap|使用==代替equals()對鍵進行比較的散列映射|
散列是映射中存儲元素時最經常使用的方式。
對Map中使用的鍵要求與對Set中的元素要求一致。
展現經過Map接口可用的操做:
import java.util.concurrent.*; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class Maps { public static void printKeys(Map<Integer,String> map) { printnb("Size = " + map.size() + ", "); printnb("Keys: "); print(map.keySet()); // Produce a Set of the keys } public static void test(Map<Integer,String> map) { print(map.getClass().getSimpleName()); map.putAll(new CountingMapData(25)); // Map has 'Set' behavior for keys: map.putAll(new CountingMapData(25)); printKeys(map); // Producing a Collection of the values: printnb("Values: "); print(map.values()); print(map); print("map.containsKey(11): " + map.containsKey(11)); print("map.get(11): " + map.get(11)); print("map.containsValue(\"F0\"): " + map.containsValue("F0")); Integer key = map.keySet().iterator().next(); print("First key in map: " + key); map.remove(key); printKeys(map); map.clear(); print("map.isEmpty(): " + map.isEmpty()); map.putAll(new CountingMapData(25)); // Operations on the Set change the Map: map.keySet().removeAll(map.keySet()); print("map.isEmpty(): " + map.isEmpty()); } public static void main(String[] args) { test(new HashMap<Integer,String>()); test(new TreeMap<Integer,String>()); test(new LinkedHashMap<Integer,String>()); test(new IdentityHashMap<Integer,String>()); test(new ConcurrentHashMap<Integer,String>()); test(new WeakHashMap<Integer,String>()); } }
使用SortedMap(TreeMap是其現階段惟一實現),能夠確保鍵處於排序狀態。
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class SortedMapDemo { public static void main(String[] args) { TreeMap<Integer,String> sortedMap = new TreeMap<Integer,String>(new CountingMapData(10)); print(sortedMap); Integer low = sortedMap.firstKey(); Integer high = sortedMap.lastKey(); print(low); print(high); Iterator<Integer> it = sortedMap.keySet().iterator(); for(int i = 0; i <= 6; i++) { if(i == 3) low = it.next(); if(i == 6) high = it.next(); else it.next(); } print(low); print(high); print(sortedMap.subMap(low, high)); print(sortedMap.headMap(high)); print(sortedMap.tailMap(low)); } }
爲了提升速度,LinkedHashMap散列化全部的元素,可是在遍歷鍵值對時,卻又以元素的插入順序返回鍵值對。
能夠在構造器中設定LinkedHashMap,使之採用基於訪問最近最少使用算法,因而沒有被訪問過的元素就會出如今隊列的前面。
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class LinkedHashMapDemo { public static void main(String[] args) { LinkedHashMap<Integer,String> linkedMap = new LinkedHashMap<Integer,String>( new CountingMapData(9)); print(linkedMap); // Least-recently-used order: linkedMap = new LinkedHashMap<Integer,String>(16, 0.75f, true); linkedMap.putAll(new CountingMapData(9)); print(linkedMap); for(int i = 0; i < 6; i++) // Cause accesses: linkedMap.get(i); print(linkedMap); linkedMap.get(0); print(linkedMap); } } /* Output: {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0} {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0} {6=G0, 7=H0, 8=I0, 0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0} {6=G0, 7=H0, 8=I0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 0=A0} *///:~
當本身建立用做HashMap的鍵的類,有可能會忘記在其中放置必須的方法,而這是一般會犯的一個錯誤:
public class Groundhog { protected int number; public Groundhog(int n) { number = n; } public String toString() { return "Groundhog #" + number; } }
import java.util.*; public class Prediction { private static Random rand = new Random(47); private boolean shadow = rand.nextDouble() > 0.5; public String toString() { if(shadow) return "Six more weeks of Winter!"; else return "Early Spring!"; } } ///:~
import java.lang.reflect.*; import java.util.*; import static net.mindview.util.Print.*; public class SpringDetector { // Uses a Groundhog or class derived from Groundhog: public static <T extends Groundhog> void detectSpring(Class<T> type) throws Exception { Constructor<T> ghog = type.getConstructor(int.class);// 經過類對象的getConstructor()或getDeclaredConstructor()方法得到構造器(Constructor)對象並調用其newInstance()方法建立對象 Map<Groundhog,Prediction> map = new HashMap<Groundhog,Prediction>(); for(int i = 0; i < 10; i++) map.put(ghog.newInstance(i), new Prediction()); print("map = " + map); Groundhog gh = ghog.newInstance(3); print("Looking up prediction for " + gh); if(map.containsKey(gh)) print(map.get(gh)); else print("Key not found: " + gh); } public static void main(String[] args) throws Exception { detectSpring(Groundhog.class); } } /* Output: map = {Groundhog #3=Early Spring!, Groundhog #7=Early Spring!, Groundhog #5=Early Spring!, Groundhog #9=Six more weeks of Winter!, Groundhog #8=Six more weeks of Winter!, Groundhog #0=Six more weeks of Winter!, Groundhog #6=Early Spring!, Groundhog #4=Six more weeks of Winter!, Groundhog #1=Six more weeks of Winter!, Groundhog #2=Early Spring!} Looking up prediction for Groundhog #3 Key not found: Groundhog #3 *///:~
從上面結果能夠看到,在map中沒法找到3這個key,那是由於這裏Groundhog 沒有實現hashCode()和equals()這兩個方法,默認使用Object中的這兩個方法進行比較,Object中的hashCode()是根據對象地址計算出來的。
重載實現兩個方法便可:
public class Groundhog { protected int number; public Groundhog(int n) { number = n; } public String toString() { return "Groundhog #" + number; } @Override public int hashCode() { return number; } @Override public boolean equals(Object obj) { return obj instanceof Groundhog && (number==((Groundhog)obj).number); } ///:~
ashMap使用equals()判斷當前的鍵是否與表中存在的鍵相同。
正確的equals()方法必須知足下面5個條件:
對任何不是null的x,x.equals(null)必定返回false。
import java.util.*; import net.mindview.util.*; public class SlowMap<K,V> extends AbstractMap<K,V> {//使用索引位置來關聯表 private List<K> keys = new ArrayList<K>(); private List<V> values = new ArrayList<V>(); public V put(K key, V value) { V oldValue = get(key); // The old value or null if(!keys.contains(key)) { keys.add(key); values.add(value); } else values.set(keys.indexOf(key), value); return oldValue; } public V get(Object key) { // key is type Object, 這裏使用Object是由於泛型引入太晚致使 if(!keys.contains(key)) return null; return values.get(keys.indexOf(key)); } public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> set= new HashSet<Map.Entry<K,V>>(); Iterator<K> ki = keys.iterator(); Iterator<V> vi = values.iterator(); while(ki.hasNext()) set.add(new MapEntry<K,V>(ki.next(), vi.next())); return set; } public static void main(String[] args) { SlowMap<String,String> m= new SlowMap<String,String>(); m.putAll(Countries.capitals(15)); System.out.println(m); System.out.println(m.get("BULGARIA")); System.out.println(m.entrySet()); } }
Map.Entry是一個接口,用來描述依賴於實現的結構,若是要建立本身的Map類型,就必須同時定義Map.Entry的實現:
import java.util.*; public class MapEntry<K,V> implements Map.Entry<K,V> { private K key; private V value; public MapEntry(K key, V value) { this.key = key; this.value = value; } public K getKey() { return key; } public V getValue() { return value; } public V setValue(V v) { V result = value; value = v; return result; } public int hashCode() { return (key==null ? 0 : key.hashCode()) ^ (value==null ? 0 : value.hashCode()); } public boolean equals(Object o) { if(!(o instanceof MapEntry)) return false; MapEntry me = (MapEntry)o; return (key == null ? me.getKey() == null : key.equals(me.getKey())) && (value == null ? me.getValue()== null : value.equals(me.getValue())); } public String toString() { return key + "=" + value; } }
散列的價值在於速度:散列使得查詢得以快速進行。
散列跟進一步,它將鍵保持在某處,以便可以很快找到。存儲一組元素最快的數據結構就是數組,因此使用它來表示鍵的信息。
數組不能調整容量,但咱們不但願鍵的數量被數組的容量限制。
解決辦法:數組並不保存鍵自己。而是經過鍵對象生成一個數字,將其做爲數組的下標,這個數字就是散列碼。
爲解決數組容量被固定的問題,不一樣的鍵能夠產生相同的下標。也就是說,可能會有衝突。所以,數組多大就不重要了,任何鍵總能在數組中找到位置。
因而查詢一個值得過程首先就是計算散列碼,而後使用散列碼查詢數組。若是可以保證沒有衝突,那就是一個完美散列函數了。可是這種狀況只是特例。衝突有外部連接處理:數組並不直接保存值,而是保存值得list。而後對list中的值使用equals()方法進行線性查詢。
public class SimpleHashMap<K,V> extends AbstractMap<K,V> { // Choose a prime number for the hash table // size, to achieve a uniform distribution: static final int SIZE = 997; // You can't have a physical array of generics, // but you can upcast to one: @SuppressWarnings("unchecked") LinkedList<MapEntry<K,V>>[] buckets = new LinkedList[SIZE]; public V put(K key, V value) { V oldValue = null; int index = Math.abs(key.hashCode()) % SIZE; if(buckets[index] == null) buckets[index] = new LinkedList<MapEntry<K,V>>(); LinkedList<MapEntry<K,V>> bucket = buckets[index]; MapEntry<K,V> pair = new MapEntry<K,V>(key, value); boolean found = false; ListIterator<MapEntry<K,V>> it = bucket.listIterator(); while(it.hasNext()) { MapEntry<K,V> iPair = it.next(); if(iPair.getKey().equals(key)) { oldValue = iPair.getValue(); it.set(pair); // Replace old with new found = true; break; } } if(!found) buckets[index].add(pair); return oldValue; } public V get(Object key) { int index = Math.abs(key.hashCode()) % SIZE; if(buckets[index] == null) return null; for(MapEntry<K,V> iPair : buckets[index]) if(iPair.getKey().equals(key)) return iPair.getValue(); return null; } public Set<Map.Entry<K,V>> entrySet() { Set<Map.Entry<K,V>> set= new HashSet<Map.Entry<K,V>>(); for(LinkedList<MapEntry<K,V>> bucket : buckets) { if(bucket == null) continue; for(MapEntry<K,V> mpair : bucket) set.add(mpair); } return set; } public static void main(String[] args) { SimpleHashMap<String,String> m = new SimpleHashMap<String,String>(); m.putAll(Countries.capitals(25)); System.out.println(m); System.out.println(m.get("ERITREA")); System.out.println(m.entrySet()); } }
設計hashCode()最重要的因素就是:不管什麼時候,對同一個對象調用hashCode()都應該產生一樣的值。
下面String爲例。String有個特色:若是程序中有多個String對象,都包含相同的字符串序列,那麼這些String對象都映射到同一塊內存區域:
public class StringHashCode { public static void main(String[] args) { String[] hellos = "Hello Hello".split(" "); System.out.println(hellos[0].hashCode()); System.out.println(hellos[1].hashCode()); } } /* Output: (Sample) 69609650 69609650 *///:~
對於String而言,hashCode()明顯是基於String的內容的。
hashCode()的散列碼必須是基於內容生成,沒必要獨一無二,可是經過hashCOde()和equals()必須可以徹底肯定對象。
怎樣寫出一份像樣的hashCode基本指導:
import java.util.*; import static net.mindview.util.Print.*; public class CountedString { private static List<String> created = new ArrayList<String>(); private String s; private int id = 0; public CountedString(String str) { s = str; created.add(s); // id is the total number of instances // of this string in use by CountedString: for(String s2 : created) if(s2.equals(s)) id++; } public String toString() { return "String: " + s + " id: " + id + " hashCode(): " + hashCode(); } public int hashCode() { // The very simple approach: // return s.hashCode() * id; // Using Joshua Bloch's recipe: int result = 17; result = 37 * result + s.hashCode(); result = 37 * result + id; return result; } public boolean equals(Object o) { return o instanceof CountedString && s.equals(((CountedString)o).s) && id == ((CountedString)o).id; } public static void main(String[] args) { Map<CountedString,Integer> map = new HashMap<CountedString,Integer>(); CountedString[] cs = new CountedString[5]; for(int i = 0; i < cs.length; i++) { cs[i] = new CountedString("hi"); map.put(cs[i], i); // Autobox int -> Integer } print(map); for(CountedString cstring : cs) { print("Looking up " + cstring); print(map.get(cstring)); } } } /* Output: (Sample) {String: hi id: 4 hashCode(): 146450=3, String: hi id: 1 hashCode(): 146447=0, String: hi id: 3 hashCode(): 146449=2, String: hi id: 5 hashCode(): 146451=4, String: hi id: 2 hashCode(): 146448=1} Looking up String: hi id: 1 hashCode(): 146447 0 Looking up String: hi id: 2 hashCode(): 146448 1 Looking up String: hi id: 3 hashCode(): 146449 2 Looking up String: hi id: 4 hashCode(): 146450 3 Looking up String: hi id: 5 hashCode(): 146451 4 *///:~
第二個實例Individual:
package pets; public class Individual implements Comparable<Individual> { private static long counter = 0; private final long id = counter++; private String name; public Individual(String name) { this.name = name; } // 'name' is optional: public Individual() {} public String toString() { return getClass().getSimpleName() + (name == null ? "" : " " + name); } public long id() { return id; } public boolean equals(Object o) { return o instanceof Individual && id == ((Individual)o).id; } public int hashCode() { int result = 17; if(name != null) result = 37 * result + name.hashCode(); result = 37 * result + (int)id; return result; } public int compareTo(Individual arg) { // Compare by class name first: String first = getClass().getSimpleName(); String argFirst = arg.getClass().getSimpleName(); int firstCompare = first.compareTo(argFirst); if(firstCompare != 0) return firstCompare; if(name != null && arg.name != null) { int secondCompare = name.compareTo(arg.name); if(secondCompare != 0) return secondCompare; } return (arg.id < id ? -1 : (arg.id == id ? 0 : 1)); } } ///:~
compareTO()方法有一個比較結構,所以它會產生一個排序序列,排序規則首先按照實際類型排序,而後若是有名字按名字排序,最後按建立順序排序:
import pets.*; import java.util.*; public class IndividualTest { public static void main(String[] args) { Set<Individual> pets = new TreeSet<Individual>(); for(List<? extends Pet> lp : MapOfList.petPeople.values()) for(Pet p : lp) pets.add(p); System.out.println(pets); } } /* Output: [Cat Elsie May, Cat Pinkola, Cat Shackleton, Cat Stanford aka Stinky el Negro, Cymric Molly, Dog Margrett, Mutt Spot, Pug Louie aka Louis Snorkelstein Dupree, Rat Fizzy, Rat Freckly, Rat Fuzzy] *///:~
儘管實際上只有4種容器:Map,List,Set,Queue,可是每種接口都有不止一個實現版本。
容器之間的區別一般歸結爲由什麼在背後支持它們。也就是說,所使用的接口是由什麼樣的數據結構實現的。
ArrayList底層由數組支持,LinkedList由雙向鏈表實現。
Set被實現爲TreeSet(基於TreeMap,生成一個老是處於排序狀態的Set),HashSet(查詢速度最快)或LinkedHashSet(保持元素插入順序)。
下面的代碼構建了一個基類,從中能夠建立一個匿名內部類列表,其中每一個匿名內部類都用於各類不一樣的測試,它們每一個都被看成測試過程的一部分而被調用。
待測容器類型是泛型參數C。每一個對象都存儲了該測試的名字。
public abstract class Test<C> { String name; public Test(String name) { this.name = name; } // Override this method for different tests. // Returns actual number of repetitions of test. abstract int test(C container, TestParam tp); } ///:~
第二個版本,接受相同類型的列表,可是它的值都在String中——經過這種方式,它能夠用來解析命令行參數:
public class TestParam { public final int size; public final int loops; public TestParam(int size, int loops) { this.size = size; this.loops = loops; } // Create an array of TestParam from a varargs sequence: public static TestParam[] array(int... values) { int size = values.length/2; TestParam[] result = new TestParam[size]; int n = 0; for(int i = 0; i < size; i++) result[i] = new TestParam(values[n++], values[n++]); return result; } // Convert a String array to a TestParam array: public static TestParam[] array(String[] values) { int[] vals = new int[values.length]; for(int i = 0; i < vals.length; i++) vals[i] = Integer.decode(values[i]); return array(vals); } } ///:~
import java.util.*; public class Tester<C> { public static int fieldWidth = 8;//標準寬度 public static TestParam[] defaultParams= TestParam.array( 10, 5000, 100, 5000, 1000, 5000, 10000, 500); // Override this to modify pre-test initialization: protected C initialize(int size) { return container; }//可覆蓋執行特殊的初始化 protected C container; private String headline = ""; private List<Test<C>> tests; private static String stringField() {//格式化字符串 return "%" + fieldWidth + "s"; } private static String numberField() { return "%" + fieldWidth + "d"; } private static int sizeWidth = 5; private static String sizeField = "%" + sizeWidth + "s"; private TestParam[] paramList = defaultParams; public Tester(C container, List<Test<C>> tests) { this.container = container; this.tests = tests; if(container != null) headline = container.getClass().getSimpleName(); } public Tester(C container, List<Test<C>> tests, TestParam[] paramList) { this(container, tests); this.paramList = paramList; } public void setHeadline(String newHeadline) { headline = newHeadline; } // Generic methods for convenience : public static <C> void run(C cntnr, List<Test<C>> tests){ new Tester<C>(cntnr, tests).timedTest(); } public static <C> void run(C cntnr, List<Test<C>> tests, TestParam[] paramList) { new Tester<C>(cntnr, tests, paramList).timedTest(); } private void displayHeader() {//頭信息 // Calculate width and pad with '-': int width = fieldWidth * tests.size() + sizeWidth; int dashLength = width - headline.length() - 1; StringBuilder head = new StringBuilder(width); for(int i = 0; i < dashLength/2; i++) head.append('-'); head.append(' '); head.append(headline); head.append(' '); for(int i = 0; i < dashLength/2; i++) head.append('-'); System.out.println(head); // Print column headers: System.out.format(sizeField, "size"); for(Test test : tests) System.out.format(stringField(), test.name); System.out.println(); } // Run the tests for this container: public void timedTest() { displayHeader(); for(TestParam param : paramList) { System.out.format(sizeField, param.size); for(Test<C> test : tests) { C kontainer = initialize(param.size); long start = System.nanoTime();//計算操做所需納秒數 // Call the overriden method: int reps = test.test(kontainer, param); long duration = System.nanoTime() - start; long timePerRep = duration / reps; // Nanoseconds System.out.format(numberField(), timePerRep); } System.out.println(); } } } ///:~
List操做中最本質部分的性能測試,它還展現了Queue中最重要的操做。在測試中,Queue操做只應用到LinkedList之上:
//: containers/ListPerformance.java // Demonstrates performance differences in Lists. // {Args: 100 500} Small to keep build testing short import java.util.*; import net.mindview.util.*; public class ListPerformance { static Random rand = new Random(); static int reps = 1000; static List<Test<List<Integer>>> tests = new ArrayList<Test<List<Integer>>>(); static List<Test<LinkedList<Integer>>> qTests = new ArrayList<Test<LinkedList<Integer>>>(); static { tests.add(new Test<List<Integer>>("add") { int test(List<Integer> list, TestParam tp) { int loops = tp.loops; int listSize = tp.size; for (int i = 0; i < loops; i++) { list.clear(); for (int j = 0; j < listSize; j++) list.add(j); } return loops * listSize; } }); tests.add(new Test<List<Integer>>("get") { int test(List<Integer> list, TestParam tp) { int loops = tp.loops * reps; int listSize = list.size(); for (int i = 0; i < loops; i++) list.get(rand.nextInt(listSize)); return loops; } }); tests.add(new Test<List<Integer>>("set") { int test(List<Integer> list, TestParam tp) { int loops = tp.loops * reps; int listSize = list.size(); for (int i = 0; i < loops; i++) list.set(rand.nextInt(listSize), 47); return loops; } }); tests.add(new Test<List<Integer>>("iteradd") { int test(List<Integer> list, TestParam tp) { final int LOOPS = 1000000; int half = list.size() / 2; ListIterator<Integer> it = list.listIterator(half); for (int i = 0; i < LOOPS; i++) it.add(47); return LOOPS; } }); tests.add(new Test<List<Integer>>("insert") { int test(List<Integer> list, TestParam tp) { int loops = tp.loops; for (int i = 0; i < loops; i++) list.add(5, 47); // Minimize random-access cost return loops; } }); tests.add(new Test<List<Integer>>("remove") { int test(List<Integer> list, TestParam tp) { int loops = tp.loops; int size = tp.size; for (int i = 0; i < loops; i++) { list.clear(); list.addAll(new CountingIntegerList(size)); while (list.size() > 5) list.remove(5); // Minimize random-access cost } return loops * size; } }); // Tests for queue behavior: qTests.add(new Test<LinkedList<Integer>>("addFirst") { int test(LinkedList<Integer> list, TestParam tp) { int loops = tp.loops; int size = tp.size; for (int i = 0; i < loops; i++) { list.clear(); for (int j = 0; j < size; j++) list.addFirst(47); } return loops * size; } }); qTests.add(new Test<LinkedList<Integer>>("addLast") { int test(LinkedList<Integer> list, TestParam tp) { int loops = tp.loops; int size = tp.size; for (int i = 0; i < loops; i++) { list.clear(); for (int j = 0; j < size; j++) list.addLast(47); } return loops * size; } }); qTests.add( new Test<LinkedList<Integer>>("rmFirst") { int test(LinkedList<Integer> list, TestParam tp) { int loops = tp.loops; int size = tp.size; for (int i = 0; i < loops; i++) { list.clear(); list.addAll(new CountingIntegerList(size)); while (list.size() > 0) list.removeFirst(); } return loops * size; } }); qTests.add(new Test<LinkedList<Integer>>("rmLast") { int test(LinkedList<Integer> list, TestParam tp) { int loops = tp.loops; int size = tp.size; for (int i = 0; i < loops; i++) { list.clear(); list.addAll(new CountingIntegerList(size)); while (list.size() > 0) list.removeLast(); } return loops * size; } }); } static class ListTester extends Tester<List<Integer>> { public ListTester(List<Integer> container, List<Test<List<Integer>>> tests) { super(container, tests); } // Fill to the appropriate size before each test: @Override protected List<Integer> initialize(int size) { container.clear(); container.addAll(new CountingIntegerList(size)); return container; } // Convenience method: public static void run(List<Integer> list, List<Test<List<Integer>>> tests) { new ListTester(list, tests).timedTest(); } } public static void main(String[] args) { if (args.length > 0) Tester.defaultParams = TestParam.array(args); // Can only do these two tests on an array: Tester<List<Integer>> arrayTest = new Tester<List<Integer>>(null, tests.subList(1, 3)) { // This will be called before each test. It // produces a non-resizeable array-backed list: @Override protected List<Integer> initialize(int size) { Integer[] ia = Generated.array(Integer.class, new CountingGenerator.Integer(), size); return Arrays.asList(ia); } }; arrayTest.setHeadline("Array as List"); arrayTest.timedTest(); Tester.defaultParams = TestParam.array( 10, 5000, 100, 5000, 1000, 1000, 10000, 200); if (args.length > 0) Tester.defaultParams = TestParam.array(args); ListTester.run(new ArrayList<Integer>(), tests); ListTester.run(new LinkedList<Integer>(), tests); ListTester.run(new Vector<Integer>(), tests); Tester.fieldWidth = 12; Tester<LinkedList<Integer>> qTest = new Tester<LinkedList<Integer>>( new LinkedList<Integer>(), qTests); qTest.setHeadline("Queue tests"); qTest.timedTest(); } } /* Output: (Sample) --- Array as List --- size get set 10 130 183 100 130 164 1000 129 165 10000 129 165 --------------------- ArrayList --------------------- size add get set iteradd insert remove 10 121 139 191 435 3952 446 100 72 141 191 247 3934 296 1000 98 141 194 839 2202 923 10000 122 144 190 6880 14042 7333 --------------------- LinkedList --------------------- size add get set iteradd insert remove 10 182 164 198 658 366 262 100 106 202 230 457 108 201 1000 133 1289 1353 430 136 239 10000 172 13648 13187 435 255 239 ----------------------- Vector ----------------------- size add get set iteradd insert remove 10 129 145 187 290 3635 253 100 72 144 190 263 3691 292 1000 99 145 193 846 2162 927 10000 108 145 186 6871 14730 7135 -------------------- Queue tests -------------------- size addFirst addLast rmFirst rmLast 10 199 163 251 253 100 98 92 180 179 1000 99 93 216 212 10000 111 109 262 384 *///:~
get和set測試都使用了隨機數生成器來執行對List的隨機訪問。對於背後是數組支撐的List和ArrayList,不管列表大小如何,這些訪問都快速一致。而對於LinkedList訪問時間對於較大的列代表顯增長,若是須要執行大量隨機訪問,連接鏈表不會是一種好的選擇。
使用迭代器在列表中間插入新的元素。對於ArrayList,當列表變大時,其開銷將變得高昂,可是對於LinkedList,相對來講比較低廉,而且不隨列表尺寸而發生變化。這是由於ArrayList在插入時,必須建立空間並將它的全部引用向前移動,這會隨ArrayList的尺寸增長而產生高昂的代價。LinkedList自須要連接新的元素,而沒必要修改列表中剩餘的元素,一次你能夠認爲列表尺寸若是變化,其代價大體相同。
沒有選擇List兩端的元素時由於LinkedList對List端點會進行特殊處理——這使得在將LinkedList用做Queue時,速度能夠獲得提升。
在LinkedList中的插入和移除代價至關低廉,而且不隨列表尺寸發生變化,可是對於ArrayList,插入操做代價很高昂,而且不隨列表尺寸的增長而增長。
從Queue測試中,能夠看到LinkedList能夠多麼快速的從列表的端點插入和移除元素,這正是對Queue行爲所作的優化。
將ArrayList做爲默認首選,只有你須要使用額外的功能,或者當程序的性能由於常常從表中間進行插入和刪除而變差的時候,纔去選擇LinkedList。
CopyOnWriteArrayList是List的一個特殊實現,專門用於併發編程。
import java.util.*; public class SetPerformance { static List<Test<Set<Integer>>> tests = new ArrayList<Test<Set<Integer>>>(); static { tests.add(new Test<Set<Integer>>("add") { int test(Set<Integer> set, TestParam tp) { int loops = tp.loops; int size = tp.size; for(int i = 0; i < loops; i++) { set.clear(); for(int j = 0; j < size; j++) set.add(j); } return loops * size; } }); tests.add(new Test<Set<Integer>>("contains") { int test(Set<Integer> set, TestParam tp) { int loops = tp.loops; int span = tp.size * 2; for(int i = 0; i < loops; i++) for(int j = 0; j < span; j++) set.contains(j); return loops * span; } }); tests.add(new Test<Set<Integer>>("iterate") { int test(Set<Integer> set, TestParam tp) { int loops = tp.loops * 10; for(int i = 0; i < loops; i++) { Iterator<Integer> it = set.iterator(); while(it.hasNext()) it.next(); } return loops * set.size(); } }); } public static void main(String[] args) { if(args.length > 0) Tester.defaultParams = TestParam.array(args); Tester.fieldWidth = 10; Tester.run(new TreeSet<Integer>(), tests); Tester.run(new HashSet<Integer>(), tests); Tester.run(new LinkedHashSet<Integer>(), tests); } } /* Output: (Sample) ------------- TreeSet ------------- size add contains iterate 10 746 173 89 100 501 264 68 1000 714 410 69 10000 1975 552 69 ------------- HashSet ------------- size add contains iterate 10 308 91 94 100 178 75 73 1000 216 110 72 10000 711 215 100 ---------- LinkedHashSet ---------- size add contains iterate 10 350 65 83 100 270 74 55 1000 303 111 54 10000 1615 256 58 *///:~
HashSet的性能基本上老是比TreeSet的好,特別是在添加和查詢元素時,而這兩個操做是最重要的操做。TreeSet存在的惟一緣由是它能夠維持元素的排序狀態。
對於插入操做,LinkedHashSet比HashSet的代價更高,這是由維護鏈表所帶來額外開銷形成的。
//: containers/MapPerformance.java // Demonstrates performance differences in Maps. // {Args: 100 5000} Small to keep build testing short import java.util.*; public class MapPerformance { static List<Test<Map<Integer,Integer>>> tests = new ArrayList<Test<Map<Integer,Integer>>>(); static { tests.add(new Test<Map<Integer,Integer>>("put") { int test(Map<Integer,Integer> map, TestParam tp) { int loops = tp.loops; int size = tp.size; for(int i = 0; i < loops; i++) { map.clear(); for(int j = 0; j < size; j++) map.put(j, j); } return loops * size; } }); tests.add(new Test<Map<Integer,Integer>>("get") { int test(Map<Integer,Integer> map, TestParam tp) { int loops = tp.loops; int span = tp.size * 2; for(int i = 0; i < loops; i++) for(int j = 0; j < span; j++) map.get(j); return loops * span; } }); tests.add(new Test<Map<Integer,Integer>>("iterate") { int test(Map<Integer,Integer> map, TestParam tp) { int loops = tp.loops * 10; for(int i = 0; i < loops; i ++) { Iterator it = map.entrySet().iterator(); while(it.hasNext()) it.next(); } return loops * map.size(); } }); } public static void main(String[] args) { if(args.length > 0) Tester.defaultParams = TestParam.array(args); Tester.run(new TreeMap<Integer,Integer>(), tests); Tester.run(new HashMap<Integer,Integer>(), tests); Tester.run(new LinkedHashMap<Integer,Integer>(),tests); Tester.run( new IdentityHashMap<Integer,Integer>(), tests); Tester.run(new WeakHashMap<Integer,Integer>(), tests); Tester.run(new Hashtable<Integer,Integer>(), tests); } } /* Output: (Sample) ---------- TreeMap ---------- size put get iterate 10 748 168 100 100 506 264 76 1000 771 450 78 10000 2962 561 83 ---------- HashMap ---------- size put get iterate 10 281 76 93 100 179 70 73 1000 267 102 72 10000 1305 265 97 ------- LinkedHashMap ------- size put get iterate 10 354 100 72 100 273 89 50 1000 385 222 56 10000 2787 341 56 ------ IdentityHashMap ------ size put get iterate 10 290 144 101 100 204 287 132 1000 508 336 77 10000 767 266 56 -------- WeakHashMap -------- size put get iterate 10 484 146 151 100 292 126 117 1000 411 136 152 10000 2165 138 555 --------- Hashtable --------- size put get iterate 10 264 113 113 100 181 105 76 1000 260 201 80 10000 1245 134 77 *///:~
全部的Map實現的插入操做都會隨着Map尺寸的變大明顯變慢。可是查找的代價比插入小不少。
Hashtable的性能大致和HashMap至關。由於HashMap是用來替代Hashtable的,所以它們使用了相同的底層存儲和查找機制。
TreeMap是一種建立有序列表的方式。樹的行爲是:老是保證有序,而且沒必要進行特殊的排序。一旦你填充了一個TreeMap,就能夠調用keySet()方法來獲取鍵的Set視圖,而後調用toArray()來產生這些鍵構成的數組。
可使用靜態方法Arrays.binarySearch()在排序數組中快速查找對象。
LingkedHashMap在插入時比HashMap慢一點,由於它維護散列數據結構的同時還要維護鏈表。就是由於這個列表,使得其迭代速度更快。
IdentityHashMap則具備徹底不一樣的性能,由於它使用==而不是equals()來比較元素。
能夠經過手工調整HashMap來提升性能,從而知足咱們特定應用的需求:
容量:表中的桶位數。
初始容量:表在建立時所擁有的桶位數。
尺寸:表中當前存儲的項數。
負載因子:尺寸/容量。負載輕的表產生的可能性小,所以對於插入和查找都是最理想的。
HashMap使用的默認負載因子時0.75,這個因子在時間和空間代價之間達到了平衡。
Java中有大量用於容器得卓越的使用方法,它們表示爲java.util.Collection類nebulous的靜態方法。
import java.util.*; import static net.mindview.util.Print.*; public class Utilities { static List<String> list = Arrays.asList( "one Two three Four five six one".split(" ")); public static void main(String[] args) { print(list); print("'list' disjoint (Four)?: " + Collections.disjoint(list, Collections.singletonList("Four")));//disjoint 兩個集合中沒有相同的元素返回true print("max: " + Collections.max(list)); print("min: " + Collections.min(list));//max,min返回最小或最大的元素,用Comparator比較 print("max w/ comparator: " + Collections.max(list, String.CASE_INSENSITIVE_ORDER)); print("min w/ comparator: " + Collections.min(list, String.CASE_INSENSITIVE_ORDER)); List<String> sublist = Arrays.asList("Four five six".split(" ")); print("indexOfSubList: " + Collections.indexOfSubList(list, sublist));//返回sublist再list中第一個出現的位置,找不到返回-1 print("lastIndexOfSubList: " + Collections.lastIndexOfSubList(list, sublist));//返回sublist再list中最後一次出現的位置,找不到返回-1 Collections.replaceAll(list, "one", "Yo");//使用newVal替換全部的oldVal print("replaceAll: " + list); Collections.reverse(list);//逆轉全部元素次序 print("reverse: " + list); Collections.rotate(list, 3);//全部元素向後移動distance個位置,將最後元素循環到前面來 print("rotate: " + list); List<String> source = Arrays.asList("in the matrix".split(" ")); Collections.copy(list, source); print("copy: " + list); Collections.swap(list, 0, list.size() - 1);//交換list中位置i與位置j的元素。 print("swap: " + list); Collections.shuffle(list, new Random(47));//隨機改變指定列表順序 print("shuffled: " + list); Collections.fill(list, "pop"); print("fill: " + list); print("frequency of 'pop': " + Collections.frequency(list, "pop")); List<String> dups = Collections.nCopies(3, "snap");//返回大小爲n的List<T>,次List不可改變 print("dups: " + dups); print("'list' disjoint 'dups'?: " + Collections.disjoint(list, dups));//當沒有相同的元素時,返回true // Getting an old-style Enumeration: Enumeration<String> e = Collections.enumeration(dups);//生成一箇舊式的Enumeraton<T> Vector<String> v = new Vector<String>(); while(e.hasMoreElements()) v.addElement(e.nextElement()); // Converting an old-style Vector // to a List via an Enumeration: ArrayList<String> arrayList = Collections.list(v.elements()); print("arrayList: " + arrayList); } } /* Output: [one, Two, three, Four, five, six, one] 'list' disjoint (Four)?: false max: three min: Four max w/ comparator: Two min w/ comparator: five indexOfSubList: 3 lastIndexOfSubList: 3 replaceAll: [Yo, Two, three, Four, five, six, Yo] reverse: [Yo, six, five, Four, three, Two, Yo] rotate: [three, Two, Yo, Yo, six, five, Four] copy: [in, the, matrix, Yo, six, five, Four] swap: [Four, the, matrix, Yo, six, five, in] shuffled: [six, matrix, the, Four, Yo, five, in] fill: [pop, pop, pop, pop, pop, pop, pop] frequency of 'pop': 7 dups: [snap, snap, snap] 'list' disjoint 'dups'?: true arrayList: [snap, snap, snap] *///:~
List排序和查詢所使用的方法與對象數組所使用的相應方法有相同的名字與語法,只是用Collections的static方法代替Arrays的方法而已。
import java.util.*; import static net.mindview.util.Print.*; public class ListSortSearch { public static void main(String[] args) { List<String> list = new ArrayList<String>(Utilities.list); list.addAll(Utilities.list); print(list); Collections.shuffle(list, new Random(47)); print("Shuffled: " + list); // Use a ListIterator to trim off the last elements: ListIterator<String> it = list.listIterator(10); while(it.hasNext()) { it.next(); it.remove(); } print("Trimmed: " + list); Collections.sort(list); print("Sorted: " + list); String key = list.get(7); int index = Collections.binarySearch(list, key); print("Location of " + key + " is " + index + ", list.get(" + index + ") = " + list.get(index)); Collections.sort(list, String.CASE_INSENSITIVE_ORDER); print("Case-insensitive sorted: " + list); key = list.get(7); index = Collections.binarySearch(list, key, String.CASE_INSENSITIVE_ORDER); print("Location of " + key + " is " + index + ", list.get(" + index + ") = " + list.get(index)); } }
生成各類只讀容器:
import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class ReadOnly { static Collection<String> data = new ArrayList<String>(Countries.names(6)); public static void main(String[] args) { Collection<String> c = Collections.unmodifiableCollection( new ArrayList<String>(data)); print(c); // Reading is OK //! c.add("one"); // Can't change it List<String> a = Collections.unmodifiableList( new ArrayList<String>(data)); ListIterator<String> lit = a.listIterator(); print(lit.next()); // Reading is OK //! lit.add("one"); // Can't change it Set<String> s = Collections.unmodifiableSet( new HashSet<String>(data)); print(s); // Reading is OK //! s.add("one"); // Can't change it // For a SortedSet: Set<String> ss = Collections.unmodifiableSortedSet( new TreeSet<String>(data)); Map<String,String> m = Collections.unmodifiableMap( new HashMap<String,String>(Countries.capitals(6))); print(m); // Reading is OK //! m.put("Ralph", "Howdy!"); // For a SortedMap: Map<String,String> sm = Collections.unmodifiableSortedMap( new TreeMap<String,String>(Countries.capitals(6))); } }
對特定類型的"不可修改的"方法的調用並不會產生編譯時的檢查,可是轉換完成後,任何改變容器內容的操做都會引起UnsupportedOperationException異常。
import java.util.*; public class Synchronization { public static void main(String[] args) { Collection<String> c = Collections.synchronizedCollection( new ArrayList<String>()); List<String> list = Collections.synchronizedList( new ArrayList<String>()); Set<String> s = Collections.synchronizedSet( new HashSet<String>()); Set<String> ss = Collections.synchronizedSortedSet( new TreeSet<String>()); Map<String,String> m = Collections.synchronizedMap( new HashMap<String,String>()); Map<String,String> sm = Collections.synchronizedSortedMap( new TreeMap<String,String>()); } } ///:~
直接將新生成的容器傳遞給適當的同步方法。
Java容器有一種保護機制,可以防止多個進程同時修改同一個容器的內容。
Java容器類類庫採用快速報錯機制,它會探查容器上的任何除了你進程所進行的操做之外的全部變化,一旦發現其餘進程修改了容器,就會拋出ConcurrentModificationException異常。
import java.util.*; public class FailFast { public static void main(String[] args) { Collection<String> c = new ArrayList<String>(); Iterator<String> it = c.iterator(); c.add("An object"); try { String s = it.next(); } catch(ConcurrentModificationException e) { System.out.println(e); } } } /* Output: java.util.ConcurrentModificationException *///:~