Stream流中方法详解

2024-04-10 08:36
文章标签 方法 详解 stream 流中

本文主要是介绍Stream流中方法详解,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

中间方法和终结方法

以下传入参数均表示lambda表达式中的参数

forEach():遍历集合,对流中的每个元素执行指定的操作
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().forEach(s-> System.out.println());void forEach(Consumer<? super T> action);//接口中的抽象方法@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);}

传入集合元素,无返回值,方法体内对元素做操作

peek():和forEach类似,但不会消耗和结束流,通常用于调试和观察流的元素
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().peek(s -> System.out.println(s));Stream<T> peek(Consumer<? super T> action);@FunctionalInterface
public interface Consumer<T> {/*** Performs this operation on the given argument.** @param t the input argument*/void accept(T t);}

传入集合元素,无返回值,方法体内对元素做操作

filter(Predicate):根据指定的条件过滤流中的元素
 ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));list.stream().filter(s -> s.equals("a"));Stream<T> filter(Predicate<? super T> predicate);@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}

传入集合元素,返回布尔值,方法体内写元素的条件,符合条件保留

distinct():去除重复的元素
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().distinct();Stream<T> distinct();

不用传入,保留第一次出现的元素,去除流中的重复元素

limit()限制元素数量
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().limit(3);Stream<T> limit(long maxSize);

传入long类型数字,只保留流的前n个元素,n为传入参数

anyMatch(Predicate)判断是否有元素满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().anyMatch(s -> s.equals("a"));boolean anyMatch(Predicate<? super T> predicate);@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

allMatch(Predicate)判断是否都满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().allMatch(s -> s.equals("a"));boolean allMatch(Predicate<? super T> predicate);@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

noneMatch(Predicate)判断是否没有元素满足条件,终结流
ArrayList<String> list = new ArrayList<>(Arrays.asList("a","b","c","d"));
list.stream().noneMatch(s -> s.equals("a"));boolean noneMatch(Predicate<? super T> predicate);@FunctionalInterface
public interface Predicate<T> {/*** Evaluates this predicate on the given argument.** @param t the input argument* @return {@code true} if the input argument matches the predicate,* otherwise {@code false}*/boolean test(T t);
}

传入集合元素,方法体中写对元素的判断条件,返回布尔值

map(Function)将函数结果映射到新的流中
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().map(s -> "t");<R> Stream<R> map(Function<? super T, ? extends R> mapper);@FunctionalInterface
public interface Function<T, R> {/*** Applies this function to the given argument.** @param t the function argument* @return the function result*/R apply(T t);
}

传入集合元素,方法体中写映射的元素,返回这个类型的元素

最终返回的是一个只含映射后的新元素的流

sort()从小到大排序
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().sorted();Stream<T> sorted();

对流进行排序

sort(Comparator)构造器排序
ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().sorted((o1,o2)->o2.compareTo(o1));Stream<T> sorted(Comparator<? super T> comparator);@FunctionalInterface
public interface Comparator<T> {//熟悉的构造器接口int compare(T o1, T o2);
}

同上,只不过传入的是构造器自定义排序

收集方法

ArrayList<String> list = new ArrayList<>(Arrays.asList("a", "b", "c", "d"));
list.stream().toArray();
list.stream().collect(Collectors.toList());
list.stream().collect(Collectors.toSet());
list.stream().collect(Collectors.toMap(s -> s.charAt(0),s->s));

分别代表收集数组,列表,无序列表和双列集合的方法

这篇关于Stream流中方法详解的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/890564

相关文章

如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解

《如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别详解》:本文主要介绍如何通过海康威视设备网络SDK进行Java二次开发摄像头车牌识别的相关资料,描述了如何使用海康威视设备网络SD... 目录前言开发流程问题和解决方案dll库加载不到的问题老旧版本sdk不兼容的问题关键实现流程总结前言作为

Ubuntu固定虚拟机ip地址的方法教程

《Ubuntu固定虚拟机ip地址的方法教程》本文详细介绍了如何在Ubuntu虚拟机中固定IP地址,包括检查和编辑`/etc/apt/sources.list`文件、更新网络配置文件以及使用Networ... 1、由于虚拟机网络是桥接,所以ip地址会不停地变化,接下来我们就讲述ip如何固定 2、如果apt安

SQL 中多表查询的常见连接方式详解

《SQL中多表查询的常见连接方式详解》本文介绍SQL中多表查询的常见连接方式,包括内连接(INNERJOIN)、左连接(LEFTJOIN)、右连接(RIGHTJOIN)、全外连接(FULLOUTER... 目录一、连接类型图表(ASCII 形式)二、前置代码(创建示例表)三、连接方式代码示例1. 内连接(I

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

Java中八大包装类举例详解(通俗易懂)

《Java中八大包装类举例详解(通俗易懂)》:本文主要介绍Java中的包装类,包括它们的作用、特点、用途以及如何进行装箱和拆箱,包装类还提供了许多实用方法,如转换、获取基本类型值、比较和类型检测,... 目录一、包装类(Wrapper Class)1、简要介绍2、包装类特点3、包装类用途二、装箱和拆箱1、装

在不同系统间迁移Python程序的方法与教程

《在不同系统间迁移Python程序的方法与教程》本文介绍了几种将Windows上编写的Python程序迁移到Linux服务器上的方法,包括使用虚拟环境和依赖冻结、容器化技术(如Docker)、使用An... 目录使用虚拟环境和依赖冻结1. 创建虚拟环境2. 冻结依赖使用容器化技术(如 docker)1. 创

Go语言中三种容器类型的数据结构详解

《Go语言中三种容器类型的数据结构详解》在Go语言中,有三种主要的容器类型用于存储和操作集合数据:本文主要介绍三者的使用与区别,感兴趣的小伙伴可以跟随小编一起学习一下... 目录基本概念1. 数组(Array)2. 切片(Slice)3. 映射(Map)对比总结注意事项基本概念在 Go 语言中,有三种主要

Spring排序机制之接口与注解的使用方法

《Spring排序机制之接口与注解的使用方法》本文介绍了Spring中多种排序机制,包括Ordered接口、PriorityOrdered接口、@Order注解和@Priority注解,提供了详细示例... 目录一、Spring 排序的需求场景二、Spring 中的排序机制1、Ordered 接口2、Pri

Python中Markdown库的使用示例详解

《Python中Markdown库的使用示例详解》Markdown库是一个用于处理Markdown文本的Python工具,这篇文章主要为大家详细介绍了Markdown库的具体使用,感兴趣的... 目录一、背景二、什么是 Markdown 库三、如何安装这个库四、库函数使用方法1. markdown.mark

PLsql Oracle 下载安装图文过程详解

《PLsqlOracle下载安装图文过程详解》PL/SQLDeveloper是一款用于开发Oracle数据库的集成开发环境,可以通过官网下载安装配置,并通过配置tnsnames.ora文件及环境变... 目录一、PL/SQL Developer 简介二、PL/SQL Developer 安装及配置详解1.下