java Beginners 18 --- lambda and stream

lambda believe that we have met in a variety of programs, is that an anonymous function, stream Judging from the name, this is a stream, just like python in principle yeild achieve iterator like, you can represent infinite data, but only a little memory, when making the call can be calculated in real time out and return.

So let's look at lambda usage, this is actually a collection before and has been used in multiple threads, and now again look

Import java.util.Arrays; 

public  class the HelloWorld {
     public  static  void main (String [] args) throws Exception { 
        String [] L = new new String [] { "B", "A", "C", "P", "E" };
         // this is the default sort 
        Arrays.sort (L); 
        System.out.println (String.Join ( ",", L));   // a, B, C, E, P 

        / / this is the method used our custom, this function is anonymous () -> {}, if the function thereof is only one line, the braces can be omitted and a return 
        Arrays.sort (L, (String S1, S2 String) -> { 
            the System .out.println ( "the Compare" );
            return- s1.compareTo (S2); 
        }); 
        Arrays.sort (L, (S1 String, String S2) -> - s1.compareTo (S2)); 
        System.out.println (String.Join ( ",", L ));   // P, E, C, B, a 

        // method reference, if the same parameters, the return value is the same, then you can simply use this method reference 
        Arrays.sort (l, HelloWorld :: compare) ; 
        System.out.println (String.Join ( ",", L));   // A, B, C, E, P 
    } 

    static  int Compare (String S1, S2 String) {
         return s1.compareTo (S2); 
    } 
}

Next we look at the stream, if there is a demand now allows you to build a list of all natural numbers, if we directly use the list to accept, it is certainly not realistic, let's look at the use of the Stream

import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.function.Supplier;
import java.util.stream.Stream;

class NatureNum implements Supplier<Integer> {
    int n = 0;

    @Override
    public Integer get() {
        n++;
        return n;
    }
}

public class HelloWorld {
    public static void main(String[] args) throws Exception {
        // 可以使用Stream.of创建对象
        Stream <String> Stream.of S = ( "A", "B", "C" ); 
        s.forEach (the System.out :: the println);   // ABC 

        // use Stream.generate (supplier parameters achieved Interface) generating the data stream 
        stream <Integer> = L Stream.generate ( new new NatureNum ());
         // filter filtration, map pond method of performing a number of limit cycles forEach print 
        l.filter (n -> n% 2 == 0) .map (n--> * n-2) .limit (10) .forEach (the System.out :: the println);   // . 4. 8 12 is 20 is 16 24 28 32 36 40
         // files can also be used liu use files. stream lines may generate 
        the try (stream <String> F = Files.lines (Paths.get ( "./ the src / t.txt" ))) { 
            f.forEach (the System.out :: the println);  // hello
        }

    }
}

Guess you like

Origin www.cnblogs.com/yangshixiong/p/12182773.html