/
JavaTransformTest.java
110 lines (92 loc) · 5.1 KB
/
JavaTransformTest.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
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import com.gs.collections.api.block.predicate.Predicate;
import com.gs.collections.api.block.procedure.Procedure;
import com.gs.collections.api.collection.MutableCollection;
import com.gs.collections.api.list.MutableList;
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.List;
import java.util.stream.Collectors;
/**
* Transform collection (sort, delete, transtorm elements and so on)
*
*
*
* Created by vvedenin on 10/27/2015.
*/
public class JavaTransformTest {
// sorting
private static void sortTest() {
List<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> iterable = jdk;
MutableList<String> gs = FastList.newList(jdk);
// sorting
Collections.sort(jdk); // using jdk
List<String> guava = Ordering.natural().sortedCopy(iterable); // using guava
gs.sortThis(); // using gs
System.out.println("sort = " + jdk + ":" + guava + ":" + gs); // print sort = [a1, a1, a2, a3]:[a1, a1, a2, a3]:[a1, a1, a2, a3]
}
// remove if
private static void removeTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// remove if
jdk.removeIf((s) -> s.contains("1")); // using jdk
Iterables.removeIf(guava, (s) -> s.contains("1")); // using guava
CollectionUtils.filter(apache, (s) -> !s.contains("1")); // using apache
gs.removeIf((Predicate<String>) (s) -> s.contains("1")); // using gs
System.out.println("removeIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print removeIf = [a2, a3]:[a2, a3]:[a2, a3]:[a2, a3]
}
// remove if not
private static void retainTest() {
Collection<String> jdk = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> guava = Lists.newArrayList(jdk);
Iterable<String> apache = Lists.newArrayList(jdk);
MutableCollection<String> gs = FastList.newList(jdk);
// remove if not
jdk.removeIf((s) -> !s.contains("1")); // using jdk
Iterables.removeIf(guava, (s) -> !s.contains("1")); // using guava
CollectionUtils.filter(apache, (s) -> s.contains("1")); // using apache
gs.removeIf((Predicate<String>) (s) -> !s.contains("1")); //using gs
System.out.println("retainIf = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print retainIf = [a1, a1]:[a1, a1]:[a1, a1]:[a1, a1]
}
// Transform all elements
private static void transformTest() {
Collection<String> collection = Lists.newArrayList("a1", "a2", "a3", "a1");
Iterable<String> iterable = collection;
Collection<String> apache = Lists.newArrayList(collection);
MutableCollection<String> mutableCollection = FastList.newList(collection);
// Transform all elements
List<String> jdk = collection.stream().map((s) -> s + "_1").collect(Collectors.toList()); // using jdk
Iterable<String> guava = Iterables.transform(iterable, (s) -> s + "_1"); // using guava
CollectionUtils.transform(apache, (s) -> s + "_1"); // using apache
MutableCollection<String> gs = mutableCollection.collect((s) -> s + "_1"); // using gs
System.out.println("transform = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print transform = [a1_1, a2_1, a3_1, a1_1]:[a1_1, a2_1, a3_1, a1_1]:[a1_1, a2_1, a3_1, a1_1]:[a1_1, a2_1, a3_1, a1_1]
}
// Change property of all elements
private static void changeAllTest() {
Collection<StringBuilder> jdk = Lists.newArrayList(new StringBuilder("a1"), new StringBuilder("a2"), new StringBuilder("a3"));
Iterable<StringBuilder> iterable = Lists.newArrayList(new StringBuilder("a1"), new StringBuilder("a2"), new StringBuilder("a3"));;
Collection<StringBuilder> apache = Lists.newArrayList(new StringBuilder("a1"), new StringBuilder("a2"), new StringBuilder("a3"));
MutableCollection<StringBuilder> gs = FastList.newListWith(new StringBuilder("a1"), new StringBuilder("a2"), new StringBuilder("a3"));
// Change property of all elements
jdk.stream().forEach((s) -> s.append("_1")); // using jdk
Iterable<StringBuilder> guava = Iterables.transform(iterable, (s) -> s.append("_1")); // using guava
CollectionUtils.transform(apache, (s) -> s.append("_1")); // using apache
gs.forEach((Procedure<StringBuilder>) (s) -> s.append("_1")); // using gs
System.out.println("change = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print changeAll = [a1_1, a2_1, a3_1]:[a1_1, a2_1, a3_1]:[a1_1, a2_1, a3_1]:[a1_1, a2_1, a3_1]
}
public static void main(String[] args) {
sortTest();
removeTest();
retainTest();
transformTest();
changeAllTest();
}
}