import java.util.*; import java.util.stream.*; public class StreamKnownSource { void testFlatMap(List list) { long count = list.stream().flatMap(s -> Stream.empty()).count(); if(count > 0) { System.out.println("Impossible"); } } void testEmptyStream(List list) { long result = Stream.empty().count(); if (result > 0) { System.out.println("Never"); } int sum = list.stream().filter(Objects::isNull).filter(Objects::nonNull).mapToInt(String::length).sum(); if (sum == 0) { System.out.println("Always"); } Optional any = Stream.empty().map(String::trim).findAny(); if (any.isPresent()) { System.out.println("Never"); } Optional any2 = Stream.of("x").map(String::trim).findAny(); if (any2.isPresent()) { System.out.println("Always"); } Optional any3 = list.stream().map(String::trim).findAny(); if (any3.isPresent()) { System.out.println("Possible"); } Optional any4 = Stream.of("x").limit(list.size()).map(String::trim).findAny(); if (any4.isPresent()) { System.out.println("Probably"); } boolean emptyAll = Stream.empty().allMatch(Objects::nonNull); if (emptyAll) { System.out.println("True"); } boolean emptyAny = Stream.empty().anyMatch(Objects::nonNull); if (emptyAny) { System.out.println("False"); } boolean emptyNone = Stream.empty().noneMatch(Objects::nonNull); if (emptyNone) { System.out.println("True"); } boolean allMatch = Stream.generate(() -> "foo").limit(10).allMatch("bar"::equals); if (allMatch) { // currently we're not aware that stream is non-empty (limit arg is not processed), and allMatch could be true for empty list System.out.println("Who knows?"); } Optional min = Stream.empty().min(Comparator.comparing(String::length)); if(min.isPresent()) { System.out.println("Impossible"); } } void testSingleElement(String foo) { Optional notNull = Stream.of(foo).filter(Objects::nonNull).findAny(); if(notNull.isPresent() && foo == null) { System.out.println("Impossible"); } OptionalInt max = IntStream.of(1).max(); if(max.isPresent()) { System.out.println("Always"); } OptionalLong opt = LongStream.of(2).reduce(Long::sum); if(opt.isPresent()) { System.out.println("Always"); } } void testStreamOf(int[] arr) { if(Stream.of().count() > 0) { System.out.println("Impossible"); } if(Stream.of("foo", "bar").findFirst().isPresent()) { System.out.println("Always"); } if(DoubleStream.of(0.0, 1.0, 2.0, 3.0).map(x -> x*2).max().isPresent()) { System.out.println("Always"); } if(IntStream.of(arr).filter(x -> x > 0).sum() > 0 && arr.length == 0) { System.out.println("Impossible"); } } void testListSize(List list, int[] arr) { if (arr.length == 0 && Arrays.stream(arr).anyMatch(x -> x > 0)) { System.out.println("Impossible"); } if (Arrays.stream(arr).anyMatch(x -> x > 0) && arr.length == 0) { System.out.println("Impossible"); } boolean empty = list.isEmpty(); String res = list.stream().map(String::trim).findFirst().orElse(null); if(res == null && empty) { System.out.println("res == null -> empty list"); } Optional first = list.stream().filter(Objects::nonNull).findFirst(); if (empty) { System.out.println(first.get()); } } // IDEA-176129 void foo(List list) { if (!list.isEmpty()) { return; } boolean hasNoNulls = list.stream().allMatch(Objects::nonNull); if(hasNoNulls) { System.out.println("Always"); } } }