当前位置:网站首页>Stream通过findFirst()查找满足条件的一条数据
Stream通过findFirst()查找满足条件的一条数据
2022-08-10 12:52:00 【李长渊哦】
Stream通过findFirst查找满足条件的一条数据
一、Stream通过findFirst()查找满足条件的一条数据
1、案例
如果取得第一个元素,则用findFirst()
最后提取元素的时候,可以用:get或者orElse(null)
这里要注意的是,规范用法是orElse(null)
@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);
}
2、其他
Stream
的findFirst
方法在此流中查找第一个元素作为 Optional
。
如果流中没有元素, findFirst
返回空的 Optional
。
如果流没有顺序,那么 findFirst
可以选择任何元素。
如果 findFirst
选择的元素为null,它将抛出 NullPointerException
。
package com.concretepage;
import java.util.stream.Stream;
public class FindFirstDemo4 {
public static void main(String[] args) {
Stream.of(null, "A").
findFirst().ifPresent(s -> System.out.println(s));
}
}
输出将为NullPointerException。
边栏推荐
- Keithley DMM7510精准测量超低功耗设备各种运作模式功耗
- 娄底植物细胞实验室建设基本组成要点
- 需要被记录的OpenStack相关的命令_自己用
- rpn:def concat_box_prediction_layers
- M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
- Import other custom namespaces in C#
- Short read or OOM loading DB. Unrecoverable error, aborting now
- Drive IT Modernization with Low Code
- BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection 论文笔记
- 娄底农产品检验实验室建设指南盘点
猜你喜欢
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer Paper Notes
bgp双平面实验 路由策略控制流量
M²BEV: Multi-Camera Joint 3D Detection and Segmentation with Unified Bird’s-Eye View Representation
[Study Notes] Persistence of Redis
商汤自研机械臂,首款产品是AI下棋机器人:还请郭晶晶作代言
LeetCode·每日一题·640.求解方程·模拟构造
Detailed explanation of es6-promise object
Reversing words in a string in LeetCode
Basic knowledge of switches
神了!阿里数据库专家纯手写了这份604页的Oracle+MySQL攻坚指南
随机推荐
【数字IC验证进阶】SoC系统验证和IP模块验证的区别及侧重点分析
Keithley DMM7510 accurate measurement of ultra-low power consumption equipment all kinds of operation mode power consumption
需要被记录的OpenStack相关的命令_自己用
DNS欺骗-教程详解
Educational Codeforces Round 41 (Rated for Div. 2) E. Tufurama
Efficient and Robust 2D-to-BEV Representation Learning via Geometry-guided Kernel Transformer 论文笔记
G1和CMS的三色标记法及漏标问题
Codeforces Round #276 (Div. 1) B. Maximum Value
中科院深圳先进技术院合成所赵国屏院士组2022年招聘启事
MYSQL误删数据恢复
接口自动化测试基础篇
想问下大佬们 ,cdc oracle初始化一张300万的表任务运行着后面就这个错 怎么解决哇
Reversing words in a string in LeetCode
教育Codeforces轮41(额定Div。2)大肠Tufurama
CodeForces-834C
[Study Notes] Persistence of Redis
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
bgp dual plane experiment routing strategy to control traffic
BEVDet4D: Exploit Temporal Cues in Multi-camera 3D Object Detection Paper Notes
11+ chrome高级调试技巧,学会效率直接提升666%