I have a method that takes in a Stream of map and should return a TreeMap
public TreeMap<String, String> buildTreeMap(Stream<Map<String, String>> inStream) {
   return stream.collect(toMap(???));
}
How can I make it return a TreeMap?
stream.collect(TreeMap::new, TreeMap::putAll, 
    (map1, map2) -> { map1.putAll(map2); return map1; });
...assuming you want to combine all the maps into one big map.
If you want different semantics for merging values for the same key, do something like
stream.flatMap(map -> map.entrySet().stream())
   .collect(toMap(
       Entry::getKey, Entry::getValue, (v1, v2) -> merge(v1, v2), TreeMap::new));
                        Incase you're using a groupingBy,
 stream()
   .collect(
      Collectors.groupingBy(
        e -> e.hashCode(), TreeMap::new, Collectors.toList()))
where e -> e.hashCode is key function like Entry::getKey, Student::getId
and Collectors.toList() is downstream 
i.e what datatype you need as value in the tree map
This yields TreeMap<Integer, List>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With