Transform a List<Object> to a Map<String,Integer> such that the String is not a duplicate value using Java 8 Streams

  • A+
Category:Languages

We have a Student class as follows:

class Student{     private int marks;     private String studentName;      public int getMarks() {         return marks;     }      public void setMarks(int marks) {         this.marks = marks;     }      public String getStudentName() {         return studentName;     }      public void setStudentName(String studentName) {         this.studentName = studentName;     }      public Student(String studentName, int marks) {         this.marks = marks;         this.studentName = studentName;     }  } 

We have a LIST of Students as follows :

List<Student> studentList = new ArrayList<>(); studentList.add(new Student("abc", 30)); studentList.add(new Student("Abc", 32)); studentList.add(new Student("ABC", 35)); studentList.add(new Student("DEF", 40)); 

This List needs to be converted into a HashMap<String,Integer> such that:

  1. the map does not contain any duplicate Student
  2. if a duplicate student name is found, his marks shall be added with the previous occurrence.

So the output should be : {ABC = 67, DEF = 40}


I have tried to solve this issue as follows:

Map<String,Integer> studentMap = studentList.stream()             .collect(                     Collectors.toMap(                             student->student.getStudentName().toLowerCase(),                             student -> student.getMarks(),                             (s1,s2) -> s1,                             LinkedHashMap::new                             )             ); 

But the merge function does not allow me to concatenate the marks and this returns the output as

So the output should be : {abc= 30, DEF = 40}

Can someone suggest an efficient solution for this?

 


An alternative solution is to use groupingBy with summingInt:

Map<String, Integer> studentMap =      studentList.stream()                .collect(Collectors.groupingBy(s -> s.getStudentName().toLowerCase(),                                               Collectors.summingInt(Student::getMarks))); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: