StreamToLoop: min/max: Comparators as lambdas/method references supported; Comparator.naturalOrder() and reverseOrder() inlined.

This commit is contained in:
Tagir Valeev
2016-12-19 16:35:41 +07:00
parent a853ec4635
commit 204a045f0a
9 changed files with 94 additions and 35 deletions
@@ -0,0 +1,17 @@
// "Replace Stream API chain with loop" "true"
import java.util.*;
public class Main {
private static Optional<String> max(Map<String, List<String>> dependencies, String fruits, Map<String, String> weights) {
boolean seen = false;
String best = null;
for (String s : dependencies.get(fruits)) {
if (!seen || weights.get(s).compareTo(weights.get(best)) > 0) {
seen = true;
best = s;
}
}
return seen ? Optional.of(best) : Optional.empty();
}
}