- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionsHelper.java
More file actions
Latest commit
175 lines (149 loc) · 6.42 KB
/
Copy pathCollectionsHelper.java
File metadata and controls
175 lines (149 loc) · 6.42 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
packageplayground;
importjava.util.ArrayDeque;
importjava.util.ArrayList;
importjava.util.Collection;
importjava.util.Collections;
importjava.util.HashSet;
importjava.util.LinkedHashSet;
importjava.util.LinkedList;
importjava.util.TreeSet;
importjava.util.concurrent.ConcurrentSkipListSet;
importjava.util.concurrent.CopyOnWriteArrayList;
importjava.util.concurrent.CopyOnWriteArraySet;
/**
* Helps determine which collection to use.
*
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*
* This helper only covers some of the common List and Set collections.
*
* @author Christophe Roussy
*/
publicclassCollectionsHelper {
// https://www.geeksforgeeks.org/difference-between-linkedlist-and-linkedhashset-in-java/
// https://javatutorial.net/choose-the-right-java-collection
// http://anh.cs.luc.edu/363/notes/06A_Amortizing.html
// https://www.developer.com/java/article.php/10922_3829891_2/Selecting-the-Best-Java-Collection-Class-for-Your-Application.htm
// https://stackoverflow.com/questions/21974361/which-java-collection-should-i-use
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
publicstaticfinal <T> Collection<T> buildCollectionWithDuplicates(
finalbooleanisRandomAccessByPosition, // 1
finalbooleanisForContainsChecks, // 2
finalbooleanisForInsertOrRemoveAtStart, // 3
finalbooleanisForQueue, // 4
finalbooleanisSupportsNulls, // 5
finalintcapacityHint// 6
) {
// --- CAN HAVE DUPLICATES ---
// Cannot be a SET.
// https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java
if (isRandomAccessByPosition || isForContainsChecks) {
returnnewArrayList<T>(capacityHint);
}
if (isForInsertOrRemoveAtStart) {
if (isForQueue && !isSupportsNulls) {
// https://docs.oracle.com/javase/6/docs/api/java/util/ArrayDeque.html
// https://stackoverflow.com/questions/6163166/why-is-arraydeque-better-than-linkedlist
returnnewArrayDeque<T>(capacityHint);
}
returnnewLinkedList<T>(); // Consumes a lot of memory.
}
returnnewArrayList<T>(capacityHint);
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
publicstaticfinal <T> Collection<T> buildCollectionWithDuplicatesWithThreads(
finalbooleanisForMoreReadsThanWrites, // 1
finalintcapacityHint// 2
) {
// --- CAN HAVE DUPLICATES ---
// Cannot be a SET.
// https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java
// https://stackoverflow.com/questions/28979488/difference-between-copyonwritearraylist-and-synchronizedlist
if (isForMoreReadsThanWrites) {
// COWAL does not throw ConcurrentModificationException, good for reads, bad for writes.
returnnewCopyOnWriteArrayList<T>(); // COWAL
}
// Iterating must be synchronized.
returnCollections.synchronizedList(newArrayList<T>(capacityHint));
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
publicstaticfinal <T> Collection<T> buildCollectionWithoutDuplicates(
finalbooleanisPreserveInsertionOrder, // 1
finalbooleanisForSortWithComparator, // 2
finalintcapacityHint// 3
) {
// --- NO DUPLICATES ---
// Can be a SET.
// https://stackoverflow.com/questions/20116660/hashset-vs-treeset-vs-linkedhashset-on-basis-of-adding-duplicate-value
if (isPreserveInsertionOrder && isForSortWithComparator) {
thrownewAssertionError("Preserving insertion order and sorting by comparator is not compatible !");
}
if (isPreserveInsertionOrder) {
returnnewLinkedHashSet<T>(capacityHint);
}
if (isForSortWithComparator) {
returnnewTreeSet<T>();
}
returnnewHashSet<T>(capacityHint);
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
publicstaticfinal <T> Collection<T> buildCollectionWithoutDuplicatesWithThreads(
finalbooleanisForMoreReadsThanWrites, // 1
finalbooleanisForSortWithComparator// 2
) {
// --- NO DUPLICATES ---
// Can be a SET.
// https://stackoverflow.com/questions/29249714/when-is-copyonwritearrayset-useful-to-achieve-thread-safe-hashset
// https://stackoverflow.com/questions/6720396/different-types-of-thread-safe-sets-in-java
if (isForSortWithComparator) {
returnnewConcurrentSkipListSet<T>();
}
if (isForMoreReadsThanWrites) {
returnnewCopyOnWriteArraySet<T>();
}
returnCollections.synchronizedSet(newHashSet<T>());
}
publicstaticvoidmain(finalString[] args) {
// Some examples:
// --- WITH DUPLICATES.
{
finalCollection<String> collection = buildCollectionWithDuplicates(true, false, true, true, true, 16);
System.out.println(collectioninstanceofArrayList<?>);
System.out.println(collection.getClass().getName());
}
{
finalCollection<String> collection = buildCollectionWithDuplicatesWithThreads(true, 16);
System.out.println(collectioninstanceofCopyOnWriteArrayList<?>);
System.out.println(collection.getClass().getName());
}
{
finalCollection<String> collection = buildCollectionWithDuplicates(false, false, true, true, true, 16);
System.out.println(collectioninstanceofLinkedList<?>);
System.out.println(collection.getClass().getName());
}
// --- WITHOUT DUPLICATES.
{
finalCollection<String> collection = buildCollectionWithoutDuplicates(true, false, 16);
System.out.println(collectioninstanceofLinkedHashSet<?>);
System.out.println(collection.getClass().getName());
}
{
finalCollection<String> collection = buildCollectionWithoutDuplicates(false, true, 16);
System.out.println(collectioninstanceofTreeSet<?>);
System.out.println(collection.getClass().getName());
}
{
finalCollection<String> collection = buildCollectionWithoutDuplicatesWithThreads(true, false);
System.out.println(collectioninstanceofCopyOnWriteArraySet<?>);
System.out.println(collection.getClass().getName());
}
}
}