@Test
void test1() {
ArrayList<People> peopleList = Lists.newArrayList();
peopleList.add(new People(1, "小王", 1));
peopleList.add(new People(3, "小李", 3));
peopleList.add(new People(2, "小张", 2));
peopleList.add(new People(4, "小皇", 4));
People people = peopleList.stream().filter(c -> c.getJgid() % 2 == 0).findFirst().orElse(null);
System.out.println(people);
Stream
的findFirst
方法在此流中查找第一个元素作为 Optional
。
如果流中没有元素, findFirst
返回空的 Optional
。
如果流没有顺序,那么 findFirst
可以选择任何元素。
如果 findFirst
选择的元素为null,它将抛出 NullPointerException
。