Java 8,Stream流的常用操作

前言:创建流的常用方式
1.Stream的of 方法
Stream stream = Stream.of(“a”, “b”, “c”);
2. Arrays
String [] strArray = new String[] {“a”, “b”, “c”};
stream = Stream.of(strArray);
stream = Arrays.stream(strArray);
3. Collection
List list = Arrays.asList(strArray);
stream = list.stream();

 private List<Integer> list=Stream.of(10,20,30,40,50).collect(Collectors.toList());
forEach,peek
  • 遍历操作
    • forEach,peek
    • forEach是"终端化的",是遍历流中的每一个元素,执行完后相当于”流用光了“;
    • peek是"过程化的",遍历流中的元素,只是做了转换,返回的仍然是一个流,仍然可以继续对数据进行操作
 public void test1(){
        list.stream().map(x->x%20==0 ? "真":"假").forEach(x-> System.out.print(x+"  "));
        List<Double> collect = list.stream().
                filter(x -> x % 20 == 0).
                peek(x-> System.out.print(x+"  ")).
                map(x -> x + 0.1).
                peek(x-> System.out.print(x+"  ")).
                collect(Collectors.toList());
        System.out.println(collect);
    }
假  真  假  真  假  20  20.1  40  40.1  [20.1, 40.1]
map
  • map操作相当于对传入的对象进行转换成为另一个对象,1对1
public void test2(){
       list.stream().map(x -> x + 10).forEach(System.out::println);
    }
20
30
40
50
60
flatMap,distinct
  • flatMap,distinct
    • List集合中包含着四个list,flatMap就是将输入的流的层次结构扁平化,抽取四个小集合中的每一个元素
public void test5(){
        List<List <String>> list=Stream.of(
                Arrays.asList("张三","姓张"),
                Arrays.asList("李四","姓李"),
                Arrays.asList("张五","姓张"),
                Arrays.asList("李六","姓李")).collect(Collectors.toList());
        System.out.println(list);
        List<String> stringList = list.stream().
                flatMap(childList->childList.stream()).
                distinct().collect(Collectors.toList());
        System.out.println(stringList);
    }
[[张三, 姓张], [李四, 姓李], [张五, 姓张], [李六, 姓李]]
[张三, 姓张, 李四, 姓李, 张五, 李六]
filter
  • filter过滤,生成一个新的流,并不是在原有的流中删除。
public void test3(){
       list.stream().filter(x->x>30).forEach(System.out::println);
    }
40
50
reduce
  • reduce,聚合,以下两种方式等价。
public void test4(){
        Integer integer1= list.stream().filter(x -> x > 10).reduce(0,(a,b) -> a+b);
        Integer integer2= list.stream().filter(x -> x > 10).reduce(0,Integer::sum);
        System.out.println(integer1+"===="+integer2);
    }
140====140
match
  • anyMatch,allMatch,noneMatch
    • anyMatch只要流中的任何一个元素满足就返回true
    • allMatch只要流中有一个不满足条件就返回false
    • noneMatch只要流中有一个满足条件就返回false
public void test6(){
        boolean b1 = list.stream().allMatch(x -> x > 30);
        boolean b2 = list.stream().anyMatch(x -> x > 30);
        boolean b3 = list.stream().noneMatch(x -> x > 30);
        System.out.println("allMatch:"+b1+"\nanyMatch:"+b2+"\nnoneMatch:"+b3);
    }
allMatch:false
anyMatch:true
noneMatch:false
count
  • 统计流中元素的个数
 public void test7(){
        long count = list.stream().filter(x -> x > 20).count();
        System.out.println(count);
    }
3
findFirst
  • Optional,ofNullable使用它的目的是尽可能避免 NullPointerException.
  • findFirst 获取容器中第一个元素,返回值是Optional类型。
public void test9(){
        Integer first = list.stream().findFirst().get();
        System.out.println(first);
        List<Integer> list1=null;
        Integer integer = Optional.ofNullable(list1).map(List::size).orElse(-1);
        System.out.println(integer);
    }
10
-1
sorted,limit,skip
  • sorted,limit,skip
  • sorted排序,可自行指定排序规则,limit,取出流中前n个元素,skip扔掉流中前n个元素
public void test10(){
        //反序
       list.stream().sorted(Comparator.reverseOrder()).limit(3).forEach(x-> System.out.print(x+"  "));
       //正序
       list.stream().sorted(Integer::compareTo).limit(3).forEach(x-> System.out.print(x+"  "));
       //skip
        list.stream().sorted(Integer::compareTo).skip(3).forEach(x-> System.out.print(x+"  "));
    }
50  40  30  10  20  30  40  50 
max/min
  • max/min,相比于sorted来说时间复杂度更小。
public void test11(){
        Integer integer = list.stream().distinct().max(Integer::compareTo).get();
        System.out.println("最大值:"+integer);
        Integer integer1 = list.stream().distinct().min(Integer::compareTo).get();
        System.out.println("最小值:"+integer1);
    }
最大值:50
最小值:10
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值