import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.stream.*; public class StreamInlining { void testNulls(List list) { list.stream().filter(null).forEach(System.out::println); list.stream().map(null).forEach(System.out::println); list.stream().flatMap(null).forEach(System.out::println); list.stream().filter(x -> x != null).forEach(null); List l = null; l.stream().count(); int[] arr = null; Arrays.stream(arr).count(); Stream stream = null; stream.filter(x -> x != null).forEach(System.out::println); } void testMethodRef(List list, int[] data) { if(list.stream().map(String::new).anyMatch(x -> x == null)) { System.out.println("never"); } if(Arrays.stream(data).mapToObj(int[]::new).anyMatch(x -> x == null)) { System.out.println("never"); } list.stream().filter(Objects::isNull).map(String::trim).forEach(System.out::println); if(list.stream().filter(Objects::nonNull).anyMatch(x -> x == null)) { System.out.println("never"); } list.stream().map(Integer::valueOf).filter(Objects::nonNull).forEach(System.out::println); } void filterDistinctLimitSkipFilter(List list) { list.stream().filter(x -> x == null).distinct().limit(10).skip(1).filter(x -> x != null).forEach(System.out::println); } static class Holder { Object obj; @Nullable String nullable; @Nullable String getNullable() { return nullable; } } int hash(List holders) { return holders.stream().filter(h -> h.obj == null).mapToInt(h -> h.obj.hashCode()).sum(); } void test2(int[] array) { IntStream.of(array).filter(x -> x < 5) .filter(x -> x > 7) // TODO: find variable state for non-qualified value .forEach(value -> System.out.println(value)); } // IDEA-152871 static class A { void improved(String[] arr) { Arrays.stream(arr).map(s -> s.isEmpty() ? s : null) .map(s1 -> some(s1)) .forEach(s -> System.out.println(s)); } void improvedMethodRef(String[] arr) { Arrays.stream(arr).map(s -> s.isEmpty() ? s : null) .map(A::some) .forEach(s -> System.out.println(s)); } public static String some(@NotNull String s) { return s; } } // IDEA-169280 void f(List l) { if(l.stream().filter(s -> s != null).allMatch(s -> s != null && s.startsWith("A"))) { System.out.println("ok"); } } void boxed(IntStream is) { is.boxed().filter(x -> x != null).forEach(s -> System.out.println(s)); } boolean flatMap(List list, List> ll) { System.out.println(ll.stream().flatMap(l -> l.stream()).count()); return list.stream().map(s -> s.isEmpty() ? null : s) .flatMap(s -> Stream.of(s, s.trim()).filter(r -> r != null)) .anyMatch(x -> x == null); } String blockLambda(List list) { return list == null ? "" : list.stream().map(s -> { return s.equals("abc") ? null : s; }).findFirst().orElse(""); } // IDEA-164262 static class MyClass { @Nullable static String nullableFunction(String s) { return s; } static String functionThatDoesNotAcceptNull(@NotNull String s) { return s; } void test(List list) { list.stream() .map(MyClass::nullableFunction) .map(s -> functionThatDoesNotAcceptNull(s)) // Exception will be thrown here .forEach(System.out::println); } void testMr(List list) { list.stream() .map(MyClass::nullableFunction) .map(MyClass::functionThatDoesNotAcceptNull) .forEach(System.out::println); } } Optional testOptionalOfNullable(List list) { return list.stream().filter(Objects::isNull).map(Optional::ofNullable).findFirst().orElse(Optional.empty()); } void testGenerate() { List list1 = Stream.generate(() -> Math.random() > 0.5 ? "foo" : "baz") .limit(10).filter((xyz -> "bar".equals(xyz))).collect(Collectors.toList()); List list2 = Stream.generate(() -> "xyz").limit(20).filter("bar"::equals).collect(Collectors.toList()); Stream.generate(() -> Optional.of("xyz")).filter(Optional::isPresent).forEach(System.out::println); LongStream.generate(() -> 5).limit(10).filter(x -> x > 6).forEach(s -> System.out.println(s)); } @Nullable String process(String s) { return s.isEmpty() ? null : s; } void testMinMax(List list) { list.stream().map(this::process).max(Comparator.naturalOrder()); list.stream().map(this::process).max(Comparator.nullsFirst(Comparator.naturalOrder())); list.stream().map(this::process).max(Comparator.nullsFirst(Comparator.comparing(String::length))); list.stream().map(this::process).min(Comparator.nullsLast(Comparator.comparing(String::length))); list.stream().map(this::process).min(Comparator.comparing(String::length)); list.stream().map(this::process).min(Comparator.comparing(String::length).reversed()); } void testSorted(List list) { list.stream().sorted(Comparator.comparing(this::process, Comparator.reverseOrder())).collect(Collectors.toList()); list.stream().sorted(Comparator.comparing(this::process)).collect(Collectors.toList()); list.stream().sorted(Comparator.comparing(this::process, Comparator.nullsFirst(Comparator.naturalOrder()))).collect(Collectors.toList()); list.stream().map(this::process).sorted(Comparator.comparing(String::length)).collect(Collectors.toList()); list.stream().map(this::process).sorted().collect(Collectors.toList()); list.stream().map(this::process).sorted(String::compareToIgnoreCase).collect(Collectors.toList()); list.stream().map(this::process).sorted(String.CASE_INSENSITIVE_ORDER).collect(Collectors.toList()); } void testSortedCheck(List holders) { holders.stream().sorted(Comparator.comparing(h -> h.nullable)).toArray(); holders.stream().filter(h -> h.nullable != null).sorted(Comparator.comparing(h -> h.nullable)).toArray(); holders.stream().sorted(Comparator.comparing(h -> h.getNullable())).toArray(); holders.stream().filter(h -> h.getNullable() != null).sorted(Comparator.comparing(h -> h.getNullable())).toArray(); holders.stream().sorted(Comparator.comparing(Holder::getNullable)).toArray(); holders.stream().filter(h -> h.getNullable() != null).sorted(Comparator.comparing(Holder::getNullable)).toArray(); } }