Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
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) {

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) {
BiConsumer<? super T, ? super T> minMaxConsumer)
{

// your implementation here
Iterator iterator = stream.iterator();

if (!iterator.hasNext())
{
minMaxConsumer.accept(null, null);
return;
}

T e = (T)iterator.next();
T min = e;
T max = e;

while (iterator.hasNext())
{
e = (T)iterator.next();

if(order.compare(e, min) < 0)
min = e;

if(order.compare(e, max) > 0)
max = e;
}

minMaxConsumer.accept(min, max);
}
}