import java.util.Iterator; import java.util.Random; import java.util.stream.Stream; import org.jetbrains.annotations.Nullable; public class ConsumedStream { public static void test1() { Stream stream = Stream.of("x"); stream.forEach(System.out::println); stream.forEach(System.out::println); } public static void test2() { Stream stream = Stream.of("x"); Stream stream2 = stream.filter(String::isEmpty); Stream stream3 = stream.filter(x -> !x.isEmpty()); } public static void test3() { Stream stream = Stream.of("x"); for (int i = 0; i < 10; i++) { stream.forEach(System.out::println); } } public static void test4() { Stream stream = Stream.of("x"); stream.forEach(System.out::println); if (Math.random() > 0.5) { stream.forEach(System.out::println); } } public static void test5() { Stream stream = Stream.of("x"); switch (new Random().nextInt(10)) { case 0: stream.forEach(System.out::println); case 1: stream.forEach(System.out::println); } } public static void test6() { Stream stream = Stream.of("x"); try { method1(); stream.forEach(System.out::println); } finally { stream.forEach(System.out::println); } } private static void method1() {} public static void test7() { Stream stream = Stream.of("x"); try { stream.forEach(System.out::println); } catch (Exception e) { stream.forEach(System.out::println); } } public static void test1E() { Stream stream = Stream.of("x"); int x = new Random().nextInt(10); if (x < 2) { stream.forEach(System.out::println); } if (x < 1) { stream.forEach(System.out::println); } } public static void test2E() { Stream stream = Stream.of("x"); Stream stream2 = stream.filter(String::isEmpty); double random = Math.random(); Stream stream3 = stream.filter(x -> !x.isEmpty()); } public static void test3E() { Stream stream = Stream.of("x"); double random = Math.random(); if (random > 0.5) { stream.forEach(System.out::println); } if (random > 0.9) { stream.forEach(System.out::println); } } public static void test6E() { Stream stream = Stream.of("x"); double random2 = Math.random(); Stream stringStream = stream.filter(t -> true); boolean parallel = stream.isParallel(); double random3 = Math.random(); method(stream); stream.filter(t -> true); } public static void test7E() { Stream stream = Stream.of("x"); Stream parallel = stream.parallel().filter(t->true); stream.forEach(System.out::println); } public static void test8E() { Stream stream = Stream.of("x"); stream.forEach(System.out::println); Stream parallel = stream.parallel(); parallel.forEach(System.out::println); } private static void method(Stream stream) { Iterator iterator = stream.iterator(); } public static Stream test9E() { Stream stringStream = Stream.of("1"); Stream stringStream1 = stringStream.filter(t -> true); return stringStream; } public static Stream test10E() { Stream stringStream = Stream.of("1"); Stream stringStream1 = stringStream.filter(t -> true); return stringStream.sequential(); } public static void test11E() { Stream integerStream = test11Em(); Stream integerStream1 = integerStream.filter(t -> true); Stream integerStream2 = integerStream.filter(t -> true); } private static void test12E(Stream stream) { stream.forEach(System.out::println); String text = "noError"; try { stream.forEach(System.out::println); } catch (IllegalStateException e) { text = "IllegalStateException"; } catch (RuntimeException e) { text = "RuntimeException"; } if (text.equals("IllegalStateException")) { System.out.println("error"); } } private static void test13E(int x) { Stream stream = Stream.of("x"); if (x == 0) { stream.forEach(System.out::println); } stream.forEach(System.out::println); } @Nullable public static Stream test11Em() { return null; } public static void test1N() { Stream stream = Stream.of("x"); if (Math.random() > 0.5) { stream.forEach(System.out::println); } else { stream.distinct().forEach(System.out::println); } } public static void test2N() { Stream stream = Stream.of("x"); stream = stream.filter(x -> !x.isEmpty()); stream.forEach(System.out::println); } public static void test3N() { Stream stream = Stream.of("x"); System.out.println(stream.isParallel()); stream.forEach(System.out::println); } public static void test4N() { Stream stream = Stream.of("x"); switch (new Random().nextInt(10)) { case 0: stream.forEach(System.out::println); break; case 1: stream.forEachOrdered(System.out::println); } } public static void test5N() { Stream stream = Stream.of("x"); for (int i = 0; i < 10; i++) { if (Math.random() > 0.5) { stream.forEach(System.out::println); break; } } } public static void test1NE() { Stream stream = Stream.of("x"); boolean b = Math.random() > 0.5; if (b) { stream.forEach(System.out::println); } if (!b) { stream.forEach(System.out::println); } } public static void test2NE() { Stream stream = Stream.of("x"); int x = new Random().nextInt(10); if (x < 2) { stream.forEach(System.out::println); } if (x > 5) { stream.forEach(System.out::println); } } public static Stream test3NE() { Stream stringStream = Stream.of("1"); return stringStream; } public static void test4NE() { Stream stringStream = Stream.of("1"); stringStream.filter(t -> true); } }