-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionsHelper.java
More file actions
175 lines (149 loc) · 6.42 KB
/
CollectionsHelper.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
package playground;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.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
*/
public class CollectionsHelper {
// 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.
*/
public static final <T> Collection<T> buildCollectionWithDuplicates(
final boolean isRandomAccessByPosition, // 1
final boolean isForContainsChecks, // 2
final boolean isForInsertOrRemoveAtStart, // 3
final boolean isForQueue, // 4
final boolean isSupportsNulls, // 5
final int capacityHint // 6
) {
// --- CAN HAVE DUPLICATES ---
// Cannot be a SET.
// https://stackoverflow.com/questions/322715/when-to-use-linkedlist-over-arraylist-in-java
if (isRandomAccessByPosition || isForContainsChecks) {
return new ArrayList<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
return new ArrayDeque<T>(capacityHint);
}
return new LinkedList<T>(); // Consumes a lot of memory.
}
return new ArrayList<T>(capacityHint);
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
public static final <T> Collection<T> buildCollectionWithDuplicatesWithThreads(
final boolean isForMoreReadsThanWrites, // 1
final int capacityHint // 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.
return new CopyOnWriteArrayList<T>(); // COWAL
}
// Iterating must be synchronized.
return Collections.synchronizedList(new ArrayList<T>(capacityHint));
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
public static final <T> Collection<T> buildCollectionWithoutDuplicates(
final boolean isPreserveInsertionOrder, // 1
final boolean isForSortWithComparator, // 2
final int capacityHint // 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) {
throw new AssertionError("Preserving insertion order and sorting by comparator is not compatible !");
}
if (isPreserveInsertionOrder) {
return new LinkedHashSet<T>(capacityHint);
}
if (isForSortWithComparator) {
return new TreeSet<T>();
}
return new HashSet<T>(capacityHint);
}
/**
* NOTE: this should only be used to guide your choices, always read the Java documentation of the class of the returned instance.
*/
public static final <T> Collection<T> buildCollectionWithoutDuplicatesWithThreads(
final boolean isForMoreReadsThanWrites, // 1
final boolean isForSortWithComparator // 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) {
return new ConcurrentSkipListSet<T>();
}
if (isForMoreReadsThanWrites) {
return new CopyOnWriteArraySet<T>();
}
return Collections.synchronizedSet(new HashSet<T>());
}
public static void main(final String[] args) {
// Some examples:
// --- WITH DUPLICATES.
{
final Collection<String> collection = buildCollectionWithDuplicates(true, false, true, true, true, 16);
System.out.println(collection instanceof ArrayList<?>);
System.out.println(collection.getClass().getName());
}
{
final Collection<String> collection = buildCollectionWithDuplicatesWithThreads(true, 16);
System.out.println(collection instanceof CopyOnWriteArrayList<?>);
System.out.println(collection.getClass().getName());
}
{
final Collection<String> collection = buildCollectionWithDuplicates(false, false, true, true, true, 16);
System.out.println(collection instanceof LinkedList<?>);
System.out.println(collection.getClass().getName());
}
// --- WITHOUT DUPLICATES.
{
final Collection<String> collection = buildCollectionWithoutDuplicates(true, false, 16);
System.out.println(collection instanceof LinkedHashSet<?>);
System.out.println(collection.getClass().getName());
}
{
final Collection<String> collection = buildCollectionWithoutDuplicates(false, true, 16);
System.out.println(collection instanceof TreeSet<?>);
System.out.println(collection.getClass().getName());
}
{
final Collection<String> collection = buildCollectionWithoutDuplicatesWithThreads(true, false);
System.out.println(collection instanceof CopyOnWriteArraySet<?>);
System.out.println(collection.getClass().getName());
}
}
}