- Java Collection Class diagram
- The difference between HashMap and Hashtable
|
HashMap |
HashTable |
| How to Inherit |
Extends Abstractmap implements Map |
Extends Dictionary implements Map |
| Thread Safety |
Is |
Whether |
| Efficiency relative ratio |
High |
Low |
| Allow null keys and values |
Is |
Whether |
| Determine which methods are included |
Containsvalue and ContainsKey |
Contains |
Hash array Default Size |
11, |
16, |
Hash array Increment mode |
Old*2+1 |
2 Increase in Index |
- The traversal of the list
1list<string> list =NewArraylist<string>();2 3String prestring = "AA";4 for(intj = 0; J < 100000000; J + +) {5 List.add (prestring);6 }7 8 //Method 1 (fastest, list specific)9 for(inti = 0, Len = list.size (); i < Len; i++) {Ten List.get (i); One } A - //Method 2 (for each-is the most time consuming) - for(String tmp:list) { the } - - //Method 3 (as with method 2,4, applies to all classes that implement the Iterable interface, which are common: queue,set,collection,list) -Iterator<string> iter =list.iterator (); + while(Iter.hasnext ()) { -String str =Iter.next (); + } A at //Method 4 - for(Iterator<string> it2 =list.iterator (); It2.hasnext ();) { -String str =It2.next (); -}
Traversal of 2.map
1Hashmap<integer, string> map =NewHashmap<>();2String v = "value";3 for(inti=0;i<10000000;i++){4 Map.put (i, v);5 }6 7 //Method 1,8Iterator<map.entry<integer, string>> it1=Map.entryset (). iterator ();9 while(It1.hasnext ()) {TenMap.entry<integer, string> Entry =It1.next (); One intKey =Entry.getkey (); AString value =Entry.getvalue (); - } - the //Method 2 Gets the iterator for the key collection of map, which is 7 times times the time of Method 1. -Iterator<integer> it2 =Map.keyset (). iterator (); - while(It2.hasnext ()) { - intKey =It2.next (); +String value =Map.get (key); - } +
Set and map traverse in a similar way, it is not here to wordy.
Java Collection Knowledge Collation