问题
Forgive me if the solution is very obvious but I can't seem to figure out how to do this
public static void main(String[] args) {
Map<String, String> map = new HashMap<>();
map.put("b1", "a1");
map.put("b2", "a2");
map.put("b3", "a1");
Map<String, List<String>> mm = map.values().stream().collect(Collectors.groupingBy(m -> m));
System.out.println(mm);
}
I want to group by based on values in hashmap. I want the output to be {a1=[b1, b3], a2=[b2]}
but it is currently coming as {a1=[a1, a1], a2=[a2]}
回答1:
Currently, you're streaming over the map values (which I assume is a typo), based on your required output you should stream over the map entrySet
and use groupingBy
based on the map value's and mapping
as a downstream collector based on the map key's:
Map<String, List<String>> result = map.entrySet()
.stream()
.collect(Collectors.groupingBy(Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey,
Collectors.toList())));
You could also perform this logic without a stream via forEach
+ computeIfAbsent
:
Map<String, List<String>> result = new HashMap<>();
map.forEach((k, v) -> result.computeIfAbsent(v, x -> new ArrayList<>()).add(k));
回答2:
You can use Collectors.mapping with Collectors.groupingBy on the entrySet
of the map as :
Map<String, List<String>> mm = map.entrySet()
.stream()
.collect(Collectors.groupingBy(Map.Entry::getValue,
Collectors.mapping(Map.Entry::getKey, Collectors.toList())));
but it is currently coming as {a1=[a1, a1], a2=[a2]}
That's because you are currently grouping on the values collection which is {a1, a2, a1}
only.
回答3:
public class Test5 {
public static void main(String[] args) {
List<String> list1 = List.of("Tabu", "Gina", "protijayi", "Gini", "Gini","North Calcutta");
List<String> list2 = List.of("Soudipta", "Gina", "Gina", "upto");
List<String> list3 = List.of("Soudipta", "Gina", "protijayi", "Tabu","South Calcutta");
List<List<String>> listres = List.of(list1, list2, list3);
System.out.println(listres);
/*
[[Tabu, Gina, protijayi, Gini, Gini, North Calcutta],
[Soudipta, Gina, Gina, upto],
[Soudipta, Gina, protijayi, Tabu, South Calcutta]]
*/
Map<String, List<Long>> FirstKeyThenValue1 = listres.stream().flatMap(
// mapper
list -> list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().parallelStream()
).collect(Collectors.groupingBy(Entry::getKey, Collectors.mapping(
// mapper, downstream
Entry::getValue, Collectors.toList())));
System.out.println("FirstKeyThenValue1 -> " + FirstKeyThenValue1);
/*
{
upto=[1],
Soudipta=[1, 1],
Gina=[1, 2, 1],
Tabu=[1, 1],
North Calcutta=[1],
South Calcutta=[1],
protijayi=[1, 1],
Gini=[2]}
*/
Map<Long, List<String>> FirstValueThenkey1 = listres.stream().flatMap(
// mapper
list -> list.stream().collect(Collectors.groupingBy(Function.identity(), Collectors.counting()))
.entrySet().stream()
).collect(
Collectors.groupingBy(Entry::getValue, Collectors.mapping(
Entry::getKey, Collectors.toList()
))
);
System.out.println(" FirstValueThenkey1 => " + FirstValueThenkey1);
/*
{
1=[Gina, Tabu, North Calcutta, protijayi, upto, Soudipta,
Soudipta, Gina, Tabu, South Calcutta, protijayi],
2=[Gini, Gina]
}
*/
}// main
}
来源:https://stackoverflow.com/questions/53986939/collectors-groupby-for-mapstring-liststring