Reputation: 597
I have a test program:
public class App {
public static void main(String[] args) {
List<Integer> a = Arrays.asList(1, 11);
List<Integer> b = Arrays.asList(2, 22);
List<Integer> c = Arrays.asList(3, 33);
Map<String, List<Integer>> map = new HashMap<>();
map.put("a", a);
map.put("b", b);
map.put("c", c);
Set<String> valid = new HashSet<>();
valid.add("a");
Map<Boolean, List<Map.Entry<String, List<Integer>>>> partitions =
map.entrySet().stream()
.collect(Collectors.partitioningBy(
entry -> valid.contains(entry.getKey())));
System.out.println(partitions);
// partition by the key of the map
// then reduce the values into a single collection
Map<Boolean, List<Integer>> result = map.entrySet().stream()
.collect(Collectors.partitioningBy(
entry -> valid.contains(entry.getKey()),
Collectors.mapping(Map.Entry::getValue,
Collectors.reducing(new ArrayList<>(),
(l1, l2) -> {
l1.addAll(l2);
return l1;
}))));
System.out.println(result);
}
}
I'm expecting the final result to be
{false=[b=[2, 22], c=[3, 33]], true=[a=[1, 11]]}
{false=[2, 22, 3, 33], true=[1, 11]}
But in the actual result, both true and false keys have all 6 integers:
{false=[b=[2, 22], c=[3, 33]], true=[a=[1, 11]]}
{false=[1, 11, 2, 22, 3, 33], true=[1, 11, 2, 22, 3, 33]}
Notice the 2 partitioning functions are exactly the same. But the downstream mixed up the values in the separate partitions. How can that be? I assume the downstream would only operate on each partition...
What did I miss here?
Thanks.
Upvotes: 1
Views: 1381
Reputation: 44398
To complete already accepted answer, you can use Collectors.groupingBy
using your classifier with Collectors.flatMapping
as of Java 9 as a downstream collector.
Map<Boolean, List<Integer>> result = map.entrySet().stream()
.collect(Collectors.groupingBy(
e -> valid.contains(e.getKey()),
Collectors.flatMapping(e -> e.getValue().stream(), Collectors.toList())));
Upvotes: 0
Reputation: 18440
For reducing same ArrayList reference is used in both partition.
You can use Collectors.toMap
and create a new instance merging two lists.
Map<Boolean, List<Integer>> result =
map.entrySet()
.stream()
.collect(Collectors.toMap(e -> valid.contains(e.getKey()), Map.Entry::getValue,
(l1, l2) -> {
List<Integer> l3 = new ArrayList<>(l1);
l3.addAll(l2);
return l3;
}));
And if you want to use the same taste
Map<Boolean, List<Integer>> result =
map.entrySet()
.stream()
.collect(Collectors.toMap(e-> valid.contains(e.getKey()), Map.Entry::getValue,
(l1, l2) -> Stream.concat(l1.stream(), l2.stream())
.collect(Collectors.toList())));
Upvotes: 1