jdk1.8 -- stream 的使用

一.stream介绍

  stream 是jdk 一个加强的api操作,翻译过来就是流的意思,主要是对Collection 集合的一些操作,流一但生成就会有方向性,类似于自来水管的水流一样,不可以重复使用。

  stream 中的操作有filter map limt sorted collect

二.生成stream的方式

  集合.stream(); − 为集合创建串行流

  集合.parallelStream()  − 为集合创建并行流

//列举一些常见的创建stream的方法
List<Apple> apples = Arrays.asList(new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)); Stream<Apple> stream = apples.stream(); Stream<Apple> stream2 = Stream.of(new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)); Apple arr [] = {new Apple("red", 120),new Apple("green", 170),new Apple("yellow", 200)}; Stream<Apple> stream3 = Arrays.stream(arr);

三.stream中的操作

 1.filter distinct skip limit 操作

public class StreamOperator {

    public static void main(String[] args) {
        
        List<Integer> asList = Arrays.asList(1, 2, 3, 4, 5, 7, 6, 4, 2, 1, 8, 6, 9, 3);

        // 1.filter 过滤偶数  将流按一定条件进行过虑  返回过虑后符合规则的stream
        List<Integer> filterList = asList.stream().filter(i -> i % 2 == 0).collect(Collectors.toList());
        System.out.println(filterList);
        
        //2.distinct 去重
        List<Integer> disList = asList.stream().distinct().collect(Collectors.toList());
        System.out.println(disList);
        
        //3.skip 跳过n个元素
        List<Integer> skipList = asList.stream().skip(5).collect(Collectors.toList());
        System.out.println(skipList);
        
        //4.limit 极限值是几个(个人理解,也就是最多取几个元素,如果limit取的个数超过了stream中元素的最大个数,会取到流中的全部元素)
        List<Integer> limList = asList.stream().limit(5).collect(Collectors.toList());
        System.out.println(limList);    }
}

 2.map flatmap操作

public class StreamTest {
    public static void main(String[] args) {
          
         //map 操作
         List<Apple> apples =Arrays.asList(
                            new Apple("green", 150),new Apple("red", 170),new Apple("yellow", 190),
                            new Apple("blue", 210));
        
        //map 操作是将stream 通过map操作返回符合map规则的stream
     // map 与filter 的区别
     //  map是一个Function 即传和一个 T 返回一个 R 类似于get操作 如果流中有元素符合map的操作条件,会将得到的R存入到stream中,会改变流中存储的元素 
     //  filter 是一个 Predicate 即传入一个 T 返回的是bollean 是用来做判断的,如果流中的某个元素符合filter的条件,就会存入到stream中,不符合的将被过滤掉,不会改变流中存储的元素

        Stream<String> map = apples.stream().map(Apple::getColor);
        
        //flatmap(扁平化)
        String [] arr = {"hello","world"};
        //{h,e,l,l,o} {w,o,r,l,d}
        Stream<String[]> streamMap = Arrays.stream(arr).map(w->w.split(""));
        
        //[h,e,l,l,o,w,o,r,l,d] 由flatMap的参数可知,传入的参数T最后都合成一个只要是继承stream的类即可,因此将streamMap流数组传入flatMap会得到1个stream流
        // flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
        //  R apply(T t);
        Stream<String> flatMap = streamMap.flatMap(Arrays::stream);
        flatMap.distinct().forEach(System.out::print);     
    }
}

 3.sort collect 

public class StreamTest {
    public static void main(String[] args) {
        List<Apple> apples =Arrays.asList(
                            new Apple("green", 150),new Apple("red", 170),new Apple("yellow", 190),
                            new Apple("blue", 210));
        
       //sorted 是按颜色的字母顺序来排序
     //collect 是将流转换成相对应的集合
List
<Apple> collect = apples.stream().sorted(Comparator.comparing(Apple::getColor)).collect(Collectors.toList()); } }

 

四.stream 的并行操作

  将集合转换成并行流后之后,其它操作与串行流相同

  并行流可以通过将程序休眠,通过jconsole 工具查看

原文地址:https://www.cnblogs.com/MrRightZhao/p/10947834.html