Java Streams Cheat Sheet: Master Intermediate and Terminal Operations

🚀 Java Streams – Complete Cheat Sheet (Save This 📌) Still struggling with Java Streams? Here’s everything you need in one place 👇 💡 Stream Pipeline: Source → Intermediate → Terminal 🔹 Intermediate Operations: ✔ filter() -👉 Keeps only elements that match a condition Ex: filter(x -> x>10) ✔ map() - 👉 Transforms each element into something else Ex: map(x -> x*2) ✔ flatMap() - 👉 Flattens nested structures (List of Lists → single list) Ex: List<List<String>> list=List.of(List.of("A", "B"),List.of("C", "D")); list. stream() .flatMap(Collection::stream) ✔ distinct() - 👉 Removes duplicate elements Ex: list. stream().distinct().forEach(System.out::println) ✔ sorted() -👉 Sorts elements (default or custom) Ex: list. stream().sorted().forEach(System.out::println);  ✔ limit() / skip() - 👉 Takes first n elements / 👉 Skips first n elements Ex: list. stream().limit(3).forEach(System.out::println); // 1, 2, 3 list. stream().skip(2).forEach(System.out::println); // 3, 4, 5 ✔ peek() - 👉 Performs action without modifying data  Ex: .peek(x -> System.out.println("Processing: "+x)) ✔ takeWhile() / dropWhile() (Java9+) -👉 Takes elements until condition becomes false / 👉 Skips elements until condition becomes false Ex: List<Integer> list=List.of(1, 2, 3, 0, 4, 5); list. stream() .takeWhile(x -> x>0).forEach(System.out::println); // 1, 2, 3 list. stream() .dropWhile(x -> x>0).forEach(System.out::println); // 0, 4, 5 🔹 Terminal Operations: ✔ forEach() - 👉 Iterates over elements Ex: list. stream().forEach(System.out::println); ✔ collect() - 👉 Converts stream into collection (List, Set, Map) Ex: List<Integer> result = list. stream().filter(x -> x > 2).collect(Collectors.toList()); ✔ count() - 👉 Returns number of elements Ex: long count = list. stream().filter(x -> x > 2).count(); ✔ min() / max() - 👉 Finds smallest / largest element Ex: int min = list. stream().min(Comparator.naturalOrder()).get(); int max = list. stream() .max(Comparator.naturalOrder()).get(); ✔ findFirst() / findAny() - 👉 Returns first or any element Ex: Optional<Integer> first = list. stream() .findFirst(); Optional<Integer> any = list.stream() .findAny(); ✔ anyMatch() / allMatch() / noneMatch() - 👉 Returns true if any element matches condition / 👉 Returns true if all elements match / 👉 Returns true if no elements match Ex: boolean any = list. stream() .anyMatch(x -> x > 3); boolean all = list. stream() .allMatch(x -> x > 0); boolean none = list. stream() .noneMatch(x -> x < 0); ✔ reduce() - 👉 Combines all elements into one result Ex: int sum = list. stream() .reduce(0, (a, b) -> a + b); // sum of elements 🔥 Example: List<String> result=employees. stream() .filter(e -> e.getSalary() >30000) // filter .map(Employee::getName) // map .distinct() // remove duplicates .sorted() // sort .limit(3) // limit .collect(Collectors.toList()); // collect 👉 Save for interviews & daily coding #Java #JavaStreams #SpringBoot #BackendDevelopment #CodingTips #Developers #InterviewCheatSheat

To view or add a comment, sign in

Explore content categories