1.1 查找所有的偶数并求和

public static void p1() {  List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);  int sum = numbers.stream()      .filter(num -> num % 2 == 0)      .mapToInt(Integer::intValue)      .sum() ;  System.err.printf("result: %s%n", sum) ;}

1.2 查找并打印长度大于 5 的字符串个数

public static void p2() {  List<String> strings = Arrays.asList(    "apple", "banana", "grape",     "watermelon", "kiwi", "orange");  Long count = strings.stream()      .filter(str -> str.length() > 5)      .count() ;  System.err.printf("result: %s%n", count) ;}

1.3 处理每一个元素最后返回新集合​​​​​​​

public static void p3() {  List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);  List<Integer> squares = numbers.stream()      .map(num -> num * num)      .collect(Collectors.toList()) ;  System.err.printf("result: %s%n", squares) ;}

将每一个元素进行平方操作,最后返回一个新的集合。输出结果:

result: [1, 4, 9, 16, 25]

1.4 找出整数列表中的最大元素​​​​​​​

public static void p4() {  List<Integer> numbers = Arrays.asList(10, 5, 25, 15, 30);  int max = numbers.stream()      .mapToInt(Integer::intValue)      .max()      .getAsInt();  System.err.printf("result: %s%n", max) ;}

1.5 将列表中的所有字符串连接成一个字符串​​​​​​​

public static void p5() {  List<String> fruits = Arrays.asList("apple", "banana", "cherry","coconut", "apple");  String concat =fruits.stream()      .collect(Collectors.joining()) ;  System.err.printf("result: %s%n", concat) ;}

1.6 转成大写再排序​​​​​​​

public static void p6() {  List<String> fruits = Arrays.asList("apple", "Banana", "Grape", "orange", "kiwi");  List<String> sortedUppercase = fruits.stream()      .map(String::toUpperCase)      .sorted()      .collect(Collectors.toList());  System.err.printf("result: %s%n", sortedUppercase) ;}

先将字符串转为大写,然后在进行排序,输出结果:

result: [APPLE, BANANA, GRAPE, KIWI, ORANGE]

1.7 计算double类型平均值​​​​​​​

public static void p7() {  List<Double> doubles = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);  double average = doubles.stream()      .mapToDouble(Double::doubleValue)      .average()      .getAsDouble() ;  System.err.printf("result: %s%n", average) ;}

1.8 删除重复元素​​​​​​​

public static void p8() {  List<String> words = Arrays.asList("apple", "banana", "apple", "orange", "banana", "kiwi");  List<String> uniqueWords = words.stream()      .distinct()      .collect(Collectors.toList()) ;  System.err.printf("result: %s%n", uniqueWords) ;}

1.9 检查所有元素是否符合条件​​​​​​​

public static void p9() {  List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);  boolean allEven = numbers.stream()      .allMatch(n -> n%2 == 0) ;  System.err.printf("result: %s%n", allEven) ;}

1.10 检查集合中是否包含特定元素​​​​​​​

public static void p10() {  List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10);  boolean exists = numbers.stream()      .anyMatch(n -> n.equals(8)) ;  System.err.printf("result: %s%n", exists) ;}

1.11 查找流中最长的字符串​​​​​​​

public static void p11() {  List<String> fruits = Arrays.asList("apple", "banana", "cherry", "coconut", "apple") ;  int max = fruits.stream()    .mapToInt(String::length)    .max()    .getAsInt() ;  System.err.printf("result: %s%n", max) ;}

1.12 从流中删除null值​​​​​​​

public static void p12() {  List<String> fruits = Arrays.asList("apple", "banana", "cherry", null,"coconut", "apple");  List<String> nonNullValues = fruits.stream()      .filter(Objects::nonNull)      .collect(Collectors.toList()) ;  System.err.printf("result: %s%n", nonNullValues) ;}

过滤为null的值,输出结果:

result: [apple, banana, cherry, coconut, apple]

1.13 分组并查找最大值​​​​​​​

public static void p13() {  List<Employee> employees =new ArrayList<>() ;  employees.add(new Employee("Alice","HR",50000.0)) ;  employees.add(new Employee("Bob","IT",60000.0)) ;  employees.add(new Employee("Charlie","Finance",55000.0)) ;  employees.add(new Employee("David","IT",70000.0)) ;  employees.add(new Employee("Eva", "HR", 45000.0)) ;  employees.add(new Employee("Frank","Finance",58000.0));  Map<String, Optional<Employee>> highestSalaryPerDept = employees.stream()      .collect(Collectors.groupingBy(          Employee::getDepartment,           Collectors.maxBy(Comparator.comparingDouble(Employee::getSalary))      ));  highestSalaryPerDept.forEach((key, value) -> {    System.err.printf("部门: %s, \t最高薪: %s%n", key, value.get()) ;  });}

输出结果:

图片

2.14 查找列表中第二小的元素​​​​​​​

public static void p14() {  List<Integer> numbers = Arrays.asList(2, 4, 6, 8, 10) ;  Optional<Integer> secondSmallest = numbers.stream()      .distinct()      .sorted()      .skip(1)      .findFirst() ;  System.err.printf("result: %s%n", secondSmallest) ;}

1.15 查找两个列表的交集​​​​​​​

public static void p15() {  List<Integer> list1 = Arrays.asList(1, 2, 3, 4, 5) ;  List<Integer> list2 = Arrays.asList(4, 5, 6, 7,8) ;  List<Integer> intersection = list1.stream()      .filter(list2::contains)      .collect(Collectors.toList()) ;  System.err.printf("result: %s%n", intersection) ;}

1.16 并行处理提升性能

使用并行流可以通过BaseStream.parallel或Collection#parallelStream操作,如下计算1亿个数的求和​​​​​​​

public static void main(String[] args) {double[] arr = IntStream.range(0, 100_000_000)      .mapToDouble(i -> new Random().nextDouble() * 100000)      .toArray() ;  computeSumOfSquareRoots(arr);}
public static void computeSumOfSquareRoots(double[] arr) {double serialSum = computeSerialSum(DoubleStream.of(arr));  System.out.println("Serial Sum: " + serialSum);
double parallelSum = computeParallelSum(DoubleStream.of(arr));  System.out.println("Parallel Sum: " + parallelSum);}
public static double computeSerialSum(DoubleStream stream) {long startTime = System.currentTimeMillis();double sum = stream.reduce(0.0D, (l, r) -> l + r) ;long endTime = System.currentTimeMillis();  System.out.println("Serial Computation Time: " + (endTime - startTime) + " ms");return sum;}
public static double computeParallelSum(DoubleStream stream) {long startTime = System.currentTimeMillis();double sum = stream.parallel().reduce(0, (l, r) -> l + r) ;long endTime = System.currentTimeMillis();  System.out.println("Parallel Computation Time: " + (endTime - startTime) + " ms");return sum;}

运行结果​​​​​​​

SerialComputation Time: 73 msSerialSum: 5.000114159154823E12ParallelComputation Time: 38 msParallelSum: 5.000114159151367E12

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/web/86577.shtml
繁体地址,请注明出处:http://hk.pswp.cn/web/86577.shtml
英文地址,请注明出处:http://en.pswp.cn/web/86577.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

力扣 刷题(第七十一天)

灵感来源 - 保持更新&#xff0c;努力学习 - python脚本学习 4的幂 解题思路 位运算条件&#xff1a;4 的幂的二进制表示中只有一个 1&#xff0c;且位于奇数位&#xff08;如 4 100&#xff0c;4 10000&#xff09;。模运算条件&#xff1a;4 的幂减 1 后能被 3 整除&…

深度学习使用Pytorch训练模型步骤

训练模型是机器学习和深度学习中的核心过程&#xff0c;旨在通过大量数据学习模型参数&#xff0c;以便模型能够对新的、未见过的数据做出准确的预测。 训练模型通常包括以下几个步骤&#xff1a; 1.数据准备&#xff1a; 收集和处理数据&#xff0c;包括清洗、标准化和归一化…

Unity_导航操作(鼠标控制人物移动)_运动动画

文章目录 前言一、Navigation 智能导航地图烘焙1.创建Plan和NavMesh Surface2.智能导航地图烘焙 二、MouseManager 鼠标控制人物移动1.给场景添加人物&#xff0c;并给人物添加导航组件2.编写脚本管理鼠标控制3.给人物编写脚本&#xff0c;订阅事件&#xff08;添加方法给Mouse…

6. 接口分布式测试pytest-xdist

pytest-xdist实战指南&#xff1a;解锁分布式测试的高效之道 随着测试规模扩大&#xff0c;执行时间成为瓶颈。本文将带你深入掌握pytest-xdist插件&#xff0c;利用分布式测试将执行速度提升300%。 一、核心命令解析 加速安装&#xff08;国内镜像&#xff09; pip install …

预训练语言模型

预训练语言模型 1.1Encoder-only PLM ​ Transformer结构主要由Encoder、Decoder组成&#xff0c;根据特点引入了ELMo的预训练思路。 ELMo&#xff08;Embeddings from Language Models&#xff09;是一种深度上下文化词表示方法&#xff0c; 该模型由一个**前向语言模型&…

Altera PCI IP target设计分享

最近调试也有关于使用Altera 家的PCI IP&#xff0c;然后分享一下代码&#xff1a; 主要实现&#xff1a;主控作为主设备&#xff0c;FPGA作为从设备&#xff0c;主控对FPGA IO读写的功能 后续会分享FPGA作为主设备&#xff0c; 从 FPGA通过 memory写到主控内存&#xff0c;会…

基于机器学习的智能文本分类技术研究与应用

在当今数字化时代&#xff0c;文本数据的爆炸式增长给信息管理和知识发现带来了巨大的挑战。从新闻文章、社交媒体帖子到企业文档和学术论文&#xff0c;海量的文本数据需要高效地分类和管理&#xff0c;以便用户能够快速找到所需信息。传统的文本分类方法主要依赖于人工规则和…

前端项目3-01:登录页面

一、效果图 二、全部代码 <!DOCTYPE html> <html><head><meta charset"utf-8"><title>码农魔盒</title><style>.bg{position: fixed;top: 0;left:0;object-fit: cover;width: 100vw;height: 100vh;}.box{width: 950px;he…

Nexus CLI:简化你的分布式计算贡献之旅

探索分布式证明网络的力量&#xff1a;Nexus CLI 项目深入解析 在今天的数字时代&#xff0c;分布式计算和去中心化技术正成为互联网发展的前沿。Nexus CLI 是一个为 Nexus 网络提供证明的高性能命令行界面&#xff0c;它不仅在概念上先进&#xff0c;更是在具体实现中为开发者…

IBW 2025: CertiK首席商务官出席,探讨AI与Web3融合带来的安全挑战

6月26日至27日&#xff0c;全球最大的Web3安全公司CertiK亮相伊斯坦布尔区块链周&#xff08;IBW 2025&#xff09;&#xff0c;首席商务官Jason Jiang出席两场圆桌论坛&#xff0c;分享了CertiK在AI与Web3融合领域的前沿观察与安全见解。他与普华永道土耳其网络安全服务主管Nu…

Vivado 五种仿真类型的区别

Vivado 五种仿真类型的区别 我们还是用“建房子”的例子来类比。您已经有了“建筑蓝图”&#xff08;HLS 生成的 RTL 代码&#xff09;&#xff0c;现在要把它建成真正的房子&#xff08;FPGA 电路&#xff09;。这五种仿真就是在这个过程中不同阶段的“质量检查”。 1. 行为…

小程序快速获取url link方法,短信里面快速打开链接

获取小程序链接方法 uni.request({url:https://api.weixin.qq.com/cgi-bin/token?grant_typeclient_credential&appidwxxxxxxxxxxxx&secret111111111111111111111111111111111,method:GET,success(res) {console.log(res.data)let d {"path": "/xxx/…

Spring 框架(1-4)

第一章&#xff1a;Spring 框架概述 1.1 Spring 框架的定义与背景 Spring 是一个开源的轻量级 Java 开发框架&#xff0c;于 2003 年由 Rod Johnson 创立&#xff0c;旨在解决企业级应用开发的复杂性。其核心设计思想是面向接口编程和松耦合架构&#xff0c;通过分层设计&…

RabitQ 量化:既省内存又提性能

突破高维向量内存瓶颈:Mlivus Cloud RaBitQ量化技术的工程实践与调优指南 作为大禹智库高级研究员,拥有三十余年向量数据库与AI系统架构经验的我发现,在当今多模态AI落地的核心场景中,高维向量引发的内存资源消耗问题已成为制约系统规模化部署的“卡脖子”因素。特别是在大…

创客匠人:创始人 IP 打造的得力助手

在当今竞争激烈的商业环境中&#xff0c;创始人 IP 的打造对于企业的发展愈发重要。一个鲜明且具有影响力的创始人 IP&#xff0c;能够为企业带来独特的竞争优势&#xff0c;提升品牌知名度与美誉度。创客匠人在创始人 IP 打造过程中扮演着不可或缺的角色&#xff0c;为创始人提…

如何为虚拟机上的 Manjaro Linux启用 VMware 拖放功能

如果你的Manjaro 发行版本是安装在 VMware Workstation Player 上使用的 &#xff0c;而且希望可以通过拖放功能将文件或文件夹从宿主机复制到客户端的Manjaro 里面&#xff0c;那么可以按照以下的步骤进行操作&#xff0c;开启拖放功能。 在 VMware 虚拟机上安装 Manjaro 后&…

【C/C++】单元测试实战:Stub与Mock框架解析

C 单元测试中的 Stub/Mock 框架详解 在单元测试中&#xff0c;Stub&#xff08;打桩&#xff09;和Mock都是替代真实依赖以简化测试的技术。通常&#xff0c;Stub&#xff08;或 Fake&#xff09;提供了一个简化实现&#xff0c;用于替代生产代码中的真实对象&#xff08;例如…

工厂 + 策略设计模式(实战教程)

在软件开发中&#xff0c;设计模式是解决特定问题的通用方案&#xff0c;而工厂模式与策略模式的结合使用&#xff0c;能在特定业务场景下发挥强大的威力。本文将基于新增题目&#xff08;题目类型有单选、多选、判断、解答&#xff09;这一业务场景&#xff0c;详细阐述如何运…

Nuxt3中使用 Ant-Design-Vue 的BackTop 组件实现自动返回页面顶部

在现代 Web 应用中&#xff0c;提供一个方便用户返回页面顶部的功能是非常重要的。Ant Design Vue 提供了 BackTop 组件&#xff0c;可以轻松实现这一功能。本文将详细介绍如何在 Nuxt 3 项目中使用 <a-back-top/> 组件&#xff0c;并通过按需引入的方式加载组件及其样式…

在统信UOS(Linux)中构建SQLite3桌面应用笔记

目录 1 下载lazarus 2 下载sqlite3源码编译生成库文件 3 新建项目 4 设置并编译 一次极简单的测试&#xff0c;记录一下。 操作系统&#xff1a;统信UOS&#xff0c; 内核&#xff1a;4.19.0-arm64-desktop 处理器&#xff1a;D3000 整个流程难点是生成so库文件并正确加…