从对象列表中构建java 8中的地图

时间:2016-07-11 18:01:49

标签: list lambda java-8 maps

我需要将对象列表转换为地图地图。我试图用学生对象模拟问题陈述。请救援任何人。所以问题如下:

我有一个Student对象列表,我希望将其转换为Studentkey的Map,Value是另一个带有密钥DepartId Enum的地图,值是离开者的数量。 StudentKey是由studentId和Hno组成的对象(我需要将某些组合用作映射的键​​)。 Departent是Enum。我使用java7创建它,但在Java 8中无法创建它。任何人都可以帮助

public class Student {

    private int Id;
    private String name;
    private Department dept;
    private int hno;
    ...........
    }

public enum Department {
ARTS,SCIENCE,MATHS,MUSIC
}

public class StudentKey {
    private int Id;
    private int hno;
    .......
    }

    public class TestStudent {
        public static void main(String[] args) {
            Student st1= new Student(1, "Alex", Department.ARTS, 23);
            Student st2= new Student(1, "Alex", Department.ARTS, 23);
            Student st3= new Student(1, "Alex", Department.SCIENCE, 24);
            Student st4= new Student(1, "Alex", Department.SCIENCE, 24);
            Student st5= new Student(2, "John", Department.MUSIC, 25);
            Student st6= new Student(2, "John", Department.MATHS, 26);
            Student st7= new Student(2, "John", Department.MATHS, 26);
            Student st8= new Student(3, "Doe", Department.MUSIC, 25);
            Student st9= new Student(3, "Doe", Department.MATHS, 67);

            List<Student> list = new ArrayList<>();
            list.add(st1);
            list.add(st2);
            list.add(st3);
            list.add(st4);
            list.add(st5);
            list.add(st6);
            list.add(st7);
            list.add(st8);
            list.add(st9);

            //buildMapinJava8(list);

            Map<StudentKey, Map<Department, Long>> resultMap =buildMapinJava7(list);
            printMap(resultMap);
        }



        private static Map<StudentKey, Map<Department, Long>>  buildMapinJava7(List<Student> list) {
            Map<StudentKey, Map<Department, Long>> resultMap = new HashMap<>();
            for (Student student : list) {
                StudentKey newKey = new StudentKey(student.getId(), student.getAge());
                Map<Department, Long> deptMap=  resultMap.get(newKey);
                if(deptMap==null){
                    deptMap = new HashMap<>();
                    resultMap.put(newKey, deptMap);
                }
                Long count =deptMap.get(student.getDept());
                count = count!=null?count:0;
                deptMap.put(student.getDept(), count+1);
            }
            return resultMap;
        }

        private static Map<StudentKey, Map<Department, Long>>  buildMapinJava8(List<Student> list) {
            list.stream().collect(Collectors.toMap(new Function<Student, StudentKey>() {

                @Override
                public StudentKey apply(Student t) {

                    return new StudentKey(t.getId(), t.getAge());
                }
            }, new Function<Student, Map<Department, Long>>() {

                @Override
                public Map<Department, Long> apply(Student t) {
                    return null;//??? how to do here
                }
            }));

        }
private static void printMap(Map<StudentKey, Map<Department, Long>> resultMap) {
            for (Entry<StudentKey, Map<Department, Long>> entry : resultMap.entrySet()) {
                System.out.print(entry.getKey().getId()+" , "+entry.getKey().getAge()+" ");

                for(Entry<Department, Long> dept :entry.getValue().entrySet()){
                System.out.println("department : "+dept.getKey()+" :  "+dept.getValue());
                }
            }

        }
    }

1 个答案:

答案 0 :(得分:0)

Thanks @Holger,It worked. here is my code.

private static Map<StudentKey, Map<Department, Long>> buildMapInJava8(List<Student> list) 
{
    Map<StudentKey,Map<Department,Long>> map = list.stream()
      .collect(Collectors.groupingBy(t -> new StudentKey(t.getId(), t.getAge()),
               Collectors.groupingBy(Student::getDept, Collectors.counting())));

    return map;
}