Java 设置<;字符串>;来自ArrayList<;MyObject>;

Java 设置<;字符串>;来自ArrayList<;MyObject>;,java,Java,我有一个School类,它有一个名称String字段 public class School { private final String name; public School(String name) { this.name = name; } // getter and setter ... } 我得到了School实例的ArrayList: List<School> shoolList = // Got value of Arra

我有一个
School
类,它有一个名称
String
字段

public class School {
   private final String name;

   public School(String name) {
       this.name = name;
   }

   // getter and setter ...

}
我得到了
School
实例的
ArrayList

List<School> shoolList = // Got value of ArrayList<School>

但是,有没有一种简单的方法可以从上述结果中获取
集合
类型结果?

使用
收集器

Set<String> allNames = schoolList.stream()
    .map(School::getName)
    .collect(Collectors.toSet());
Set allNames=schoolList.stream()
.map(学校::getName)
.collect(收集器.toSet());
有关详细信息,请参阅:

Set<String> allNames = schoolList.stream()
    .map(School::getName)
    .collect(Collectors.toSet());