如何使用 Java 8 Stream 从某些 class 属性中获取列表?
How can I get a List from some class properties with Java 8 Stream?
我有一个List<Person>
。我需要从 Person
的 属性 中得到 List
。
比如我有一个Person
class:
class Person
{
private String name;
private String birthDate;
public String getName() {
return name;
}
public String getBirthDate() {
return birthDate;
}
Person(String name) {
this.name = name;
}
}
List<Person> personList = new ArrayList<>();
personList.add(new Person("David"));
personList.add(new Person("Joe"));
personList.add(new Person("Michel"));
personList.add(new Person("Barak"));
我想获取带有 Stream
API 的姓名列表,如下所示:
List<String> names = personList.stream().somecode().collect(Collectors.toList());
names.stream().forEach(System.out::println);
#David
#Joe
#Michel
#Barak
此代码无效:
public class Main
{
public static void main(String[] args)
{
List<Person> personList = new ArrayList<>();
Person person = new Person("Иван");
person.getFriends().addAll(Arrays.asList("Друг 1", "Друг 2", "Друг 3"));
personList.add(person);
person = new Person("Федор");
person.getFriends().addAll(Arrays.asList("Друг 4", "Друг 5", "Друг 6"));
personList.add(person);
person = new Person("Алексей");
person.getFriends().addAll(Arrays.asList("Друг 7", "Друг 8", "Друг 9"));
personList.add(person);
person = new Person("Константин");
person.getFriends().addAll(Arrays.asList("Друг 10", "Друг 11", "Друг 12"));
List<String> friens = personList.stream().map(e->e.getFriends()).collect(Collectors.toList());
friends.stream().forEach(System.out::println);
//Друг 1
//Друг 2
//Друг 3
//Друг 4
//...
}
}
class Person
{
String name;
List<String> friends;
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<String> getFriends() {
return friends;
}
}
我怎样才能得到 List
的 属性 和 Stream
API?
您可以使用 map
:
List<String> names =
personList.stream()
.map(Person::getName)
.collect(Collectors.toList());
编辑:
为了合并好友名称列表,您需要使用flatMap
:
List<String> friendNames =
personList.stream()
.flatMap(e->e.getFriends().stream())
.collect(Collectors.toList());
我有一个List<Person>
。我需要从 Person
的 属性 中得到 List
。
比如我有一个Person
class:
class Person
{
private String name;
private String birthDate;
public String getName() {
return name;
}
public String getBirthDate() {
return birthDate;
}
Person(String name) {
this.name = name;
}
}
List<Person> personList = new ArrayList<>();
personList.add(new Person("David"));
personList.add(new Person("Joe"));
personList.add(new Person("Michel"));
personList.add(new Person("Barak"));
我想获取带有 Stream
API 的姓名列表,如下所示:
List<String> names = personList.stream().somecode().collect(Collectors.toList());
names.stream().forEach(System.out::println);
#David
#Joe
#Michel
#Barak
此代码无效:
public class Main
{
public static void main(String[] args)
{
List<Person> personList = new ArrayList<>();
Person person = new Person("Иван");
person.getFriends().addAll(Arrays.asList("Друг 1", "Друг 2", "Друг 3"));
personList.add(person);
person = new Person("Федор");
person.getFriends().addAll(Arrays.asList("Друг 4", "Друг 5", "Друг 6"));
personList.add(person);
person = new Person("Алексей");
person.getFriends().addAll(Arrays.asList("Друг 7", "Друг 8", "Друг 9"));
personList.add(person);
person = new Person("Константин");
person.getFriends().addAll(Arrays.asList("Друг 10", "Друг 11", "Друг 12"));
List<String> friens = personList.stream().map(e->e.getFriends()).collect(Collectors.toList());
friends.stream().forEach(System.out::println);
//Друг 1
//Друг 2
//Друг 3
//Друг 4
//...
}
}
class Person
{
String name;
List<String> friends;
Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<String> getFriends() {
return friends;
}
}
我怎样才能得到 List
的 属性 和 Stream
API?
您可以使用 map
:
List<String> names =
personList.stream()
.map(Person::getName)
.collect(Collectors.toList());
编辑:
为了合并好友名称列表,您需要使用flatMap
:
List<String> friendNames =
personList.stream()
.flatMap(e->e.getFriends().stream())
.collect(Collectors.toList());