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.

4
  • key and value both are string? can you please show the code with and example Commented Oct 3, 2019 at 23:10
  • Each of such lists in your example would have only one element in it, which makes it look like XY problem. Commented Oct 3, 2019 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. Commented Oct 3, 2019 at 23:26
  • Possible duplicate of How do I convert a String to an int in Java? Commented Oct 3, 2019 at 23:39

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

If I remember correctly, HashMaps and HashSets don't assure any order of the keys or values...
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)
    ));
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.