创建实体类Person
public class Person {
private Integer age;
private String name;
private String enName;
public Person(Integer age, String name,String enName) {
this.age = age;
this.name = name;
this.enName =enName;
}
public Person() {
}
public Integer getAge() {
return age;
}
public Person setAge(Integer age) {
this.age = age;
return this;
}
public String getName() {
return name;
}
public Person setName(String name) {
this.name = name;
return this;
}
public String getEnName() {
return enName;
}
public void setEnName(String enName) {
this.enName = enName;
}
@Override
public String toString() {
return "Person [age=" + age + ", name=" + name + ", enName=" + enName + "]";
}
}
测试类
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class TestStreamFilter {
public static void main(String[] args) {
String nameStr = "o";
List<Person> people = new ArrayList<>();
Person person1 = new Person();
person1.setName("张三");
person1.setEnName("Bob");
person1.setAge(18);
Person person2 = new Person();
person2.setName("李四");
person2.setEnName("Jonson");
person2.setAge(15);
Person person3 = new Person();
person3.setName("王五");
person3.setEnName("DaMi");
person3.setAge(16);
people.add(person1);
people.add(person2);
people.add(person3);
System.out.println("people:" + people.toString());
List<Person> nameList = null;
try {
nameList = people.stream()
.filter(p -> p.getName().contains(nameStr)
|| p.getEnName().contains(nameStr) )
.collect(Collectors.toList());
nameList.forEach(System.out::println);
System.out.println("nameList:" + nameList);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("e"+e);
}
}
}
打印结果如下:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/100487.html