编程语言
首页 > 编程语言> > java8 stream的使用心得

java8 stream的使用心得

作者:互联网

首先,我们一起看看stream的层次体系关系:

在这里插入图片描述
一般我们直接使用Strem,比如我们需要从一个指定的字符串数组中,查找指定的字符串是否存在
未使用stream的时候:
import org.junit.Test;

import java.util.*;
/**

输出:
存在

使用steam:
@Test
public void testFindStrByStream() {
String findStr = “b”;
List stringList = Arrays.asList(“a”, “b”, “c”, “d”, “e”);
boolean result = stringList.stream().anyMatch(x -> x.equals(findStr));
System.out.println(result);
}
输出:
存在

stream的操作符主要分为中间操作符和终止操作符
中间操作:
1.filter
过滤数据,保留 boolean 为 true 的元素,返回一个集合

标签:findStr,String,stream,stringList,Test,心得,java8,match
来源: https://blog.csdn.net/nikiliqi8/article/details/119223049