forked from JFulgoni/Java-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionTest.java
More file actions
56 lines (49 loc) · 1.59 KB
/
Copy pathCollectionTest.java
File metadata and controls
56 lines (49 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package john_test;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.TreeMap;
public class CollectionTest {
public static void main(String[] args){
/*
* HashMap is pretty standard. O(1) get, items are in arbitrary order
*/
HashMap<Integer, String> map1 = new HashMap<Integer, String>();
map1.put(1, "Steve");
map1.put(2, "Bill");
map1.put(3, "Uri");
map1.put(1, "Not Steve");
/*
* LinkedHashMap offers O(1) lookup and Insertion
* It will keep the order that the items were inserted in
*/
LinkedHashMap<Integer, String> map2 = new LinkedHashMap<Integer, String>();
map2.put(1, "Steve");
map2.put(3, "Uri");
map2.put(2, "Bill");
/*
* TreeMap is logn Lookup and Insertion, but the values are sorted, and can be iterated through
*/
TreeMap<Integer, String> map3 = new TreeMap<Integer, String>();
map3.put(2, "Bill");
map3.put(3, "Uri");
map3.put(1, "Steve");
Iterator<Entry<Integer, String>> iter = map1.entrySet().iterator();
//iter = map1.values().iterator();
System.out.println("With HashMap, order is arbitrary");
while(iter.hasNext()){
System.out.println(iter.next());
}
iter = map2.entrySet().iterator();
System.out.println("\nWith LinkedHashMap, it prints in order of insertion");
while(iter.hasNext()){
System.out.println(iter.next());
}
iter = map3.entrySet().iterator();
System.out.println("\nWith TreeMap, it prints in sorted order, at a slower price to enter and retrieve");
while(iter.hasNext()){
System.out.println(iter.next());
}
}
}