5 Advanced Java Stream Tricks You Need to Know
Java Streams are a powerful feature that can simplify data processing and manipulation. In this article, we’ll explore five advanced Java Stream tricks that will help you leverage Streams to create more efficient and readable code. These tricks i...

I am Tuanh.net. As of 2024, I have accumulated 8 years of experience in backend programming. I am delighted to connect and share my knowledge with everyone.
1. Creating a Map to Cache an Entity
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class User {
private int id;
private String name;
// Constructors, getters, setters
}
public class EntityCacheExample {
public static void main(String[] args) {
List<User> users = List.of(
new User(1, "Alice"),
new User(2, "Bob"),
new User(3, "Charlie")
);
Map<Integer, User> userCache = users.stream()
.collect(Collectors.toMap(User::getId, user -> user));
System.out.println(userCache);
}
}
{1=User{id=1, name='Alice'}, 2=User{id=2, name='Bob'}, 3=User{id=3, name='Charlie'}}
2. Creating a Nested Map
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class User {
private String department;
private String role;
private String name;
// Constructors, getters, setters
}
public class NestedMapExample {
public static void main(String[] args) {
List<User> users = List.of(
new User("HR", "Manager", "Alice"),
new User("IT", "Developer", "Bob"),
new User("IT", "Manager", "Charlie")
);
Map<String, Map<String, List<User>>> nestedMap = users.stream()
.collect(Collectors.groupingBy(User::getDepartment,
Collectors.groupingBy(User::getRole)));
System.out.println(nestedMap);
}
}
{HR={Manager=[User{name='Alice'}]}, IT={Developer=[User{name='Bob'}], Manager=[User{name='Charlie'}]}}
3. Creating a Map with Two Values
import java.util.List;
import java.util.Map;
import java.util.AbstractMap.SimpleEntry;
import java.util.stream.Collectors;
class User {
private int id;
private String name;
private int age;
// Constructors, getters, setters
}
public class MapWithTwoValuesExample {
public static void main(String[] args) {
List<User> users = List.of(
new User(1, "Alice", 30),
new User(2, "Bob", 25),
new User(3, "Charlie", 35)
);
Map<Integer, Map.Entry<String, Integer>> userMap = users.stream()
.collect(Collectors.toMap(User::getId, user ->
new SimpleEntry<>(user.getName(), user.getAge())));
System.out.println(userMap);
}
}
{1=Alice=30, 2=Bob=25, 3=Charlie=35}
4. Grouping By and Mapping
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class User {
private String department;
private String name;
// Constructors, getters, setters
}
public class GroupingByMappingExample {
public static void main(String[] args) {
List<User> users = List.of(
new User("HR", "Alice"),
new User("IT", "Bob"),
new User("HR", "Charlie")
);
Map<String, List<String>> groupedMap = users.stream()
.collect(Collectors.groupingBy(User::getDepartment,
Collectors.mapping(User::getName, Collectors.toList())));
System.out.println(groupedMap);
}
}
{HR=[Alice, Charlie], IT=[Bob]}
5. Grouping By, Mapping, and Reducing
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
class Transaction {
private String type;
private int amount;
// Constructors, getters, setters
}
public class GroupingByMappingReducingExample {
public static void main(String[] args) {
List<Transaction> transactions = List.of(
new Transaction("Deposit", 100),
new Transaction("Deposit", 200),
new Transaction("Withdrawal", 50),
new Transaction("Withdrawal", 30)
);
Map<String, Integer> transactionSums = transactions.stream()
.collect(Collectors.groupingBy(Transaction::getType,
Collectors.reducing(0, Transaction::getAmount, Integer::sum)));
System.out.println(transactionSums);
}
}
{Deposit=300, Withdrawal=80}
6. Conclusion
Read more at : 5 Advanced Java Stream Tricks You Need to Know





