You can do it with groupingBy.
Assuming that your input is a List<Dog>, the Map member inside the Dog class is called map, and the Breed is stored for the "Breed" key :
List<Dog> dogs = ...
Map<String, List<Dog>> map = dogs.stream()
.collect(Collectors.groupingBy(d -> d.map.get("Breed")));
Answer from Eran on Stack OverflowYou can do it with groupingBy.
Assuming that your input is a List<Dog>, the Map member inside the Dog class is called map, and the Breed is stored for the "Breed" key :
List<Dog> dogs = ...
Map<String, List<Dog>> map = dogs.stream()
.collect(Collectors.groupingBy(d -> d.map.get("Breed")));
The great answer above can further be improved by method reference notation:
List<Dog> dogs = ...
Map<String, List<Dog>> map = dogs.stream()
.collect(Collectors.groupingBy(Dog::getBreed));
Convert a list of maps to a map of lists using Java Streams - Stack Overflow
Need help with creating a map from a list with stream
lambda - Java 8 List<V> into Map<K, V> - Stack Overflow
hashmap - Java 8 streams: iterate over Map of Lists - Stack Overflow
E.g. like this, relying on the collect stage of java streams.
List<Map<String, Object>> data = new ArrayList<>();
HashMap<String, List<Object>> transposed = data.stream()
.flatMap(i -> i.entrySet().stream())
.collect(Collectors.groupingBy(
Map.Entry::getKey,
HashMap::new,
Collectors.mapping(Map.Entry::getValue,
Collectors.toCollection(ArrayList::new))));
There are a number of such application on SO (e.g. this one), though not precisely tailored to this example.
You could do it without streams, yet in a modern fashion, as follows:
Map<String, List<Object>> result = new HashMap<>();
data.forEach(element ->
element.forEach((k, v) - >
result.computeIfAbsent(k, x -> new ArrayList<>()).add(v)));
This iterates the list and then each map and uses Map.computeIfAbsent to accomodate entries in the result map.
I have a list of objects (let's say object has string a, string b, string c ) need to convert it into map with key as a+b and value as entire object. Is this possible? Please help.
I m trying like this syntax: list.stream().collect(Collectors.toMap(key, value)); But not able to set key value as concatenation of a and b.
Based on Collectors documentation it's as simple as:
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(Choice::getName,
Function.identity()));
If your key is NOT guaranteed to be unique for all elements in the list, you should convert it to a Map<String, List<Choice>> instead of a Map<String, Choice>
Map<String, List<Choice>> result =
choices.stream().collect(Collectors.groupingBy(Choice::getName));
Map<String, List<String>> result = anotherHashMap
.entrySet().stream() // Stream over entry set
.collect(Collectors.toMap( // Collect final result map
Map.Entry::getKey, // Key mapping is the same
e -> e.getValue().stream() // Stream over list
.sorted(Comparator.comparingLong(MyObject::getPriority)) // Sort by priority
.map(MyObject::getName) // Apply mapping to MyObject
.collect(Collectors.toList())) // Collect mapping into list
);
Essentially, you stream over each entry set and collect it into a new map. To compute the value in the new map, you stream over the List<MyOjbect> from the old map, sort, and apply a mapping and collection function to it. In this case I used MyObject::getName as the mapping and collected the resulting names into a list.
For generating another map, we can have something like following:
HashMap<String, List<String>> result = anotherHashMap.entrySet().stream().collect(Collectors.toMap(elem -> elem.getKey(), elem -> elem.getValue() // can further process it);
Above I am recreating the map again, but you can process the key or the value according to your needs.