簡體   English   中英

通過對象列表在Java 8中構建Maps

[英]build Map of Maps in java 8 from list of Objects

我需要將對象列表轉換為地圖。 我試圖根據學生對象來模擬問題陳述。 請任何人來營救。 所以問題如下:

我有一個Student對象的列表,我想將其轉換為Studentkey的Map,Value是另一個映射,其鍵為DepartId Enum,值作為離場人數。 StudentKey是Object,由StudentId和Hno組成(我需要使用一些組合作為映射的鍵)。 出發的是枚舉。 我已經使用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());
                }
            }

        }
    }

謝謝@霍爾格,它奏效了。 這是我的代碼。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM