Stream Study Notes

1. Create an instance of Stream Five Ways

  @Test
     public  void test1 () {
         // a first way to create an object Stream 
        List <String> Lists.newArrayList List = ( "A", "B", "C" ); 
        Stream <String> STREAM1 = list.stream (); 

        // Create a Stream object of the second embodiment 
        String [] ARR = { "a", "B" }; 
        Stream <String> STREAM2 = Arrays.stream (ARR); 

        // Create a Stream object third embodiment 

        Stream <String> STREAM3 Stream.of = ( "a", "B" ); 

        // create a fourth embodiment Stream object
         // the iterate indefinitely performed, so limit only the leading 5 
        Stream <Integer>stream4 = Stream.iterate(0, x -> x * x).limit(5);

        // Create a Stream object fifth embodiment
         // Generate will perform unlimited, so limit only the leading 5 
        Stream <Double> Stream5 = Stream.generate (the Math :: Random) .limit (. 5 ); 
    }

 

2. Stream commonly Api

 @Test
    public void test2() {
        List<String> list = Lists.newArrayList("abc", "bcd", "efg", "a12", "bcd");
        // 1.过滤
        list.stream().filter(x -> x.contains("b")).forEach(System.out::println);

        // 2.取前n
        list.stream().limit(1).forEach(System.out::println);

        //3. 跳过前n个
        list.stream().skip(2).forEach(System.out::println);

        //4. distinct 去重
        list.stream().distinct().forEach(System.out::println);

        //5. map映射
        list.stream().map(x -> x.toUpperCase()).forEach(System.out::println);

        System.out.println ( "-----------------------" );
         // 6. The flatMap map (in the scala map, flatMap same) , returns a Stream 
        list.stream () flatMap (X ->. Stream.of (x.toUpperCase ())) forEach (the System.out :: the println);. 


        // 7. the sort sorted () ---> natural ordering 
        List <Integer> = Lists.newArrayList List1 (12 is,. 1, 0, -1, 2, 56 is, 34 is ); 
        .. list1.stream () the sorted () forEach (the System.out :: the println); 

        // 8. Sort sorted (Comparator com) -> Sort specify the rule 
        list1.stream () the sorted. ((E1, E2) -> - . Integer.compare (E1, E2)) forEach (the System.out :: the println) ; 

        // 9. match lookup
         // all elements in the list contains "a" 
        Boolean= list.stream RET () allMatch (X -> x.contains ( "A." )); 
        System.out.println (RET); 

        // List long as there is an element with "a", it is to true 
        RET = List . .stream () AnyMatch (X -> x.contains ( "a" )); 
        System.out.println (RET); 

        // List not to "a" at the beginning of an element 
        ret = list.stream () noneMatch. (X -> x.startsWith ( "a" )); 
        System.out.println (RET); 

        // first element 
        Optional The <first String => . list.stream () the findFirst (); 
        System.out.println (first.get ()); 

        // find an arbitrary element 
        Optional The <String> = the any list.parallelStream () findAny ();. 
        the System.out.println(any.get());

        //Get the maximum 
        Optional The <Integer> = max list1.stream () max (the compareTo :: Integer);.
         // The above line is shorthand for this code
 //         Optional The <Integer> MAX1 = list1.stream () max (. (E1, E2) -> Integer.compare (E1, E2)); 
        System.out.println (max.get ()); 

        // 10, reduction 
        Optional <Integer> reduce = list1.stream ( ) reduce (. (X, Y) -> X + Y);   // List1 summing elements 
        System.out.println (reduce.get ()); 

        Integer SUM = list1.stream () the reduce (0, Integer :: SUM). ;   // List1 summing elements 
        System.out.println (SUM); 

        Integer SUM2 = list1.stream () the reduce (10, Integer :: SUM);.   //10 + list1 summing elements 
        System.out.println (SUM2); 

        // 11. The the collect -> stream will turn into a set of list 
        List <Integer> collect = list1.stream ( ) map (x -> x * 2. ) .collect (Collectors.toList ()); 
        collect.forEach (the System.out :: the println); 

    }

 

Guess you like

Origin www.cnblogs.com/z-qinfeng/p/11762114.html