-
Notifications
You must be signed in to change notification settings - Fork 203
Expand file tree
/
Copy pathTask03Main.java
More file actions
46 lines (38 loc) · 1.24 KB
/
Task03Main.java
File metadata and controls
46 lines (38 loc) · 1.24 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
package com.example.task03;
import java.util.Comparator;
import java.util.Iterator;
import java.util.function.BiConsumer;
import java.util.stream.Stream;
public class Task03Main {
public static void main(String[] args) {
findMinMax(
Stream.of(2, 9, 5, 4, 8, 1, 3),
Integer::compareTo,
(min, max) ->
System.out.println("min: " + min + " / max: " + max)
);
}
public static <T> void findMinMax(
Stream<? extends T> stream,
Comparator<? super T> order,
BiConsumer<? super T, ? super T> minMaxConsumer) {
Iterator<? extends T> iterator = stream.iterator();
if(!iterator.hasNext()){
minMaxConsumer.accept(null, null);
}
else{
T min = iterator.next();
T max = min;
while(iterator.hasNext()){
T currentElem = iterator.next();
if(order.compare(currentElem, min) < 0){
min = currentElem;
}
else if (order.compare(currentElem, max) > 0) {
max = currentElem;
}
}
minMaxConsumer.accept(min, max);
}
}
}