ストリームを使用して、型Bから型Aのコレクションを作成します。Java 8ストリームA型のコレクションから型Bのコレクションを作成します。
私はコード
現在public static List<Person> createPersonsFromEmployees(List<Employee> employees) {
List<Person> persons = new ArrayList<>();
employees.stream().filter(Object :: nonNull)
.forEach(e -> {
persons.add(new Person(e.getFirstName(),
e.getLastName(),
e.getEmail());
};)
return persons;
}
、コードの作品のこの部分以下の書いてきました、私は従業員のコレクションから人のコレクションを作成するには、二つのクラス
Class Employee{
String firstName;
String lastName;
int age;
String id;
String email;
double salary;
}
Class Person {
String firstName;
String lastName;
String email;
}
があるとします。しかし、forEach
を使わずにEmployee
からPerson
のコレクションを作成するより良い方法があるのかどうか疑問に思っています。
多くの説明をありがとう。 – cmodha