Jave8新特性:Stream流

distinct():去掉流中重复的数据

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

filter():根据条件过滤流中的数据

list.stream().filter(x -> x.length() > 4).forEach(System.out::println);

sort():对流中的数据排序

list.stream().sorted(String::compareTo).forEach(System.out::println);

limt():对流中的数据截取操作

list.stream().limit(5).forEach(System.out::println);

skip():跳过流中开头几个元素

list.stream().skip(3).forEach(System.out::println);

map映射
map映射转换元素,map方法接受一个函数,把这个函数应用于每个元素上,并将它映射为一个新的元素

list.stream().map(String::toUpperCase).forEach(System.out::println);

allMatch():判断流中的元素是否都匹配给定的条件

boolean allMatch = list.stream().allMatch(s -> s.length() > 3);

anyMathc():判断流中的元素是否有匹配给定的条件

boolean anyMatch = list.stream().anyMatch(s -> s.length() > 3);

noneMathc():判断流中的元素是否都不匹配给定的条件

boolean noneMatch = list.stream().noneMatch(s -> s.length() < 2);

count统计

long count = list.stream().count();

reduce归纳合并

	List<Integer> list2 = Arrays.asList(6,21,87,34,1,78,54);
    //求和,指定初始值
    Integer reduce = list2.stream().reduce(0, Integer::sum);
    System.out.println(reduce);
    //求和,没有初始值
    Optional<Integer> reduce1 = list2.stream().reduce((x,y) -> x + y);
    System.out.println(reduce1.get());
    //求最大值
    Integer max = (list2.stream().reduce(Math::max)).get();
    System.out.println(max);
    //求最小值
    Integer min = (list2.stream().reduce(Math::min)).get();
    System.out.println(min);

collect归约

	//把stream流转换为集合
    Set<String> set = list.stream().collect(Collectors.toSet());
    System.out.println(set);
    System.out.println("-------------");

    //把stream流转换为数组
    Object[] objects = list.stream().toArray();
    System.out.println(Arrays.toString(objects));
    System.out.println("-------------");

    //将stream流转换为字符串
    String collect = list.stream().collect(Collectors.joining("-"));
    System.out.println(collect);
    System.out.println("-------------");
package com.wkcto.stream;

import java.util.*;
import java.util.stream.Collectors;

public class Test02 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Collections.addAll(list,"bjpowernode","wkcto","hello","jj","xx","jj","jj","good","xx","good");

        //流的筛选与切片
        //distinct():去掉流中重复的数据
        list.stream().distinct().forEach(System.out::println);
        System.out.println("-------------");

        //filter():根据条件过滤流中的数据
        list.stream().filter(x -> x.length() > 4).forEach(System.out::println);
        System.out.println("-------------");

        //sort():对流中的数据排序
        list.stream().sorted(String::compareTo).forEach(System.out::println);
        System.out.println("-------------");

        //limt():对流中的数据截取操作
        list.stream().limit(5).forEach(System.out::println);
        System.out.println("-------------");

        //skip():跳过流中开头几个元素
        list.stream().skip(3).forEach(System.out::println);
        System.out.println("-------------");

        //map映射
        //map映射转换元素,map方法接受一个函数,把这个函数应用于每个元素上,并将它映射为一个新的元素
        list.stream().map(String::toUpperCase).forEach(System.out::println);
        System.out.println("-------------");

        list.stream()
                .map(String::length)
                .map(length -> length + "")
                .forEach(System.out::println);
        System.out.println("-------------");

        //allMatch():判断流中的元素是否都匹配给定的条件
        boolean allMatch = list.stream().allMatch(s -> s.length() > 3);
        System.out.println(allMatch);
        System.out.println("-------------");

        //anyMathc():判断流中的元素是否有匹配给定的条件
        boolean anyMatch = list.stream().anyMatch(s -> s.length() > 3);
        System.out.println(anyMatch);
        System.out.println("-------------");

        //noneMathc():判断流中的元素是否都不匹配给定的条件
        boolean noneMatch = list.stream().noneMatch(s -> s.length() < 2);
        System.out.println(noneMatch);
        System.out.println("-------------");

        //count统计
        long count = list.stream().count();
        System.out.println(count);
        System.out.println("-------------");

        //reduce归纳合并
        List<Integer> list2 = Arrays.asList(6,21,87,34,1,78,54);

        //求和,指定初始值
        Integer reduce = list2.stream().reduce(0, Integer::sum);
        System.out.println(reduce);
        //求和,没有初始值
        Optional<Integer> reduce1 = list2.stream().reduce((x,y) -> x + y);
        System.out.println(reduce1.get());
        //求最大值
        Integer max = (list2.stream().reduce(Math::max)).get();
        System.out.println(max);
        //求最小值
        Integer min = (list2.stream().reduce(Math::min)).get();
        System.out.println(min);
        System.out.println("-------------");

        //collect归约
        //把stream流转换为集合
        Set<String> set = list.stream().collect(Collectors.toSet());
        System.out.println(set);
        System.out.println("-------------");

        //把stream流转换为数组
        Object[] objects = list.stream().toArray();
        System.out.println(Arrays.toString(objects));
        System.out.println("-------------");

        //将stream流转换为字符串
        String collect = list.stream().collect(Collectors.joining("-"));
        System.out.println(collect);
        System.out.println("-------------");


    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值