繁体   English   中英

如何将列表从一项服务复制到另一项(Spring Boot)

[英]how to copy List from one Service to another (Spring Boot)

我在StudentService中有清单

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>()但我只有错误

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM