gourabゴーシュ:
要件は、収集されたStringのセットで唯一の社員コード、いくつかの基準にオブジェクトの設定、フィルタをストリーミングすることで、
class Employee {
private String empId;
private int type;
public int getType() {
return type;
}
public String getEmpId() {
return empId;
}
}
次の文をフィルタリングしながら、
employees.stream().filter(x-> x.getType() == 1).collect(Collectors.toSet());
リターンSet<Employee>
の代わりに私がしたいだけでEMPIDすなわち収集しますSet<String>
注:プライベート以外のインスタンス変数の何かを作ることはできません。
彼らは次のとおりでした:
使用map()
マッピングするためにEmployee
、対応する従業員のIDにインスタンスを。
Set<String> empIds =
employees.stream()
.filter(x-> x.getType() == 1)
.map(Employee::getEmpId)
.collect(Collectors.toSet());