java lambda表达式检查list集合是否存在某个值
import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class Test { public static void main(String[] args) { People p1 = new People("b", "b"); People p2 = new People("a", "b11"); List<People> list = new ArrayList<People>(); list.add(p1); list.add(p2); boolean bool = list.stream().anyMatch(a -> a.getName().equals("a") || a.getNick().equals("a")); System.out.println(bool); List<People> data = list.stream().filter(a -> a.getName().equals("a") || a.getNick().equals("a")) .collect(Collectors.toList()); if(data!=null && data.size() > 0) { for (int i = 0; i < data.size(); i++) { System.out.println(data.get(i).getName() + "---" + data.get(i).getNick()); } } } }
public class People { private String name; private String nick; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNick() { return nick; } public void setNick(String nick) { this.nick = nick; } public People(String name, String nick) { super(); this.name = name; this.nick = nick; } }
看完打开支付宝扫一扫领个红包吧!