I am new to Java and I am trying to merge multiple maps with string as key and list as values to produce a new Map.
public class Student{ private String name; private String country; //Setters and Getters }
Now I have an util class to add students to the list based on their country.
public class MapAdder { static Map<String,List<Student>> studentMap = new LinkedHashMap<String,List<Student>>(); public static void addToMap(String key,Student student) { studentMap.computeIfAbsent( key , k->new LinkedList<Student>()).add(student); } public static Map<String, List<Student>> getStudentMap() { return studentMap; } public static void clearStudentMap() { studentMap.clear(); } }
Main Method
public static void main(String[] args) { Map<String,List<Student>> studentMap1 ; Map<String,List<Student>> studentMap2 ; Map<String,List<Student>> studentMap3 ; MapAdder.addToMap("India", new Student("Mounish","India") ); MapAdder.addToMap("USA", new Student("Zen","USA") ); MapAdder.addToMap("India", new Student("Ram","India") ); MapAdder.addToMap("USA", new Student("Ronon","USA") ); MapAdder.addToMap("UK", new Student("Tony","UK") ); studentMap1 = MapAdder.getStudentMap(); MapAdder.clearStudentMap(); MapAdder.addToMap("India", new Student("Rivar","India") ); MapAdder.addToMap("UK", new Student("Loki","UK") ); MapAdder.addToMap("UK", new Student("Imran","UK") ); MapAdder.addToMap("USA", new Student("ryan","USA") ); studentMap2= MapAdder.getStudentMap(); MapAdder.clearStudentMap(); Map<String,List<Student>>map3 = Stream.of(studentMap1, studentMap2) .flatMap(map -> map.entrySet().stream()) .collect(Collectors.toMap( Entry::getKey, Entry::getValue ));
But when I try to merge both the maps I am getting empty map. Actually, I need to have a map with three keys (India, UK, USA) and their values that are list from multiple maps to be merged w.r.t keys.
https://stackoverflow.com/questions/65428163/how-to-combine-multiple-maps-with-multiple-same-keys-and-lists-as-values December 24, 2020 at 01:08AM
没有评论:
发表评论