Java数组怎么转List,Stream的基本方法使用教程

发布于:2024-08-20 ⋅ 阅读:(26) ⋅ 点赞:(0)

Stream流

Java 的 Stream 流操作是一种简洁而强大的处理集合数据的方式,允许对数据进行高效的操作,如过滤、映射、排序和聚合。Stream API 于 Java 8 引入,极大地简化了对集合(如 List、Set)等数据的处理。

一、创建 Stream

从集合创建:

List<String> list = Arrays.asList("a", "b", "c");
Stream<String> stream = list.stream();

从数组创建:

String[] array = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(array);

使用 Stream.of 方法:

Stream<String> stream = Stream.of("a", "b", "c");

二、中间操作

filter:过滤符合条件的元素。

stream.filter(s -> s.startsWith("a"));

map:将每个元素转换为另一种形式。

stream.map(String::toUpperCase);

sorted:排序流中的元素

stream.sorted();
stream.sorted(Comparator.reverseOrder());

distinct:去除重复元素

stream.distinct();

limit:截取流中的前 n 个元素

stream.limit(3);

skip:跳过流中的前 n 个元素

stream.skip(2);

三、终端操作

forEach:对流中每个元素执行操作。

stream.forEach(System.out::println);

collect:将流转换为另一种形式(如 List、Set)

List<String> resultList = stream.collect(Collectors.toList());

count:返回流中元素的个数

long count = stream.count();

max:最大值

//数组获取最大值
        int asInt = Arrays.stream(nums).max().getAsInt();
        System.out.println("数组中最大值: "+asInt);

//List获取最大值
        Integer integer = numList.stream().max(Integer::compare).get();
        System.out.println("List中最大值:"+integer);

 reduce:将流中的元素组合为一个值。

Optional<String> concatenated = stream.reduce((s1, s2) -> s1 + s2);

anyMatch、allMatch、noneMatch:检测流中的元素是否匹配给定的条件

boolean anyStartsWithA = stream.anyMatch(s -> s.startsWith("a"));

并行流

List<String> list = Arrays.asList("a", "b", "c");
list.parallelStream().forEach(System.out::println);

 四、数组转List

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: ThatMonth
 * @create: 2024-08-16 16:57
 **/
public class Test49 {
    public static void main(String[] args) {
        int[] nums = new int[]{2,1,5,6,2,3};
        System.out.println("原始数组: "+Arrays.toString(nums));

     
        //数组转List
        List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
        System.out.println("数组转List: "+numList);

     

        //List转数组
        int[] array = numList.stream().mapToInt(Integer::intValue).toArray();
        System.out.println("List转数组: "+Arrays.toString(array));

    }
}

五、综合使用

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: ThatMonth
 * @create: 2024-08-16 16:57
 **/
public class Test {
    public static void main(String[] args) {
        int[] nums = new int[]{2,1,5,6,2,3};
        System.out.println("原始数组: "+Arrays.toString(nums));

        //数组获取最大值
        int asInt = Arrays.stream(nums).max().getAsInt();
        System.out.println("数组中最大值: "+asInt);

        //数组转List
        List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
        System.out.println("数组转List: "+numList);

        //List获取最大值
        Integer integer = numList.stream().max(Integer::compare).get();
        System.out.println("List中最大值:"+integer);

        //条件过滤
        List<Integer> collect = numList.stream().filter(e -> e > 3).collect(Collectors.toList());
        System.out.println("filter 条件过滤大于3的: "+collect);

        //map遍历修改,(flatmap返回是最低一层的数据结构,如List<List<Student>>返回的是List<Student>)
        List<Integer> collect1 = numList.stream().map(e -> ++e).collect(Collectors.toList());
        System.out.println("map 遍历修改加1: "+collect1);

        //规约求和
        int reduc = numList.stream().reduce(100, (e1, e2) -> e1 + e2);
        System.out.println("reduce 规约求和: "+reduc);

        //自定义排序
        List<Integer> collect2 = numList.stream().sorted((e1,e2)->e1-e2).collect(Collectors.toList());
        System.out.println("sorted 自定义排序: "+collect2);

        //任意匹配
        boolean m = numList.stream().anyMatch(e1->e1>100);
        System.out.println("anyMatch 是否存在大于100的: "+m);

        //分页
        List<Integer> collect3 = numList.stream().skip(2).limit(3).collect(Collectors.toList());
        System.out.println("skip,limit 分页查询从第2条起,查3条: "+collect3);

        //去重
        List<Integer> collect4 = numList.stream().distinct().collect(Collectors.toList());
        System.out.println("distinct 去重: "+collect4);

        //转字符串并用‘--’连接
        String collect5 = numList.stream().map(e ->e.toString()).collect(Collectors.joining("--"));
        System.out.println("Collectors.joining 转字符串并用‘--’连接: "+collect5);

        //List转数组
        int[] array = numList.stream().mapToInt(Integer::intValue).toArray();
        System.out.println("List转数组: "+Arrays.toString(array));

    }
}

 示例:


网站公告

今日签到

点亮在社区的每一天
去签到