Java Collection knowledge sorting, java Collection knowledge
| |
HashMap |
HashTable |
| Inheritance Method |
extends AbstractMap implements Map |
extends Dictionary implements Map |
| Thread Security |
No |
Yes (all methods are modified by synchronized)
|
| Relative Efficiency |
High |
Low |
| Null keys and values are allowed. |
Yes |
No |
| Methods for determining inclusion |
Containsvalue and containsKey |
Contains |
Default hash array size |
11 |
16 |
Add hash Array |
Old * 2 + 1 |
2 index increase |
1 List <String> list = new ArrayList <String> (); 2 3 String preString = "aa"; 4 for (int j = 0; j <100000000; j ++) {5 list. add (preString); 6} 7 8 // method 1 (fastest, List exclusive) 9 for (int I = 0, len = list. size (); I <len; I ++) {10 list. get (I); 11} 12 13 // method 2 (for each-most time-consuming) 14 for (String tmp: list) {15} 16 17 // method 3 (same as method 2 and 4, applicable to all classes that implement the Iterable interface, common ones include Queue, Set, Collection, List) 18 Iterator <String> iter = list. iterator (); 19 while (iter. hasNext () {20 String str = iter. next (); 21} 22 23 // method 424 for (Iterator <String> it2 = list. iterator (); it2.hasNext ();) {25 String str = it2.next (); 26}
2. map Traversal
1 HashMap <Integer, String> map = new HashMap <> (); 2 String v = "value"; 3 for (int I = 0; I <10000000; I ++) {4 map. put (I, v); 5} 6 7 // method 1, 8 Iterator <Map. entry <Integer, String> it1 = map. entrySet (). iterator (); 9 while (it1.hasNext () {10 Map. entry <Integer, String> entry = it1.next (); 11 int key = entry. getKey (); 12 String value = entry. getValue (); 13} 14 15 // method 2 gets the iterator of the map key set, 7 times the time consumed by method 1. 16 Iterator <Integer> it2 = map. keySet (). iterator (); 17 while (it2.hasNext () {18 int key = it2.next (); 19 String value = map. get (key); 20} 21
The Traversal method of set and map is similar.