import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.*; 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; } 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) .forEach(value -> System.out.println(value)); } void testInstanceof(List objects) { objects.stream() .filter(x -> x instanceof String) .filter(x -> x instanceof Number) .forEach(System.out::println); } void testIsInstanceIncomplete(List objects) { IntStream is = objects.stream() .filter(String.class::isInstance) .mapToInt(x -> (Integer)x); objects.stream() .filter(String.class::isInstance) .filter(Number.class::isInstance); objects.stream() .filter(x -> x instanceof String) .filter(Number.class::isInstance); objects.stream() .filter(String.class::isInstance) .filter(String.class::isInstance); } Stream testInstanceOfMap(List objects) { return objects.stream().filter(it -> it instanceof String) .map(entry -> { if (entry instanceof String) { return (String)entry; } return null; }) .filter(Objects::nonNull); } void test(Stream stream, Optional opt) { stream.filter(String.class::isInstance).forEach(System.out::println); opt.filter(String.class::isInstance).ifPresent(System.out::println); } // 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.isEmpty() ? null : 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); } } 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)); } // IDEA-183501 void testFlatMapIdentity(Stream stream) { Integer res = Stream.of(stream) .flatMap(Function.identity()) .min(Comparator.naturalOrder()) .orElse(null); if(res == null) { // not always null System.out.println("possible"); } } // IDEA-190591 void testReduce() { List input = new ArrayList<>(); input.add(0.0); Optional result = input.stream().reduce((a, b) -> { throw new IllegalStateException("Multiple entries found: " + a + " and " + b); }); Double res = result.orElse(null); if (res != null) { System.out.println(res); } else { System.out.println("Huh?"); } } void testReduce2(List input) { Optional result = input.stream().reduce((a, b) -> { throw new IllegalStateException("Multiple entries found: " + a + " and " + b); }); Double res = result.orElse(null); if (res != null) { System.out.println(res); } else { System.out.println("Huh?"); } } void testReduceNullability() { Optional res1 = Stream.of("foo", "bar", null).reduce((a, b) -> a); // a is never null - ok Optional res2 = Stream.of("foo", null, "bar").reduce((a, b) -> a); // wrong, but not supported yet Optional res3 = Stream.of("foo", "bar", null).reduce((a, b) -> b); Optional res4 = Stream.of(null, "foo", "bar").reduce((a, b) -> b); // b is never null - ok } public void testStreamTryFinally() { try { } finally { Stream.of("x").map(a -> { if(a.equals("baz")) { System.out.println("impossible"); } testStreamTryFinally(); return "bar"; }).count(); } } void testTryFinally2() { try { } finally { try { List list = Stream.of("xyz").map(a -> { testTryFinally2(); return "foo"; }).collect(Collectors.toList()); } catch (Exception e) { } } } void testToArray(List list) { list.stream().toArray(size -> null); } Object testBoxingExplicit() { return String.join("\n", Stream.of(12, 22) .map(Object::toString) .collect(Collectors.toList()) ); } public static void testBoxingExplicit2() { double s = Stream.of(1).mapToDouble(x -> x * x).sum(); } void testOptionalNullity(List groups) { Optional optional = groups.stream().findFirst(); if (optional != null && optional.isPresent()) { System.out.println("found"); } } void testImmediateCollection() { String result = Collections.singleton(" foo ").stream().map(String::trim).findFirst().orElse(null); if (result == null) { System.out.println("impossible"); } } void testCountNarrowing(String[] arr, List list) { long count1 = list.stream().filter(String::isEmpty).distinct().sorted().parallel().unordered().map(String::trim).count(); if (count1 > Integer.MAX_VALUE || count1 < 0) {} long count2 = Arrays.stream(arr).map(String::trim).count(); if (count2 > Integer.MAX_VALUE || count2 < 0) {} long count3 = Stream.of(arr).map(String::trim).count(); if (count3 > Integer.MAX_VALUE || count3 < 0) {} if (count3 == 1) {} long count4 = Stream.of(arr[0]).map(String::trim).count(); if (count4 == 1) {} long count5 = Stream.of("foo", "bar", "baz", "qux").filter(s -> s.length() > 1).count(); long count5a = Stream.of("foo", "bar", "baz", "q").filter(s -> s.length() > 1).count(); long count5b = Stream.of("foo", "bar", "bazzz", "qx").filter(s -> s.length() > 1).count(); if (count5 == 4) {} if (count5 < 0) {} if (count5 > 4) {} long count6 = Stream.of("foo", "bar", "baz", "qux").flatMap(s -> Stream.of(s, s)).count(); if (count6 < 0) {} if (count6 > 4) {} long count7 = Stream.of("foo", "bar").filter(x -> !x.isEmpty()).count(); if (count7 == 0 || count7 == 1 || count7 == 2) {} long count8 = x.isEmpty()).count()' is always '0'">Stream.of("foo", "bar").filter(x -> x.isEmpty()).count(); if (count8 == 0) {} long count9 = list.stream().map(String::trim).count(); if (count9 == 0) {} if (list.isEmpty()) return; long count10 = list.stream().map(String::trim).count(); if (count10 == 0) {} long count = Stream.of().filter(String::isEmpty).count(); if (count == 0) { } } void testFlatMapUnresolvedSymbol() { Stream.of("foo", "bar").flatMap(x -> Stream.of( aa, bb, cc)); } void testNotTooComplexForEach(List list) { int[] count = {0}; list.stream().forEach(l -> count[0]++); System.out.println(count[0]); } void testFromArray(String[] data) { if (data.length != 0) return; long result = Arrays.asList(data).stream().count(); if (result == 0) {} } }