我在StudentService中有List
private List<Student> students = new ArrayList<>(Arrays.asList( new Student("1", "Lukasz", "Nowak", "Cicha 3", "1a"), new Student("2", "Tomasz", "Tomczyk", "Krakowska 13a", "1a"), new Student("3", "Grzegorz", "Adamiak", "Podkarpacka 8", "2b"), new Student("4", "Klaudia", "Kurcz", "Warszawska 13", "2b")));
我希望将一些元素复制到PresentService并添加一个礼物
我试着这样做
List<Presents> presents = new ArrayList<Student>()
但我只得到错误
答案 0 :(得分:0)
List<Presents> presents = new ArrayList<Student>()
此声明无法编译。
您的变量定义期望Presents类型的对象,但您给它一个Student列表。
最简单的解决方案是
List<Presents> presents = students.stream().map((student) -> new Present(student)).collect(Collectors.toList());
在Presents类
中创建一个构造函数public Presents(Student student) {
//Create your present object as you would want to here
//For example if you wanted name
this.name = student.getName();
}