collecting HashMap<String, List<String>> java 8
You can use the groupingBy
method to manage aggregation, for example:
public static void main(String[] args) { List<String> list = Arrays.asList("A", "B", "C", "D", "A"); Map<String, List<String>> map = list.stream().collect(Collectors.groupingBy(Function.identity()));}
If you want more flexibility (for example to map the value and return a Set instead of a List) you can always use the groupingBy
method with more parameters as specified in javadoc:
Map<City, Set<String>> namesByCity = people.stream().collect(Collectors.groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));
Functions key
and value
you have defined in your code are not correct because they should operate on the elements of your list, and your elements are not Map
s.
The following code works for me:
List<String> list = Arrays.asList("A", "B", "C", "D");Map<String, List<String>> map = list.stream() .collect(Collectors.toMap(Function.identity(), Arrays::asList));
First argument to Collectors.toMap
defines how to make a key from the list element (leaving it as is), second argument defines how to make a value (making an ArrayList
with a single element).
Thanks for all the help guys! @izstas "they should operate on the elements" helped a lot :). Actually this is what I was looking for to be exact
public static void test2 (){ Function<Entry<String, List<String>>, String> key = (entry) -> { return entry.getKey(); }; Function<Entry<String, List<String>>, List<String>> value = (entry) -> { return new ArrayList<String>(entry.getValue()); }; BinaryOperator<List<String>> merge = (old, latest)->{ old.addAll(latest); return old; }; Map<String, List<String>> map1 = new HashMap<>(); map1.put("A", Arrays.asList("A1", "A2")); map1.put("B", Arrays.asList("B1")); map1.put("D", Arrays.asList("D1")); Map<String, List<String>> map2 = new HashMap<>(); map2.put("C", Arrays.asList("C1","C2")); map2.put("D", Arrays.asList("D2")); Stream<Map<String, List<String>>> stream =Stream.of(map1, map2); System.out.println(stream.flatMap((map)->{ return map.entrySet().stream(); }).collect(Collectors.toMap(key, value, merge)));}