Based on Collectors documentation it's as simple as:
Map<String, Choice> result =
choices.stream().collect(Collectors.toMap(Choice::getName,
Function.identity()));
Answer from zapl on Stack Overflowlambda - Java 8 List<V> into Map<K, V> - Stack Overflow
collections - Java 8 stream map to list of keys sorted by values - Stack Overflow
Convert a list of maps to a map of lists using Java Streams - Stack Overflow
collections - Java 8 streams - collect and stream map of lists - Stack Overflow
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));
You say you want to sort by value, but you don't have that in your code. Pass a lambda (or method reference) to sorted to tell it how you want to sort.
And you want to get the keys; use map to transform entries to keys.
List<Type> types = countByType.entrySet().stream()
.sorted(Comparator.comparing(Map.Entry::getValue))
.map(Map.Entry::getKey)
.collect(Collectors.toList());
You have to sort with a custom comparator based on the value of the entry. Then select all the keys before collecting
countByType.entrySet()
.stream()
.sorted((e1, e2) -> e1.getValue().compareTo(e2.getValue())) // custom Comparator
.map(e -> e.getKey())
.collect(Collectors.toList());
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.
It looks like all you need is flatMap the values of the original Map and then continue processing as you currently do:
Collection< PublicationSession > wantedPublications =
effectiveDatePublicationMap.values() // Collection<List<PublicationSession>>
.stream() // Stream<List<PublicationSession>>
.flatMap(list->list.stream()) // Stream<PublicationSession>
.filter(pub -> PublicationStatus.valueOf(pub.getPublishStatus()) == PublicationStatus.COMPLETE)
.sorted(Comparator.comparing(PublicationSession::getCreateTime))
.collect(toMap(p -> p.getPublicationSession().getEffDateTime(), UnaryOperator.identity(), PICK_LATEST))
.values();
To obtain the same result of your iterative code
List< PublicationSession> publicationsToFilter = new ArrayList<>();
for (List< PublicationSession> sessions : effectiveDatePublicationMap.values()) {
publicationsToFilter.addAll(sessions);
}
with streams you first have to use flatMap to transoform your stream of List<PublicationSession> in a stream of PublicationSession, and then collect all of them in a list with collect(Collectors.toList())
List<PublicationSession> publicationsToFilter = effectiveDatePublicationMap.values()
.flatMap(Collection::stream)
.collect(Collectors.toList());
Before collecting the result, you can filter or sort as you wish.
Just make sure array has unique titles and then use Collectors.toMap
Map<String, List<String>> result = Arrays.stream(myStringArray)
.map(title -> new AbstractMap.SimpleEntry<>(title,
pages.stream().filter(page -> isPageContainsTitle(page,title)).collect(Collectors.toList())))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
And as @Naman suggested you can avoid the intermediate map operation
Map<String, List<String>> result = Arrays.stream(myStringArray)
.collect(Collectors.toMap(Function.identity()
,title -> pages.stream()
.filter(page -> isPageContainsTitle(page, title))
.collect(Collectors.toList())));
Below code should work for you:
package com.shree.test;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class StreamTest {
public static void main(String[] args) {
String[] myStringArray = {"TITLE1", "TITLE2", "TITLE3"};
List<String> pages = new ArrayList<>();
pages.add("TEXT,TITLE2,TEXT,ONE,TWO");
pages.add("TEXT,TITLE1,TEXT,ONE,TWO");
pages.add("TEXT,TITLE2,TEXT,ONE,TWO");
pages.add("TEXT,TITLE2,TEXT,TITLE3,TWO");
pages.add("TEXT,TITLE3,TEXT,ONE,TWO");
Map<Object, Object> titleMap = getTitleMap(myStringArray, pages);
titleMap.entrySet().forEach(System.out::println);
}
private static Map<Object, Object> getTitleMap(String[] myStringArray, List<String> pages) {
Map<Object, Object> titleMap = Arrays.asList(myStringArray).stream()
.map(
(title)->{
return new AbstractMap.SimpleEntry<>(title, pages.stream()
.filter((page)->page.contains(title))
.collect(Collectors.toList()));
}
)
.collect(Collectors.toMap(entry->entry.getKey(), entry->entry.getValue()));
return titleMap;
}
}
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")));
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));
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.