java Stream collect()使用

2024-08-20 17:12
文章标签 java 使用 stream collect

本文主要是介绍java Stream collect()使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Stream的collect()方法是一个非常强大的终端操作,用于将流中的元素收集到一个结果容器中。collect()方法通常接收一个Collector接口的实现作为参数。
toList():将流中的元素收集到一个List中
 import java.util.Arrays;import java.util.List;import java.util.stream.Collectors;public class CollectToListExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);List<Integer> evenNumbers = numbers.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());System.out.println(evenNumbers);}}
toSet():将流中的元素收集到一个Set中,去除重复元素。
 import java.util.Arrays;import java.util.Set;import java.util.stream.Collectors;public class CollectToSetExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 2, 4, 3, 5);Set<Integer> distinctNumbers = numbers.stream().collect(Collectors.toSet());System.out.println(distinctNumbers);}}
toMap(Function<? super T,? extends K> keyMapper, Function<? super T,? extends U> valueMapper):将流中的元素收集到一个Map中,通过指定键和值的映射函数。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectToMapExample {public static void main(String[] args) {List<Person> people = Arrays.asList(new Person("Alice", 25),new Person("Bob", 30),new Person("Charlie", 28));Map<String, Integer> nameToAgeMap = people.stream().collect(Collectors.toMap(Person::getName, Person::getAge));System.out.println(nameToAgeMap);}static class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}}}
joining():将流中的元素连接成一个字符串
 import java.util.Arrays;import java.util.stream.Collectors;public class CollectJoiningExample {public static void main(String[] args) {List<String> words = Arrays.asList("Hello", "World", "Java");String joinedString = words.stream().collect(Collectors.joining(", "));System.out.println(joinedString);}}
summingInt(ToIntFunction<? super T> mapper)、summingLong(ToLongFunction<? super T> mapper)、summingDouble(ToDoubleFunction<? super T> mapper):分别用于计算流中元素的整数、长整数和双精度浮点数属性的总和。
import java.util.Arrays;import java.util.stream.Collectors;public class CollectSummingExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);int sum = numbers.stream().collect(Collectors.summingInt(Integer::intValue));System.out.println(sum);}}
averagingInt(ToIntFunction<? super T> mapper)、averagingLong(ToLongFunction<? super T> mapper)、averagingDouble(ToDoubleFunction<? super T> mapper):分别用于计算流中元素的整数、长整数和双精度浮点数属性的平均值。
 import java.util.Arrays;import java.util.stream.Collectors;public class CollectAveragingExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);double average = numbers.stream().collect(Collectors.averagingInt(Integer::intValue));System.out.println(average);}}
groupingBy(Function<? super T,? extends K> classifier):根据指定的分类函数对流中的元素进行分组,返回一个Map<K, List>。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectGroupingByExample {public static void main(String[] args) {List<Person> people = Arrays.asList(new Person("Alice", 25),new Person("Bob", 30),new Person("Charlie", 28),new Person("David", 25));Map<Integer, List<Person>> ageGroupMap = people.stream().collect(Collectors.groupingBy(Person::getAge));System.out.println(ageGroupMap);}static class Person {private String name;private int age;public Person(String name, int age) {this.name = name;this.age = age;}public String getName() {return name;}public int getAge() {return age;}}}
partitioningBy(Predicate<? super T> predicate):根据指定的谓词对流中的元素进行分区,返回一个Map<Boolean, List>,其中键为true和false,分别对应满足和不满足谓词的元素列表。
import java.util.Arrays;import java.util.List;import java.util.Map;import java.util.stream.Collectors;public class CollectPartitioningByExample {public static void main(String[] args) {List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);Map<Boolean, List<Integer>> evenOddMap = numbers.stream().collect(Collectors.partitioningBy(n -> n % 2 == 0));System.out.println(evenOddMap);}}

这篇关于java Stream collect()使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

Elasticsearch 在 Java 中的使用教程

《Elasticsearch在Java中的使用教程》Elasticsearch是一个分布式搜索和分析引擎,基于ApacheLucene构建,能够实现实时数据的存储、搜索、和分析,它广泛应用于全文... 目录1. Elasticsearch 简介2. 环境准备2.1 安装 Elasticsearch2.2 J