Created
September 22, 2020 08:38
-
-
Save SiAust/255884b8686d7dd6e6336f68cff7ca30 to your computer and use it in GitHub Desktop.
A functional stream example with Comparator, BiConsumer and Optional
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.function.*; | |
import java.util.stream.*; | |
class MinMax { | |
public static <T> void findMinMax( | |
Stream<? extends T> stream, | |
Comparator<? super T> order, | |
BiConsumer<? super T, ? super T> minMaxConsumer) { | |
List<T> list = stream.collect(Collectors.toList()); | |
minMaxConsumer.accept(list.stream().min(order).orElse(null), | |
list.stream().max(order).orElse(null)); | |
} | |
public static void main(String[] args) { | |
Stream<Integer> intStream = Stream.of(2,3,41,3,7,4,32,5); | |
Comparator<Integer> comparator = Comparator.naturalOrder(); | |
BiConsumer<Integer, Integer> minMaxConsumer = (a, b) -> System.out.printf("Min:%d Max:%d", a, b); | |
findMinMax(intStream, comparator, minMaxConsumer); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment