相关文章推荐
有胆有识的槟榔  ·  如何通过Go SDK ...·  5 天前    · 
伤情的消防车  ·  定义 NMAKE 宏 | ...·  3 天前    · 
阳刚的路灯  ·  String.raw() - ...·  3 天前    · 
粗眉毛的硬币  ·  通过Java ...·  昨天    · 
威武的香瓜  ·  jenkins shell 管道符-掘金·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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]}

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));

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.

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]

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.