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

后端 未结 4 790
盖世英雄少女心
盖世英雄少女心 2021-01-12 16:02

We have a Student class as follows:

class Student {
    private int marks;
    private String studentName;

    public int getMarks() {
        r         


        
4条回答
  •  梦谈多话
    2021-01-12 17:08

    An alternative solution is to use groupingBy with summingInt:

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

提交回复
热议问题