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

相关文章

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Redis中6种缓存更新策略详解

《Redis中6种缓存更新策略详解》Redis作为一款高性能的内存数据库,已经成为缓存层的首选解决方案,然而,使用缓存时最大的挑战在于保证缓存数据与底层数据源的一致性,本文将介绍Redis中6种缓存更... 目录引言策略一:Cache-Aside(旁路缓存)策略工作原理代码示例优缺点分析适用场景策略二:Re

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法

《SQLyog中DELIMITER执行存储过程时出现前置缩进问题的解决方法》在SQLyog中执行存储过程时出现的前置缩进问题,实际上反映了SQLyog对SQL语句解析的一个特殊行为,本文给大家介绍了详... 目录问题根源正确写法示例永久解决方案为什么命令行不受影响?最佳实践建议问题根源SQLyog的语句分

Java注解之超越Javadoc的元数据利器详解

《Java注解之超越Javadoc的元数据利器详解》本文将深入探讨Java注解的定义、类型、内置注解、自定义注解、保留策略、实际应用场景及最佳实践,无论是初学者还是资深开发者,都能通过本文了解如何利用... 目录什么是注解?注解的类型内置注编程解自定义注解注解的保留策略实际用例最佳实践总结在 Java 编程

MySQL数据库约束深入详解

《MySQL数据库约束深入详解》:本文主要介绍MySQL数据库约束,在MySQL数据库中,约束是用来限制进入表中的数据类型的一种技术,通过使用约束,可以确保数据的准确性、完整性和可靠性,需要的朋友... 目录一、数据库约束的概念二、约束类型三、NOT NULL 非空约束四、DEFAULT 默认值约束五、UN

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

MySQL中的分组和多表连接详解

《MySQL中的分组和多表连接详解》:本文主要介绍MySQL中的分组和多表连接的相关操作,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录mysql中的分组和多表连接一、MySQL的分组(group javascriptby )二、多表连接(表连接会产生大量的数据垃圾)MySQL中的

Java 实用工具类Spring 的 AnnotationUtils详解

《Java实用工具类Spring的AnnotationUtils详解》Spring框架提供了一个强大的注解工具类org.springframework.core.annotation.Annot... 目录前言一、AnnotationUtils 的常用方法二、常见应用场景三、与 JDK 原生注解 API 的

redis中使用lua脚本的原理与基本使用详解

《redis中使用lua脚本的原理与基本使用详解》在Redis中使用Lua脚本可以实现原子性操作、减少网络开销以及提高执行效率,下面小编就来和大家详细介绍一下在redis中使用lua脚本的原理... 目录Redis 执行 Lua 脚本的原理基本使用方法使用EVAL命令执行 Lua 脚本使用EVALSHA命令