import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
public final class Main {
public static void main
(String[] args
) { Predicate<String> p = s -> s.startsWith("b");
findFirst(p);
limitList(p);
}
public static void findFirst(Predicate<String> matchesFavorite) {
var firstFavorite
= List.
of("foo",
"bar",
"baz") .stream()
.filter(matchesFavorite)
.findFirst();
firstFavorite.
ifPresent(s
-> System.
out.
println("A:" + s
));
// As a result, it is equivalent to the following code:
List.
of("foo",
"bar",
"baz") .stream()
.filter(matchesFavorite)
.findFirst()
.
ifPresent(s
-> System.
out.
println("B:" + s
)); }
public static void limitList(Predicate<String> matchesFavorite) {
var favoriteList
= List.
of("foo",
"bar",
"baz") .stream()
.filter(matchesFavorite)
.limit(1)
.collect(Collectors.toList());
favoriteList.
forEach(s
-> System.
out.
println("C:" + s
));
// Or
for (var s : favoriteList) {
// This loop only runs once at most.
}
// Or again, it could be as follows, but...
if (favoriteList.size() != 0) {
var s = favoriteList.get(0);
}
// As a result, it is equivalent to the following code:
List.
of("foo",
"bar",
"baz") .stream()
.filter(matchesFavorite)
.limit(1)
.
forEach(s
-> System.
out.
println("F:" + s
)); }
}
aW1wb3J0IGphdmEudXRpbC5MaXN0OwppbXBvcnQgamF2YS51dGlsLmZ1bmN0aW9uLlByZWRpY2F0ZTsKaW1wb3J0IGphdmEudXRpbC5zdHJlYW0uQ29sbGVjdG9yczsKCnB1YmxpYyBmaW5hbCBjbGFzcyBNYWluIHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgUHJlZGljYXRlPFN0cmluZz4gcCA9IHMgLT4gcy5zdGFydHNXaXRoKCJiIik7CiAgICAgICAgZmluZEZpcnN0KHApOwogICAgICAgIGxpbWl0TGlzdChwKTsKICAgIH0KCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgZmluZEZpcnN0KFByZWRpY2F0ZTxTdHJpbmc+IG1hdGNoZXNGYXZvcml0ZSkgewogICAgICAgIHZhciBmaXJzdEZhdm9yaXRlID0gTGlzdC5vZigiZm9vIiwgImJhciIsICJiYXoiKQogICAgICAgICAgICAuc3RyZWFtKCkKICAgICAgICAgICAgLmZpbHRlcihtYXRjaGVzRmF2b3JpdGUpCiAgICAgICAgICAgIC5maW5kRmlyc3QoKTsKICAgICAgICBmaXJzdEZhdm9yaXRlLmlmUHJlc2VudChzIC0+IFN5c3RlbS5vdXQucHJpbnRsbigiQToiICsgcykpOwoKICAgICAgICAvLyBBcyBhIHJlc3VsdCwgaXQgaXMgZXF1aXZhbGVudCB0byB0aGUgZm9sbG93aW5nIGNvZGU6CiAgICAgICAgTGlzdC5vZigiZm9vIiwgImJhciIsICJiYXoiKQogICAgICAgICAgICAuc3RyZWFtKCkKICAgICAgICAgICAgLmZpbHRlcihtYXRjaGVzRmF2b3JpdGUpCiAgICAgICAgICAgIC5maW5kRmlyc3QoKQogICAgICAgICAgICAuaWZQcmVzZW50KHMgLT4gU3lzdGVtLm91dC5wcmludGxuKCJCOiIgKyBzKSk7CiAgICB9CgogICAgcHVibGljIHN0YXRpYyB2b2lkIGxpbWl0TGlzdChQcmVkaWNhdGU8U3RyaW5nPiBtYXRjaGVzRmF2b3JpdGUpIHsKICAgICAgICB2YXIgZmF2b3JpdGVMaXN0ID0gTGlzdC5vZigiZm9vIiwgImJhciIsICJiYXoiKQogICAgICAgICAgICAuc3RyZWFtKCkKICAgICAgICAgICAgLmZpbHRlcihtYXRjaGVzRmF2b3JpdGUpCiAgICAgICAgICAgIC5saW1pdCgxKQogICAgICAgICAgICAuY29sbGVjdChDb2xsZWN0b3JzLnRvTGlzdCgpKTsKCiAgICAgICAgZmF2b3JpdGVMaXN0LmZvckVhY2gocyAtPiBTeXN0ZW0ub3V0LnByaW50bG4oIkM6IiArIHMpKTsKCiAgICAgICAgLy8gT3IKICAgICAgICBmb3IgKHZhciBzIDogZmF2b3JpdGVMaXN0KSB7CiAgICAgICAgICAgIC8vIFRoaXMgbG9vcCBvbmx5IHJ1bnMgb25jZSBhdCBtb3N0LgogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkQ6IiArIHMpOwogICAgICAgIH0KCiAgICAgICAgLy8gT3IgYWdhaW4sIGl0IGNvdWxkIGJlIGFzIGZvbGxvd3MsIGJ1dC4uLgogICAgICAgIGlmIChmYXZvcml0ZUxpc3Quc2l6ZSgpICE9IDApIHsKICAgICAgICAgICAgdmFyIHMgPSBmYXZvcml0ZUxpc3QuZ2V0KDApOwogICAgICAgICAgICBTeXN0ZW0ub3V0LnByaW50bG4oIkU6IiArIHMpOwogICAgICAgIH0KCiAgICAgICAgLy8gQXMgYSByZXN1bHQsIGl0IGlzIGVxdWl2YWxlbnQgdG8gdGhlIGZvbGxvd2luZyBjb2RlOgogICAgICAgIExpc3Qub2YoImZvbyIsICJiYXIiLCAiYmF6IikKICAgICAgICAgICAgLnN0cmVhbSgpCiAgICAgICAgICAgIC5maWx0ZXIobWF0Y2hlc0Zhdm9yaXRlKQogICAgICAgICAgICAubGltaXQoMSkKICAgICAgICAgICAgLmZvckVhY2gocyAtPiBTeXN0ZW0ub3V0LnByaW50bG4oIkY6IiArIHMpKTsKICAgIH0KfQo=