我有这样的POJO。另外,我有学生名单,我想与登记在他schoolname文本流进行过滤santa
。在SQL它会是这样的:LIKE %SANTA%
public class Student {
private String name;
private String lastName;
private String city;
private String schoolname;
private String telephone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getSchoolname() {
return schoolname;
}
public void setSchoolname(String schoolname) {
this.schoolname = schoolname;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
而且我想以获得他的同学schoolname
包含字符串santa
,你知道我怎么能有做流过滤器?
我需要流做,我不能用if和else语句做到这一点。
流过List
,并应用filter()
该检查是否的校名Student
中含有“圣诞老人”。
List<School> input = ...
List<School> output =
input.stream()
.filter(s -> s.getSchoolname().contains("SANTA"))
.collect(Collectors.toList());
如果你想这个过滤器不区分大小写,你可以写
.filter(s -> s.getSchoolname().toUpperCase().contains("SANTA"))
如果你想既List
包含“圣诞老人”和List
不包含它,你可以使用Collectors.partitioningBy()
:
List<School> input = ...
Map<Boolean,List<School>> output =
input.stream()
.collect(Collectors.partitioningBy(s -> s.getSchoolname().contains("SANTA")));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句