-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionsIntroduction.java
More file actions
156 lines (145 loc) · 7.09 KB
/
Copy pathCollectionsIntroduction.java
File metadata and controls
156 lines (145 loc) · 7.09 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package Phase5_CollectionsLambdasStreams.Collections;
/**
* Java Collections Framework - Introduction
* -----------------------------------------
* The COLLECTIONS FRAMEWORK is the standard library for storing and
* manipulating groups of objects. It is one of the most important pieces of
* the JDK; almost every non-trivial Java program uses it.
* <p>
*
* Why a Framework, Not Just Arrays?
* ---------------------------------
* Arrays are simple but limited:
* - FIXED SIZE.
* - One ELEMENT TYPE only.
* - Few BUILT-IN OPERATIONS (no search, sort, remove-by-value).
* - No POLYMORPHISM across "list-like things".
* <p>
*
* The Collections Framework gives you:
* - GROWABLE containers that resize automatically.
* - Many SHAPES (List, Set, Queue, Map, Deque) for different access patterns.
* - A RICH METHOD SET (add, remove, contains, sort, filter, ...).
* - INTERFACES so you can swap implementations without changing callers.
* - INTEROPERABILITY with streams, lambdas, parallel processing, and the
* rest of java.util.
* <p>
*
* The Hierarchy in One Picture
* ----------------------------
* <p>
*
* Iterable<E>
* |
* v
* Collection<E> ---------------------------------+
* | |
* +---- List<E> +---- Map<K,V> (NOT a Collection - separate root)
* | |-- ArrayList |
* | |-- LinkedList |-- HashMap
* | |-- Vector (legacy) |-- LinkedHashMap
* | |-- CopyOnWriteArrayList |-- TreeMap
* | |-- ConcurrentHashMap
* +---- Set<E> |-- Hashtable (legacy)
* | |-- HashSet
* | | `-- LinkedHashSet
* | |-- TreeSet (also SortedSet, NavigableSet)
* | |-- EnumSet
* | |-- CopyOnWriteArraySet
* |
* +---- Queue<E>
* | |-- PriorityQueue
* | |-- ArrayBlockingQueue, LinkedBlockingQueue (concurrent)
* | `-- Deque<E>
* | |-- ArrayDeque
* | |-- LinkedList (yes, also a List!)
* | `-- BlockingDeque (concurrent)
* <p>
*
* NOTE: Map is NOT a sub-type of Collection. A Map is a separate root
* because its operations work on KEY/VALUE PAIRS, not on single elements.
* <p>
*
* Three "Buckets" of Implementations
* ----------------------------------
* 1. ORDER-PRESERVING (List, Queue, Deque) - keep insertion order or a
* meaningful "first / last".
* 2. UNIQUENESS (Set) - no duplicates, no positional access by default.
* 3. KEY -> VALUE (Map) - look up a value by a key.
* <p>
*
* Picking The Right Implementation (90% Of Real-World Use)
* --------------------------------------------------------
* List of stuff, mostly read -> ArrayList
* Frequent insert/remove in the middle -> LinkedList (rare in practice)
* Need uniqueness -> HashSet
* Uniqueness + sorted order -> TreeSet
* Uniqueness + insertion order -> LinkedHashSet
* Key/value lookup -> HashMap
* Key/value + insertion order -> LinkedHashMap
* Key/value + sorted by key -> TreeMap
* FIFO queue -> ArrayDeque
* Priority queue -> PriorityQueue
* Stack -> ArrayDeque (push / pop / peek)
* <p>
*
* What This Folder Covers
* -----------------------
* CollectionsIntroduction.java (this file)
* CollectionInterface.java - methods on the root Collection<E>
* CollectionsClass.java - java.util.Collections static helpers
* ListInterface.java + ArrayListDemo / LinkedListDemo
* SetInterface.java + HashSet / LinkedHashSet / TreeSet
* QueueInterface.java + PriorityQueueDemo
* DequeInterface.java
* MapInterface.java + HashMap / LinkedHashMap / TreeMap
* IteratorDemo.java
* ComparatorComparable.java
* ModernCollections.java - Java 8-21 (factory methods,
* Stream collectors, Sequenced
* collections)
* FaceDetectionApp/ - end-to-end project using many
* collection types together.
*/
public class CollectionsIntroduction {
public static void main(String[] args) {
section("1) Polymorphism through the interface hierarchy");
// ALL of these implement the Collection<E> contract, so we can use
// the same code path on any of them.
java.util.Collection<String> list = new java.util.ArrayList<>(java.util.List.of("a", "b", "c"));
java.util.Collection<String> set = new java.util.HashSet<>(java.util.List.of("a", "b", "c", "a"));
java.util.Collection<String> queue = new java.util.ArrayDeque<>(java.util.List.of("a", "b", "c"));
printSize("ArrayList ", list);
printSize("HashSet ", set); // duplicates collapsed
printSize("ArrayDeque", queue);
section("2) The Map root - separate API for key/value");
java.util.Map<String, Integer> ages = new java.util.HashMap<>();
ages.put("Alice", 30);
ages.put("Bob", 25);
System.out.println("ages = " + ages);
System.out.println("ages.get(\"Alice\") = " + ages.get("Alice"));
section("3) Same data, different collection type");
java.util.List<Integer> input = java.util.List.of(3, 1, 4, 1, 5, 9, 2, 6, 5, 3);
System.out.println("input = " + input);
System.out.println("as HashSet = " + new java.util.HashSet<>(input));
System.out.println("as LinkedHashSet = " + new java.util.LinkedHashSet<>(input));
System.out.println("as TreeSet (sorted) = " + new java.util.TreeSet<>(input));
section("4) Big-O cheatsheet (memorise once)");
System.out.println("""
Operation ArrayList LinkedList HashSet TreeSet HashMap
add at end O(1)* O(1) O(1)* O(log n) O(1)*
add at index O(n) O(n) n/a n/a n/a
get(index) O(1) O(n) n/a n/a n/a (get(key))
contains(x) O(n) O(n) O(1)* O(log n) n/a (containsKey)
remove(x) O(n) O(n) O(1)* O(log n) O(1)*
* amortised, assumes good hash distribution where applicable
""");
// OUTPUT (representative)
}
private static void printSize(String label, java.util.Collection<?> c) {
System.out.println(label + " size=" + c.size() + " contents=" + c);
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}