-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathSample2.java
172 lines (145 loc) · 5.87 KB
/
Sample2.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.Random;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Sample2 {
public static final Comparator<Boolean> BOOLEAN_REVERSED_COMPARATOR = (o1, o2) -> o1 == o2 ? 0 : !o1 ? 1 : -1;
static final Random random = new Random();
public static void main(String[] args) {
var customers = generateSampleDatas();
var customersByNameContainsA = customers.stream()
.collect(Collectors.groupingBy(c -> c.getName().contains("A")));
System.out.println(customersByNameContainsA);
var uniqueCustomersByNameContainsA = customers.stream()
.collect(Collectors.groupingBy(c -> c.getName().contains("A"), Collectors.toSet()));
System.out.println(uniqueCustomersByNameContainsA);
var uniqueBirthdayByNameContainsA = customers.stream()
.collect(Collectors.groupingBy(c -> c.getName().contains("A"),
Collectors.mapping(Customer::getBirthday, Collectors.toSet())));
System.out.println(uniqueBirthdayByNameContainsA);
var customersByReversedOrderNameA = customers.stream().collect(Collectors.groupingBy(
c -> c.getName().contains("A"),
() -> new TreeMap<>(BOOLEAN_REVERSED_COMPARATOR),
Collectors.toList()));
System.out.println(customersByReversedOrderNameA);
System.out.println(samplingCustomer(customers));
System.out.println(samplingCustomer(customers));
System.out.println(samplingCustomer(customers));
System.out.println(samplingCustomer(customers));
System.out.println(customers.stream().collect(new Sampling<>(3)));
System.out.println(Stream.of(12, 3, 4, 5, 6, 7, 434, 2234, 4).collect(new Sampling<>(5)));
}
private static Optional<Customer> samplingCustomer(List<Customer> customers) {
return customers.stream().reduce((c1, c2) -> random.nextBoolean() ? c1 : c2);
}
private static List<Customer> generateSampleDatas() {
return List.of(
Customer.generate("Aiden Ramirez"),
Customer.generate("Abraham Weston"),
Customer.generate("Amiee Maynard"),
Customer.generate("Walid Phillips"),
Customer.generate("Jesus Donnelly"),
Customer.generate("Sallie Ahmed"),
Customer.generate("Zarah Rowland"),
Customer.generate("Omari Gregory"),
Customer.generate("Amanah Bullock"),
Customer.generate("Lola Key"),
Customer.generate("Issac Rice"),
Customer.generate("Kieran Morris"),
Customer.generate("Tracey Hendrix"),
Customer.generate("Menaal Ramos"),
Customer.generate("Amy-Leigh Hilton"),
Customer.generate("Graeme Leech"),
Customer.generate("Jose Rogers"),
Customer.generate("Andre Cote"),
Customer.generate("Ira Mccarthy"),
Customer.generate("Izabel Sanders"),
Customer.generate("Amy Davie"),
Customer.generate("Cruz Head"),
Customer.generate("Carmen Sampson"),
Customer.generate("Spike Peel"),
Customer.generate("Clinton Harrington"),
Customer.generate("Sahil Jaramillo"),
Customer.generate("Nial Plummer"),
Customer.generate("Tyla Frost"),
Customer.generate("Aoife Mccallum"),
Customer.generate("Kean Mason"),
Customer.generate("Lyndsey Beech"),
Customer.generate("Evalyn Ford"),
Customer.generate("Ayah Stevenson"),
Customer.generate("Dalia Wilkins"),
Customer.generate("Matthew Villalobos"),
Customer.generate("Jerry Owens"),
Customer.generate("Kaja Livingston"),
Customer.generate("Kenan Alvarado"),
Customer.generate("Sonya Legge"),
Customer.generate("Rees Armitage"),
Customer.generate("Ajay Compton"),
Customer.generate("Olaf Adamson"),
Customer.generate("Nafeesa Roberson"),
Customer.generate("Rania Dickerson"),
Customer.generate("Jeffery Baird"),
Customer.generate("Clayton Aguirre"),
Customer.generate("Jaydon Summers"),
Customer.generate("Chance Medina"),
Customer.generate("Owen Kouma"),
Customer.generate("Sania Marriott"));
}
static class Sampling<T> implements Collector<T, ArrayList<Sampling.SamplingData<T>>, List<T>> {
final int count;
Sampling(int count) {
this.count = count;
}
@Override public Supplier<ArrayList<SamplingData<T>>> supplier() {
return ArrayList::new;
}
@Override public BiConsumer<ArrayList<SamplingData<T>>, T> accumulator() {
return (list, data) -> list.add(new SamplingData<>(random.nextInt(), data));
}
@Override public BinaryOperator<ArrayList<SamplingData<T>>> combiner() {
return (l1, l2) -> {
l1.addAll(l2);
return l1;
};
}
@Override public Function<ArrayList<SamplingData<T>>, List<T>> finisher() {
return sources -> sources.stream()
.sorted()
.limit(count)
.map(SamplingData::getData)
.collect(Collectors.toList());
}
@Override public Set<Characteristics> characteristics() {
return Collections.unmodifiableSet(EnumSet.of(Characteristics.UNORDERED));
}
static class SamplingData<T> implements Comparable<SamplingData<T>> {
Integer token;
T data;
public SamplingData(Integer token, T data) {
this.token = token;
this.data = data;
}
@Override public int compareTo(SamplingData that) {
return this.token.compareTo(that.token);
}
public Integer getToken() {
return token;
}
public T getData() {
return data;
}
}
}
}