Stream grouping by sum of determinate objects
Stream grouping by sum of determinate objects I have a Request class like this : public class Request { String name,destName; int nSeats; //Example : requestOne,Paris,3 ... } I want to group the request in a Map |Integer,List of String| where the keys are the sum of the request with the same destName and the values are the destinations' names. Here is my code: public TreeMap<Integer, List<String>> destinationsPerNSeats() { return requests. stream(). collect(Collectors.groupingBy(Request::getnSeats, TreeMap::new, Collectors.mapping(Request::getDestName, Collectors.toList()))). } Input : TreeMap<Integer, List<String>> map = mgr.destinationsPerNSeats(); print(map); Output : {4=[Paris], 3=[London, Berlin, Berlin], 2=[Paris]} Output expected : {6=[Berlin, Paris], 3=[London]} How can I solve this? Thanks! What did you use as Input? – Glains 8 mins ago ...
