簡體   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