Java list stream 常用方法

embedded/2024/10/21 11:58:46/

Sorted

根据字符长短排序

java">public class Java8Demo1 {public static void main(String[] args) {// Sort by length of the words.List<String> list = Arrays.asList("1234","456","abefc");List<String> list1 = list.stream().sorted(Comparator.comparing(x -> x.length())).collect(Collectors.toList());list1.forEach(l -> {System.out.println(l);});//lambdaList<String> list2 = list.stream().sorted((l1, l2) -> l1.length() - l2.length()).collect(Collectors.toList());list2.forEach(l -> {System.out.println(l);});}
}

BiFunction example

java">public class Java8Demo1 {public static void main(String[] args) {// Try BiFunctionMyCalculator<Double> calculator = new MyCalculator();// +System.out.println(calculator.compute(2.0, 3.0, (a, b) -> a + b));// -System.out.println(calculator.compute(21.0, 3.0, (a, b) -> a - b));// *System.out.println(calculator.compute(2.0, 3.0, (a, b) -> a * b));// /System.out.println(calculator.compute(6.0, 3.0, (a, b) -> a / b));}
}class MyCalculator<T> {public T compute(T t1, T t2, BiFunction<T,T,T> function) {return function.apply(t1, t2);}
}

输出

java">5.0
18.0
6.0
2.0Process finished with exit code 0

http://www.ppmy.cn/embedded/56068.html

相关文章

算法基础-----【动态规划】

动态规划(待完善) 动规五部曲分别为&#xff1a; 确定dp数组&#xff08;dp table&#xff09;以及下标的含义确定递推公式&#xff08;状态转移公式&#xff09;dp数组如何初始化确定遍历顺序举例推导dp数组、 动态规划的核心就是递归剪枝&#xff08;存储键值&#xff0c;…

K210视觉识别模块学习笔记6: 识别苹果_图形化操作函数_

今日开始学习K210视觉识别模块: 图形化操作函数 亚博智能 K210视觉识别模块...... 固件库: canmv_yahboom_v2.1.1.bin 训练网站: 嘉楠开发者社区 今日学习如何在识别到目标的时候添加图形化操作:(获取坐标、框出目标等) 在识别苹果的基础上 学习与添加 这些操…

Java SE入门及基础(62) 线程池 执行器

线程池 1. 执行器 In all of the previous examples, theres a close connection between the task being done by a new thread, as defined by its Runnable object, and the thread itself, as defined by a Thread object. This works well for small applications, but…

AI 与 Python 实战干货:基于深度学习的图像识别

《AI 与 Python 实战干货&#xff1a;基于深度学习的图像识别》 今天咱不啰嗦&#xff0c;直接上干货&#xff01; 在 AI 领域&#xff0c;特别是图像识别方面&#xff0c;Python 简直是一把利器。咱就以手写数字识别为例&#xff0c;来看看怎么用 Python 实现一个深度学习模…

【mybatis】mybatis-plus简单使用

1、简介 MyBatis-Plus&#xff08;简称MP&#xff09;是MyBatis的增强工具&#xff0c;它在MyBatis的基础上进行了扩展&#xff0c;以简化开发、提高效率。本案例是一个MyBatis-Plus的简单使用案例&#xff0c;包括项目的初始化、配置、实体类、Mapper接口以及基本的CRUD操作。…

3ds Max导出fbx贴图问题简单记录

1.前言 工作中发现3ds Max导出的fbx在其它软件&#xff08;Autodesk viewer&#xff0c;blender&#xff0c;navisworks&#xff0c;FBXReview等&#xff09;中丢失了部分贴图&#xff0c;但导出的fbx用3ds Max打开却正常显示。 fbx格式使用范围较广&#xff0c;很多常见的三…

整合 Mybatis Plus

什么是 MyBatis Plus&#xff1f; MyBatis Plus &#xff08;简称 MP&#xff09; 是一款持久层框架&#xff0c;说白话就是一款操作数据库的框架。它是一个 MyBatis 的增强工具&#xff0c;就像 iPhone手机一般都有个 plus 版本一样&#xff0c;它在 MyBatis 的基础上只做增强…

Elasticsearch环境搭建|ES单机|ES单节点模式启动|ES集群搭建|ES集群环境搭建

文章目录 版本选择单机ES安装与配置创建非root用户导入安装包安装包解压配置JDK环境变量配置single-node配置JVM参数后台启动|启动日志查看启动成功&#xff0c;访问终端访问浏览器访问 Kibana安装修改配置后台启动|启动日志查看浏览器访问 ES三节点集群搭建停止es服务域名配置…