ADVANCED STREAM FEATURES AND REGULAR EXPRESSIONS
UNIT 5
Importance and Use Cases of Advanced Stream Features
Write compact, readable code to handle sophisticated transformations.
Use parallel streams to take advantage of multi-core processors.
Group, partition, and reduce data beyond simple collection operations.
Compose dynamic, modular data processing pipelines.
Avoid side effects, mutable state, and complex loops.
Key Concepts & Use Cases
Collector | Description |
toList() | Collect elements into a List |
toSet() | Collect into a Set |
toMap() | Collect into a Map with key-value pairs |
joining() | Concatenate elements into a single String |
counting() | Count the number of elements |
summarizingInt() | Collect statistics (count, sum, min, avg, max) |
groupingBy() | Group elements by a classifier |
partitioningBy() | Partition elements by a predicate |
Custom Streams
Stream.generate()
Stream.iterate()
Stream.builder()
FlatMapping
Chaining Stream Operations
Stream Peeking (peek)
Introduction to Regular Expressions
Character Classes
Pattern | Matches |
[abc] | a, b, or c |
[^abc] | any character except a, b, or c |
[a-z] | any lowercase letter |
[A-Z] | any uppercase letter |
[0-9] | any digit |
[a-zA-Z] | any letter |
[a-zA-Z0-9] | any alphanumeric character |
Quantifiers
Symbol | Meaning | Example | Matches |
? | 0 or 1 time | a? | "", "a" |
* | 0 or more times | a* | "", "a", "aa", "aaa" |
+ | 1 or more times | a+ | "a", "aa", "aaa" |
{n} | exactly n times | a{3} | "aaa" |
{n,} | n or more times | a{2,} | "aa", "aaa", ... |
{n,m} | between n and m times | a{2,4} | "aa", "aaa", "aaaa" |
String Manipulation
Operation | Method |
Length | length() |
Character at index | charAt(int) |
Substring | substring(int, int) |
Replace | replace(old, new) |
Split | split(regex) |
Case conversion | toUpperCase(), toLowerCase() |
Trimming | trim() |
Comparison | equals(), compareTo() |
String Replacement
String s = "banana";
String replaced = s.replace('a', 'o');
System.out.println(replaced); // "bonono"
String s = "The cat sat on the mat.";
String replaced = s.replace("cat", "dog");
System.out.println(replaced); // "The dog sat on the mat."
String s = "Price: $100, $200, and $300";
String replaced = s.replaceAll("\\$\\d+", "[PRICE]");
System.out.println(replaced); // "Price: [PRICE], [PRICE], and [PRICE]"
String s = "abc123xyz123";
String replaced = s.replaceFirst("\\d+", "###");
System.out.println(replaced); // "abc###xyz123"
Replacing with Backreferences