从对象列表中构建 java 8 个地图中的地图
build Map of Maps in java 8 from list of Objects
我需要将对象列表转换为地图中的地图。我试图根据学生对象来模拟问题陈述。请任何人来救援。所以问题如下:
我有一个 Student 对象列表,我想将其转换为 Studentkey 的 Map,Value 是另一个 map,键为 DepartId 枚举,值为 departents 的计数。 StudentKey 是一个由 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());
}
}
}
}
谢谢@Holger,它成功了。这是我的代码。
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;
}
我需要将对象列表转换为地图中的地图。我试图根据学生对象来模拟问题陈述。请任何人来救援。所以问题如下:
我有一个 Student 对象列表,我想将其转换为 Studentkey 的 Map,Value 是另一个 map,键为 DepartId 枚举,值为 departents 的计数。 StudentKey 是一个由 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());
}
}
}
}
谢谢@Holger,它成功了。这是我的代码。
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;
}