0

I am new to Java 8 Stream API. I am wondering if I can create multiple lists based on the Key Value from a Map? For Example. If my Map is

{"Developer", Developer; "Manager", Manager; "Lead", Lead; "Director", Director}

I would like to create a List of Developer, List of Manager, List of Lead and List of Director from the Map based on the Key Values.Any help is appreciated.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
  • key and value both are string? can you please show the code with and example – Ryuzaki L Oct 03 '19 at 23:10
  • Each of such lists in your example would have only one element in it, which makes it look like [XY problem](https://meta.stackexchange.com/q/66377). – Pshemo Oct 03 '19 at 23:21
  • Are you talking about a stream of maps? or just one map? If it is just one map, then no need for streams as maps are meant to store key-value pairs. Having a key means you can easily access the values too. – jrook Oct 03 '19 at 23:26
  • Possible duplicate of [How do I convert a String to an int in Java?](https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java) – Henry Henrinson Oct 03 '19 at 23:39

3 Answers3

2

Using Collectors.groupingBy, you can generate a Map from your Key to a List of Values, provided you can compute the Key from the Value. Alternatively, you can use Collectors.toMap, provided you can compute both the Key and the Value from an upstream element. You probably want the version of toMap with a merge function, because that will allow you to handle multiple keys with the same value (by putting them in a list together).

Edit:
If you want ordering, there are overloads for toMap and groupingBy that allow you to provide a mapFactory (Supplier<Map>) , such as TreeMap::new.

Avi
  • 2,611
  • 1
  • 14
  • 26
0

To invert a map, so that its distinct values become keys, and its keys are added to a collection under the corresponding value, use groupingBy() on the map entries. It's important that the values from the original map implement equals() and hashCode() correctly to be used as a key in the new hash table .

static <K, V> Map<V, Set<K>> invert(Map<? extends K, ? extends V> original) {
  return original.entrySet().stream()
    .collect(Collectors.groupingBy(
      Map.Entry::getValue, 
      Collectors.mapping(Map.Entry::getKey, Collectors.toSet())
    ));
}

If you want to groups to be sorted, you can create a specialized "downstream" collector:

static <K, V> Map<V, SortedSet<K>> invert(
    Map<? extends K, ? extends V> original, 
    Comparator<? super K> order) {

  Collector<K, ?, SortedSet<K>> toSortedSet = 
     Collectors.toCollection(() -> new TreeSet<>(order));
  return original.entrySet().stream()
    .collect(Collectors.groupingBy(
      Map.Entry::getValue, 
      Collectors.mapping(Map.Entry::getKey, toSortedSet)
    ));
}
erickson
  • 265,237
  • 58
  • 395
  • 493
0

Please find below code for the same by using Collectors.groupBy() :

                List<Details> employeeList = Arrays.asList(new Details("Pratik", "Developer"), new Details("Rohit", "Manager"), new Details("Sonal", "Developer"), new Details("Sagar", "Lead"), new Details("Sanket", "Lead"));       

                Map<String, List<Details>> collect = employeeList.stream().collect(Collectors.groupingBy(x-> x.getDesignation()));
                System.out.println("Checking details "+ collect);