-
Notifications
You must be signed in to change notification settings - Fork 47
/
CollectionSearchTests.java
189 lines (159 loc) · 8.53 KB
/
CollectionSearchTests.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package com.github.vedenin.eng.collections.utils;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.list.MutableList;
import com.gs.collections.api.ordered.OrderedIterable;
import com.gs.collections.impl.list.mutable.FastList;
import org.apache.commons.collections4.CollectionUtils;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;
/**
* Testing search method using guava, apache, jdk, gs
*
* Attention: version with russian comments in "rus" package
*
* Created by vedenin on 17 .10.15.
*/
public class CollectionSearchTests {
// Get frequency element in collection
private static void testCount() {
Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> iterable = collection;
MutableCollection<String> collectionGS = FastList.newListWith("a1", "a2", "a3", "a1");
// Get frequency element in collection
int i1 = Iterables.frequency(iterable, "a1"); // using guava
int i2 = Collections.frequency(collection, "a1"); // using JDK
int i3 = CollectionUtils.cardinality("a1", iterable); // using Apache
int i4 = collectionGS.count("a1"::equals);
long i5 = collection.stream().filter("a1"::equals).count(); // using stream JDK
System.out.println("count = " + i1 + ":" + i2 + ":" + i3 + ":" + i4 + ":" + i5); // print count = 2:2:2:2:2
}
// get first element
private static void testGetFirst() {
Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
Iterable<String> iterable = collection;
// get first element
Iterator<String> iterator = collection.iterator(); // using JDK
String jdk = iterator.hasNext() ? iterator.next(): "1";
String guava = Iterables.getFirst(iterable, "1"); // using guava
String apache = CollectionUtils.get(iterable, 0); // using Apache
String gs = orderedIterable.getFirst(); // using GS
String stream = collection.stream().findFirst().orElse("1"); // using Stream API
System.out.println("first = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // print first = a1:a1:a1:a1:a1
}
// get last element
private static void testGetLast() {
Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a8");
OrderedIterable<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a8");
Iterable<String> iterable = collection;
// get last element
Iterator<String> iterator = collection.iterator(); // using JDK
String jdk = "1";
while(iterator.hasNext()) {
jdk = iterator.next();
}
String guava = Iterables.getLast(iterable, "1"); // using guava
String apache = CollectionUtils.get(collection, collection.size()-1); // using Apache
String gs = orderedIterable.getLast(); // using GS
String stream = collection.stream().skip(collection.size()-1).findFirst().orElse("1"); // using Stream API
System.out.println("last = " + jdk + ":" + guava + ":" + apache + ":" + gs + ":" + stream); // print last = a8:a8:a8:a8:a8
}
// get a element, if collection has only one element
private static void testGetSingle() {
Collection<String> collection = Lists.newArrayList("a3");
OrderedIterable<String> orderedIterable = FastList.newListWith("a3");
Iterable<String> iterable = collection;
// get a element, if collection has only one element
String guava = Iterables.getOnlyElement(iterable); // using guava
String jdk = collection.iterator().next(); // using JDK
String apache = CollectionUtils.extractSingleton(collection); // using Apache
assert(orderedIterable.size() != 1);// using GS
String gs = orderedIterable.getFirst();
System.out.println("single = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print single = a3:a3:a3:a3
}
// get max element
private static void testGetMax() {
Collection<String> collection = Lists.newArrayList("5", "1", "3", "8", "4");
OrderedIterable<String> orderedIterable = FastList.newListWith("5", "1", "3", "8", "4");
Iterable<String> iterable = collection;
// get max element
String jdk = Collections.max(collection); // using JDK
String gs = orderedIterable.max(); // using GS
String guava = Ordering.natural().max(iterable); // using guava
System.out.println("max = " + jdk + ":" + guava + ":" + gs); // print max = 8:8:8
}
// get min element
private static void testGetMin() {
Collection<String> collection = Lists.newArrayList("5", "1", "3", "8", "4");
OrderedIterable<String> orderedIterable = FastList.newListWith("5", "1", "3", "8", "4");
Iterable<String> iterable = collection;
// get min element
String jdk = Collections.min(collection); // using JDK
String gs = orderedIterable.min(); // using GS
String guava = Ordering.natural().min(iterable); // using guava
System.out.println("min = " + jdk + ":" + guava + ":" + gs); // print min = 1:1:1
}
// find element in sorted list
private static void testBinarySearch() {
List<String> list = Lists.newArrayList("2", "4", "13", "31", "43");
MutableList<String> mutableList = FastList.newListWith("2", "4","13", "31", "43");
// find element in sorted list
int jdk = Collections.binarySearch(list, "13");
int guava = Ordering.natural().binarySearch(list, "13");
int gs = mutableList.binarySearch("13");
System.out.println("find = " + jdk + ":" + guava + ":" + gs); // print find = 2:2:2
}
// find element in unsorted collection
private static void testSearch() {
Collection<String> collection = Lists.newArrayList("2", "14", "3", "13", "43");
MutableList<String> mutableList = FastList.newListWith("2", "14", "3", "13", "43");
Iterable<String> iterable = collection;
// find element in unsorted collection
String jdk = collection.stream().filter("13"::equals).findFirst().get();
String guava = Iterables.find(iterable, "13"::equals);
String apache = CollectionUtils.find(iterable, "13"::equals);
String gs = mutableList.select("13"::equals).get(0);
System.out.println("find = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print find = 13:13:13:13
}
// get third element
private static void testGetByIndex() {
Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
MutableCollection<String> orderedIterable = FastList.newListWith("a1", "a2", "a3", "a1");
Iterable<String> iterable = collection;
// get third element
String jdk = collection.stream().skip(2).findFirst().get(); // using JDK
String guava = Iterables.get(iterable, 2); // using guava
String apache = CollectionUtils.get(iterable, 2); // using Apache
System.out.println("third = " + jdk + ":" + guava + ":" + apache); // print third = a3:a3:a3
}
// find all elements using pattern
private static void testSelect() {
Collection<String> collection = Lists.newArrayList("2", "14", "3", "13", "43");
MutableCollection<String> mutableCollection = FastList.newListWith("2", "14", "3", "13", "43");
Iterable<String> iterable = collection;
// find all elements using pattern
List<String> jdk = collection.stream().filter((s) -> s.contains("1")).collect(Collectors.toList()); // using JDK
Iterable<String> guava = Iterables.filter(iterable, (s) -> s.contains("1")); // using guava
Collection<String> apache = CollectionUtils.select(iterable, (s) -> s.contains("1")); // using Apache
MutableCollection<String> gs = mutableCollection.select((s) -> s.contains("1")); // using GS
System.out.println("select = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print select = [14, 13]:[14, 13]:[14, 13]:[14, 13]
}
public static void main(String[] args) {
testCount();
testGetFirst();
testGetLast();
testGetSingle();
testGetMax();
testGetMin();
testBinarySearch();
testSearch();
testGetByIndex();
testSelect();
}
}