首页 > 其他分享 >《数据篇》stream,流(区别于数据流)

《数据篇》stream,流(区别于数据流)

时间:2024-02-06 09:35:44浏览次数:29  
标签:stream Stream 区别 Arrays integerList List 数据流 asList

链接:https://blog.csdn.net/QiuHaoqian/article/details/120942134

1、什么是流

流是从支持数据处理操作的源生成的元素序列,源可以是数组、文件、集合、函数。流不是集合元素,它不是数据结构并不保存数据,它的主要目的在于计算。

2、如何生成流

生成流的方式主要有五种

1、通过集合生成,应用中最常用的一种

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
Stream<Integer> stream = integerList.stream();

2、通过数组生成

int[] intArr = {1, 2, 3, 4, 5, 6};
IntStream stream = Arrays.stream(intArr);

通过Arrays.stream方法生成流,并且该方法生成的流是数值流【即IntStream】而不是 Stream。补充一点使用数值流可以避免计算过程中拆箱装箱,提高性能。

Stream API提供了mapToInt、mapToDouble、mapToLong三种方式将对象流【即Stream 】转换成对应的数值流,同时提供了boxed方法将数值流转换为对象流.

3、通过值生成

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);

通过Stream的of方法生成流,通过Stream的empty方法可以生成一个空流.

4、通过文件生成

Stream<String> lines = Files.lines(Paths.get("data.txt"), Charset.defaultCharset());

通过Files.line方法得到一个流,并且得到的每个流是给定文件中的一行.

5、通过函数生成

iterator

Stream<Integer> stream = Stream.iterate(0, n -> n + 2).limit(5);

iterate方法接受两个参数,第一个为初始化值,第二个为进行的函数操作,因为iterator生成的流为无限流,通过limit方法对流进行了截断,只生成5个偶数。

generator

Stream<Double> stream = Stream.generate(Math::random).limit(5);

generate方法接受一个参数,方法参数类型为Supplier ,由它为流提供值。generate生成的流也是无限流,因此通过limit对流进行了截断。

3、流的操作类型

流的操作类型主要分为两种

3.1、中间操作

一个流可以后面跟随零个或多个中间操作。其目的主要是打开流,做出某种程度的数据映射/过滤,然后返回一个新的流,交给下一个操作使用。这类操作都是惰性化的,仅仅调用到这类方法,并没有真正开始流的遍历,真正的遍历需等到终端操作时,常见的中间操作有下面即将介绍的 filter、map 等。

3.2、终端操作

一个流有且只能有一个终端操作,当这个操作执行后,流就被关闭了,无法再被操作,因此一个流只能被遍历一次,若想在遍历需要通过源数据在生成流。终端操作的执行,才会真正开始流的遍历。如下面即将介绍的 count、collect 等。

4、流的使用

4.1 中间操作

filter 筛选
通过使用filter方法进行条件筛选,filter的方法参数为一个条件(过滤保留函数返回值为 true 的元素)。

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);

Stream<Integer> stream = integerList.stream().filter(i -> i > 3);

结果为:4,5,6

distinct 去重
通过distinct方法快速去除重复的元素。

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);

Stream<Integer> stream = integerList.stream().distinct();

limit 返回指定流个数
通过limit方法指定返回流的个数,limit的参数值必须 >=0,否则将会抛出异常。

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);

Stream<Integer> stream = integerList.stream().limit(3);

skip 跳过流中的元素
通过skip方法跳过流中的元素。

List<Integer> integerList = Arrays.asList(1, 1, 2, 3, 4, 5);

Stream<Integer> stream = integerList.stream().skip(2);

结果为: 2,3,4,5

skip的参数值必须>=0,否则将会抛出异常。

map 流映射
所谓流映射就是将接受的元素映射成另外一个元素。

List<String> stringList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

List<Integer> collect = stringList.stream()
		.map(String::length)
		.collect(Collectors.toList());

结果为:[6, 7, 2, 6]

通过map方法可以完成映射,该例子完成中 String -> Integer 的映射。

flatMap 流转换
将一个流中的每个值都转换为另一个流.

List<String> wordList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

List<String> strList = wordList.stream()
		.map(w -> w.split(" "))
		.flatMap(Arrays::stream)
		.distinct()
		.collect(Collectors.toList());

结果为:[Java, 8, Lambdas, In, Action]

map(w -> w.split(" ")) 的返回值为 Stream<String[]>,想获取 Stream,可以通过flatMap方法完成 Stream ->Stream 的转换。

allMatch 匹配所有元素

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

if (integerList.stream().allMatch(i -> i > 3)) {
	System.out.println("所有元素值都大于3");
} else {
	System.out.println("并非所有元素值都大于3");
}

anyMatch匹配其中一个

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

if (integerList.stream().anyMatch(i -> i > 3)) {
	System.out.println("存在值大于3的元素");
} else {
	System.out.println("不存在值大于3的元素");
}

等同于

for (Integer i : integerList) {
	if (i > 3) {
		System.out.println("存在大于3的值");
		break;
	}
}

noneMatch全部不匹配

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);


if (integerList.stream().noneMatch(i -> i > 3)) {
	System.out.println("值都小于3的元素");
} else {
	System.out.println("值不都小于3的元素");
}

4.2 终端操作

count 统计流中元素个数

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

Long result = integerList.stream().count();

结果为:5

findFirst 查找第一个

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

Optional<Integer> result = integerList.stream().filter(i -> i > 3).findFirst();

System.out.println(result.orElse(-1));

结果为:4

findAny 随机查找一个

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

Optional<Integer> result = integerList.stream().filter(i -> i > 3).findAny();

System.out.println(result.orElse(-1));

结果为:4

通过findAny方法查找到其中一个大于三的元素并打印,因为内部进行优化的原因,当找到第一个满足大于三的元素时就结束,该方法结果和findFirst方法结果一样。提供findAny方法是为了更好的利用并行流,findFirst方法在并行上限制更多【本篇文章将不介绍并行流】。

reduce 将流中的元素组合

用于求和:

List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5);

int sum = integerList.stream()
		.reduce(0, Integer::sum);

结果为:15

等同于

int sum = 0;
for (int i : integerList) {
	sum += i;
}

reduce接受两个参数,一个初始值这里是0,一个 BinaryOperatoraccumulator
来将两个元素结合起来产生一个新值,另外reduce方法还有一个没有初始化值的重载方法。

用于获取最大最小值

List<String> stringList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

Optional<Integer> min = stringList.stream()
		.map(String::length)
		.reduce(Integer::min);

Optional<Integer> max = stringList.stream()
		.map(String::length)
		.reduce(Integer::max);

结果为:

Optional[2] 和 Optional[7]

min/max 获取最小最大值

方法参数为 Comparator<?superT>comparator

写法1:

List<String> stringList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

Optional<Integer> min = stringList.stream()
		.map(String::length)
		.min(Integer::compareTo);

Optional<Integer> max = stringList.stream()
		.map(String::length)
		.max(Integer::compareTo);

结果为:

Optional[2] 和 Optional[7]

写法2:

List<String> stringList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

OptionalInt
		min = stringList.stream()
		.mapToInt(String::length)
		.min();

OptionalInt
		max = stringList.stream()
		.mapToInt(String::length)
		.max();

方法3:使用reduce获取最大最小值

List<String> stringList = Arrays.asList("Java 8", "Lambdas", "In", "Action");

Optional<Integer> min = stringList.stream()
		.map(String::length)
		.reduce(Integer::min);

Optional<Integer> max = stringList.stream()
		.map(String::length)
		.reduce(Integer::max);

sum / summingxxx / reduce 求和

详见链接:https://blog.csdn.net/QiuHaoqian/article/details/120942134

标签:stream,Stream,区别,Arrays,integerList,List,数据流,asList
From: https://www.cnblogs.com/fusio/p/18009141

相关文章

  • neteq的RELATIVE_ARRIVAL_DELAY,INTER_ARRIVAL_TIME的抖动计算区别
    正文neteq两种模式:RELATIVE_ARRIVAL_DELAY,INTER_ARRIVAL_TIME,两者计算抖动方式的存在区别如上图所示:RELATIVE_ARRIVAL_DELAY(简称relative_delay)的计算相邻两个点的iat_ms,然后从区间头进行累加到当前(数学展开的话,可以发现是每个包和第一个包求传输延时different)INTER_A......
  • 堆栈与堆(Stack vs Heap):有什么区别?
    编写有效的代码需要了解堆栈和堆内存,这使其成为学习编程的重要组成部分。不仅如此,新程序员或职场老手都应该完全熟悉堆栈内存和堆内存之间的区别,以便编写有效且优化的代码。这篇博文将对这两种内存分配技术进行全面的比较。通过本文的结论,我们将对堆栈和堆内存有一个透彻的了解,从而......
  • C#中Thread和Task的区别
    https://blog.csdn.net/happyjava2/article/details/131411791Thread和Task是.NET框架中用于实现多线程编程的两个重要概念。它们的主要区别如下:1、基于不同的.NET框架:Thread是基于Windows操作系统提供的API实现,而Task则是基于.NET框架提供的TPL(TaskParallelL......
  • stream流
    目录一、流程1.创建流2.中间操作3.终结操作二、中间操作1.过滤filter2.map()3.sorted()4.flatMap5.limit()6.skip(inta)7.去重8.排序三、终结操作1.forEach2.count3.max&min4.collect5.anyMatch(重写判断条件)6.allMatch(重写判断条件)7.noneMatch(重写判断条件)8.findAny随......
  • 服务器与电脑的区别
    1.PC机和专业服务器是完全是两种东西,硬件不同,当然驱动也不可能相同。比如说对服务器/工作站主板而言,最重要的是高可靠性和稳定性,其次才是高性能。因为大多数的服务器都要满足每天24小时、每周7天的满负荷工作要求。价格方面自然要贵很多,当然PC也有20000元以上的配置,这样的PC也......
  • C++中的typedef和define的区别
    我们来看看typede和define的区别define是C++中用来定义宏的,也就是宏定义用的,宏在代码中就是纯粹进行简单的替换,这个替换过程不是在C++的编译过程中进行的,而是在这之前的预处理过程中就已经完成了.因为它不是在编译过程中进行,所以如果有潜在的错误,很难发现.typedef说白了,就是起......
  • MySql聚集索引和非聚集索引的区别
    MySql中的聚集索引和非聚集索引主要体现在以下几个方面:1.存储方式:聚集索引的叶子节点存储的是整行数据,非聚集索引叶子节点存储的是键值和整行的标识。2.索引结构:聚集索引的索引结构于数据表的物理结构相同,非聚集索引的索引结构与数据表的物理结构不同。   在数......
  • 【转帖】localhost和127.0.0.1的区别
    plantegg.github.io/2023/09/24/localhost和127.0.0.1的区别/背景有人告诉我localhost和127.0.0.1的区别是localhost不经过网卡,把我惊到了,因为我还真不知道这个知识点,于是去特别去验证了一下,这是个错误的理解,localhost会解析成127.0.0.1然后接下来的流程和127.0.0.1一模一......
  • docker和k8s的区别
    docker是一个开源的应用容器引擎,开发者可以打包他们的应用以及依赖到一个容器中,发布到流行的liunx系统上,或者实现虚拟化。k8s是一个开源的容器集群管理系统,可以实现容器集群的自动化部署、自动扩缩容、维护等。一、虚拟化角度:传统的虚拟技术,在将物理硬件虚拟成多套硬件后,需要......
  • LVS Nginx HAProxy区别
    LVS抗负载能力强,性能高,能达到F5硬件的60%,对内存和cpu资源消耗比较低工作在四层仅作分发之用,通过vrrp协议转发,具体流量由linux内核处理,没有流量的产生稳定性、可靠性好,自身有完整的双机热备方案,如LVS+keepalived应用范围比较广,可以对所有应用做负载均衡配置性比较低,减少人为出错的概......