Skip to content

Instantly share code, notes, and snippets.

@SiAust
Created September 22, 2020 08:38
Show Gist options
  • Save SiAust/255884b8686d7dd6e6336f68cff7ca30 to your computer and use it in GitHub Desktop.
Save SiAust/255884b8686d7dd6e6336f68cff7ca30 to your computer and use it in GitHub Desktop.
A functional stream example with Comparator, BiConsumer and Optional
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