๐ธ What is Stream API?
The Stream API was introduced in Java 8 to process collections of data in a functional-style โ i.e., using a chain of operations instead of loops.
โ
Works with: List
, Set
, Map
, arrays, etc.
โ
Focuses on what to do, not how.
๐น Stream Basics
List<String> names = List.of("John", "Alice", "Bob");
// Stream Example: print all names
names.stream().forEach(System.out::println);
๐น Common Stream Operations
1. filter()
โ Select matching elements
List<String> names = List.of("John", "Alice", "Bob");
List<String> result = names.stream()
.filter(name -> name.startsWith("A"))
.collect(Collectors.toList());
System.out.println(result); // [Alice]
2. map()
โ Transform elements
List<String> names = List.of("john", "alice");
List<String> upper = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
System.out.println(upper); // [JOHN, ALICE]
3. sorted()
โ Sort elements
List<Integer> numbers = List.of(5, 1, 3);
List<Integer> sorted = numbers.stream()
.sorted()
.collect(Collectors.toList());
System.out.println(sorted); // [1, 3, 5]
4. distinct()
โ Remove duplicates
List<Integer> nums = List.of(1, 2, 2, 3);
List<Integer> unique = nums.stream()
.distinct()
.collect(Collectors.toList());
System.out.println(unique); // [1, 2, 3]
5. limit()
and skip()
List<String> data = List.of("a", "b", "c", "d", "e");
List<String> result = data.stream()
.skip(1)
.limit(3)
.collect(Collectors.toList());
System.out.println(result); // [b, c, d]
6. count()
โ Count elements
long count = List.of(1, 2, 3, 4, 5).stream()
.filter(n -> n % 2 == 0)
.count();
System.out.println(count); // 2
7. collect()
โ Gather results
List<String> list = List.of("A", "B", "C");
String joined = list.stream()
.collect(Collectors.joining(", "));
System.out.println(joined); // A, B, C
8. reduce()
โ Reduce to a single value
int sum = List.of(1, 2, 3, 4).stream()
.reduce(0, (a, b) -> a + b);
System.out.println(sum); // 10
9. anyMatch()
, allMatch()
, noneMatch()
List<Integer> list = List.of(1, 2, 3, 4);
// Any even?
System.out.println(list.stream().anyMatch(n -> n % 2 == 0)); // true
// All even?
System.out.println(list.stream().allMatch(n -> n % 2 == 0)); // false
// None even?
System.out.println(list.stream().noneMatch(n -> n % 2 == 0)); // false
10. findFirst()
/ findAny()
Optional<Integer> first = List.of(10, 20, 30).stream()
.findFirst();
System.out.println(first.get()); // 10
๐ธ Streams with Map
Map<String, Integer> map = Map.of("A", 10, "B", 20);
// Filter map by value
map.entrySet().stream()
.filter(entry -> entry.getValue() > 10)
.forEach(e -> System.out.println(e.getKey())); // B
๐ธ Parallel Stream
Process in multiple threads (fast on large data):
List<Integer> bigList = List.of(1,2,3,4,5,6,7,8,9,10);
long count = bigList.parallelStream()
.filter(n -> n % 2 == 0)
.count();
System.out.println(count); // 5
๐ธ Summary Table
Operation | Purpose |
---|---|
filter() | Filter data |
map() | Transform data |
forEach() | Perform action per element |
sorted() | Sort elements |
distinct() | Remove duplicates |
limit() , skip() | Control stream range |
count() | Count items |
collect() | Gather into list/set/map |
reduce() | Reduce to single value |
anyMatch() | Check any match |
allMatch() | Check all match |
findFirst() | Get first matching element |
Would you like a PDF cheat sheet version of this or an interactive Java project to practice it?