Stream流

目录

1.中间操作

2.终结操作

3.所有经典举例

4.Stream流总结


1.中间操作

  • filter:对集合数据进行过滤操作
  • limit:获取前几条
  • skip:跳过几条
  • distinct:去重
  • sorted:排序
  • map:转换操作

2.终结操作

  • foreach 遍历
  • count 统计
  • toArray 放入数组

举例:

String[] arr2 = list.stream()
                .toArray(value -> new String[value]);
  • collect 放入集合

举例 1:收集到 List 集合中去

List<String> newlist1 = list.stream()
        .filter(s -> "男".equals(s.split("-")[1]))
        .collect(Collectors.toList());

举例 2:收集到 Set 集合中去

Set<string> newlist2 = list.stream()
        .filter(s -> "男".equals(s.split("-")[1]))
        .collect(Collectors.toSet());

举例 3:收集到 Map 集合中去

注意点:

如果我们要收集到 Map 集合当中,键不能重复,否则会报错。

Map<String, Integer> map = list.stream()
    .filter(s -> "男".equals(s.split("-")[1]))
    /*
         *     toMap(): 参数一表示键的生成规则
         *              参数二表示值的生成规则
         * 参数一:
         *       Function泛型一:表示流中每一个数据的类型
         *               泛型二:表示Map集合中键的数据类型
         *       方法apply形参:依次表示流里面的每一个数据
         *                   方法体:生成键的代码
         *                   返回值:已经生成的键
         *    参数二:
         *       Function泛型一:表示流中每一个数据的类型
         *               泛型二:表示Map集合中值的数据类型
         *       方法apply形参:依次表示流里面的每一个数据
         *                   方法体:生成值的代码
         *                   返回值:已经生成的值
         *
*/
.collect(Collectors.toMap(
    new Function<String, String>() {
    @Override
    public String apply(String s) {
        return s.split("-")[0];
    }},
    new Function<String, Integer>() {
    @Override
    public Integer apply(String s) {
        return Integer.parseInt(s.split("-")[2]);
    }
}));
Map<String, Integer> map2 = list.stream()
    .filter(s -> "男".equals(s.split("-")[1]))
    .collect(Collectors.toMap(
                s -> s.split("-")[0],
                s -> Integer.parseInt(s.split("-")[2])));

3.所有经典举例

举例 1:

List<Product> products = productMapper.selectProduct();
List<Product> products1 = products.stream()
    .filter(p -> p.getProductName().contains("设计模式"))
    .sorted(new Comparator<Product>(){
        @Override
        public int compare(Product o1, Product 02){
            return o1.getProductPrice()
                            .compareTo(02.getProductPrice());
            }
        })
    .limit(10).collect(Collectors.tolist();

举例 2:

public static int longestConsecutive1(int[] nums){
    Arrays.sort(nums);
    nums = Arrays.stream(nums)
    .distinct()
    .toArray();//数组去重
    //。。。
    return 0;
}

举例 3:

Stream<String> stream1 = list.stream();
stream1.forEach(new Consumer<String>(){
    @Override
    public void accept(string s){
        system.out.println(s);
    }
});
list.stream().forEach(s -> System.out.println(s))

foreach 方法的参数是 实现一个函数式接口,可以用匿名内部类形式,最后用 Lambda 表达式简化一下就是以上的最终结果了。

举例 4:(双列集合)

举例 5:(一堆零散数据)

举例 6:(中间方法:concat)

//concat    合并a和b两个流为一个流
Stream.concat(list.stream(),list2.stream())
        .forEach(s-> System.out.println(s));

举例7:(中间方法 map)

// map:转换"流"中的数据类型
//第一个类型:流中原本的数据类型
//第二个类型:要转成之后的类型
//apply的形参s:依次表示流里面的每一个数据
//返回值:表示转换之后的数据
list.stream().map(new Function<String, Object>() {
    @Override
    public Object apply(String s) {
        String[] arr = s.split("-");
        String ageString = arr[1];
        int age = Integer.parseInt(ageString);
        return age;
    }
}).forEach(s-> System.out.print(s + " "));
//以上是匿名内部类的写法,可以简化成Lambda表达式的写法
list.stream()
    .map(s -> Integer.parseInt(s.split("-")[1]))
    .forEach(s-> System.out.println(s));

4.Stream流总结

  • 1.Stream流的作用
    结合了Lambda表达式,简化集合,数组的操作。
  • 2.Stream的使用步骤
    • 获取Stream流对象
    • 使用中间方法处理数据
    • 使用终结方法处理数据
  • 3.如何获取Stream流对象
    • 单列集合:Collection中的默认方法stream
    • 双列集合:不能直接获取
    • 数组:Arrays工具类型中的静态方法stream
    • 一堆零散的数据:Stream接口中的静态方法of
  • 4.常见方法
    • 中间方法: filter, limit, skip, distinct, concat, map
    • 终结方法: forEach, count, collect

Stream 特性:

Stream和for的区别:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值