【JAVA】【Stream流】

1. filter操作

filter()方法用于根据给定的条件过滤列表中的元素,仅保留满足条件的项。

java 复制代码
    List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8);
    List<Integer> res = list.stream().filter(a -> a % 2 == 0).collect(Collectors.toList());
    for(Integer it :res){
        System.out.println(it);
    }

Collectors属于 package java.util.stream;

2. Sorted操作

sorted()方法可以对流中的元素进行排序,可以使用自然顺序或自定义Comparator。

java 复制代码
    List<String> names = Arrays.asList("Bob", "Alice", "Charlie", "David");
    List<String> sortedNames = names.stream()
                                   .sorted()
                                   .collect(Collectors.toList()); // 自然排序
    List<String> reverseSortedNames = names.stream()
                                          .sorted(Comparator.reverseOrder())
                                          .collect(Collectors.toList()); // 倒序排序

3. GroupingBy操作

groupBy()方法用于将流中的元素按照指定的属性进行分组,返回的是Map类型结果。

java 复制代码
    List<Employee> employees = ... // 假设Employee类有department属性
    Map<String, List<Employee>> groupedEmployees = employees.stream()
                                                        .collect(Collectors.groupingBy(Employee::getDepartment));

4.map操作

map 方法是 Stream 接口中的一个中间操作,它接受一个 Function 接口作为参数。这个 Function 接口定义了如何将流中的元素映射为另一个元素。map 方法返回一个新的 Stream,其中包含由原始流中的元素经过函数转换后的结果。

map 方法的主要用途是对流中的元素进行转换或映射。这种转换可以是类型转换、数据提取、逻辑处理等。通过 map 方法,可以轻松地实现复杂的数据处理逻辑,同时保持代码的简洁和可读性。

字符串长度转换

java 复制代码
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamMapExample1 {
        public static void main(String[] args) {
            List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
            // 使用 map 将字符串列表转换为对应的长度列表
            List<Integer> lengths = names.stream()
                                         .map(String::length)
                                         .collect(Collectors.toList());
            System.out.println("字符串长度列表: " + lengths);
        }
    }

数字加倍

java 复制代码
    import java.util.Arrays;
    import java.util.List;
    import java.util.stream.Collectors;
    
    public class StreamMapExample2 {
        public static void main(String[] args) {
            List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
            // 使用 map 将每个数字加倍
            List<Integer> doubledNumbers = numbers.stream()
                                                 .map(n -> n * 2)
                                                 .collect(Collectors.toList());
            System.out.println("加倍后的数字列表: " + doubledNumbers);
        }
    }

java 复制代码
int minCol = recList.stream().sorted(Comparator.comparing(Node::getCol))
      				.collect(Collectors.toList()).get(0).col;
      				
int maxWidth = widthList.stream().sorted().collect(Collectors.toList()).get(widthList.size() - 1);

int maxHeight = heightList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()).get(0);
相关推荐
小羊没烦恼!3 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
俊昭喜喜里3 天前
java中的继承和多态的区别
java
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
rockmelodies3 天前
# Windows Server2008 R2 Standard(SP1镜像U盘)重置本地管理员密码【完整详细步骤】
windows·密码破解
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
譕痕3 天前
JSONObject与JSONArray封装数据格式区别
java·json
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
胡写代码3 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖3 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商