如何在java中转换列表
我想把我的名单转过来 假设我有学生名单如何在java中转换列表,java,arraylist,data-structures,collections,Java,Arraylist,Data Structures,Collections,我想把我的名单转过来 假设我有学生名单 List<Student> studentList = new ArrayList<Student>(); studentList.add(new Student(101, "English", "95")); studentList.add(new Student(101, "Maths", "82")); studentList.add(new Student(101, "Biology", "93")); studentLis
List<Student> studentList = new ArrayList<Student>();
studentList.add(new Student(101, "English", "95"));
studentList.add(new Student(101, "Maths", "82"));
studentList.add(new Student(101, "Biology", "93"));
studentList.add(new Student(101, "Physics", "77"));
studentList.add(new Student(101, "Chemistry", "65"));
studentList.add(new Student(102, "English", "86"));
studentList.add(new Student(102, "Maths", "75"));
studentList.add(new Student(102, "Biology", "68"));
studentList.add(new Student(102, "Physics", "63"));
studentList.add(new Student(102, "Chemistry", "84"));
studentList.add(new Student(103, "English", "92"));
studentList.add(new Student(103, "Maths", "88"));
studentList.add(new Student(103, "Biology", "67"));
studentList.add(new Student(103, "Physics", "81"));
studentList.add(new Student(103, "Chemistry", "93"));
public class Student {
private Integer rollNo;
private String subject;
private String marks;
..........
}
我必须使用什么集合来转换我的列表
我试图使用
HashMap
(整数是RollNo,对象是另一个hashmap)
HashMap
(字符串为主题,另一个字符串为标记)
使用此选项,我将studentlist转换为studentResultlist
有人建议我,有没有更好的方法来转换列表?没有隐式/自动的方法 您必须坐下来写下迭代studentList的代码,并收集构建另一个列表所需的信息 记住:基本上你有两个物体;一是代表学生课堂的对象;另一个用于保存StudentResult类的对象。但计算机不知道应该收集一个类的哪些属性来构建第二个类的对象,除非您坐下来提供这些信息
换句话说:如果您要查看数据库,则可以运行查询和其他操作,以在数据上构建不同的视图。但是没有数据库,所以所有在现有数据上构建不同视图的逻辑。。。必须手动编程 你为什么需要这些额外的物品?为什么你不能循环遍历学生并将其添加到studentresult中呢?@ergonaut在Studentclass英语、物理等学科中是学科领域的价值观。在StudentResult类中,它们是字段。我们无法直接迭代student类并将其添加到studentresult classI尝试使用HashMap
List<StudentResult> studentResultList = new ArrayList<StudentResult>();
public class StudentResult {
private String rollno;
private String english;
private String maths;
private String biology;
private String physics;
private String chemistry;
..........
}
101 102 103
English 95 86 92
Maths 82 75 88
Biology 93 68 67
Physics 77 63 81
Chemistry 65 84 93