I am having the below code and doing groupingBy based on boolean
Map<Boolean, List<Test>> products = testList
.stream()
.collect(Collectors.groupingBy(Test::isValidUser));
I want to collect it as Map<String, List<Test>
.
Based on the boolean value, want to add the custom key as "Valid" and "Invalid".
If the isValidUser
true, want to add the key as "Valid", else key should be "Invalid"
Any possibility to do this in Java 11?
Note: without adding the String variable in Test class
You may do it using the ternary operator in the key classifier function in the groupingBy
collector. Here's how it looks.
Map<String, List<Test>> products = testList.stream()
.collect(Collectors.groupingBy(t -> t.isValidUser() ? "Valid" : "Invalid"));