-
Notifications
You must be signed in to change notification settings - Fork 0
/
PrimeNumbersCollector.java
52 lines (44 loc) · 1.21 KB
/
PrimeNumbersCollector.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
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
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;
public class PrimeNumbersCollector<T extends Integer> implements Collector<T, List<T>, List<T>> {
/*
* public PrimeNumbersCollector() { }
*/
@Override
public Supplier<List<T>> supplier() {
return LinkedList<T>::new;
}
@Override
public BiConsumer<List<T>, T> accumulator() {
return List<T>::add;
}
@Override
public BinaryOperator<List<T>> combiner() {
return ( left, right )->{ left.addAll(right); return left; };
}
@Override
public Function<List<T>, List<T>> finisher() {
return ( numbers ) -> {
final int size=Mathematics.getMaximum(numbers).intValue();
List<T> primes=Mathematics.getPrimes(size);
List<T> result=new LinkedList<T>();
for(T number:numbers) {
if(primes.contains(number)) {
result.add(number);
}
}
return result;
};
}
@Override
public Set<Characteristics> characteristics() {
return Collections.emptySet();
}
}