[Java_22] JDK New

Keywords: Programming Java Lambda JDK

JDK 1.8 New

1. Functional interface

1. Functional interface

(1) Concepts
    *Interfaces with only one abstract method
 (2) Format
    @FunctionalInterface //Use this comment to determine if the interface is a functional interface.
    public class class name {
        public abstract return value method name (parameter list);
    }

2. Functional programming

① Lambda delay
    * Lambda Do not use Do Not Execute
② Example
    //Define functional interfaces
    @FunctionalInterface
    public interface Inter {
        String show();
    }

    public class Demo {
        public static void method(int i, Inter inter) {
            //Lambda splices strings when i is greater than 10, otherwise they are not spliced
            if (i > 10) {
                System.out.println(inter.show());
            }
        }

        public static void main(String[] args) {
            String s1 = "you";
            String s2 = "good";
            String s3 = "!";
            
            //Lambda will not stitch the strings at this time
            method2(5,()-> s1 + s2 + s3);
        }
    }

3. Common Functional Interfaces

① Supplier (Producer)
    * What to Give(Return a data based on a generic)
    * Example
        import java.util.Date;
        import java.util.function.Supplier;

        public class demo_Supplier {
            //Get String
            public static String getString(Supplier<String> sup) {
                return sup.get();
            }
 
            //get date
            public static Date getDate(Supplier<Date> sup) {
                return sup.get();
            }
 
            public static void main(String[] args) {
                String s = getString(() -> "I Love Java");
                System.out.println(s);
 
                Date time = getDate(() -> new Date());
                System.out.println(time);
            }
        }
 
② Consumer(Consumer)
    * What to use?(Pass in a data to use it)
    * Abstract methods: accept(T t) Consume a data
    * Default method: anfThen(Consumer<T> con) Combination consumption
    * Example
        import java.util.function.Consumer;
 
        public class demo_Consumer {
            public static void and(String s, Consumer<String> con1, Consumer<String> con2) {
                System.out.println("-----------noAndThen------------");
                con1.accept(s);
                con2.accept(s);
                
                //Whoever consumes first has the same effect as above
                System.out.println("------------andThen-------------");
                con1.andThen(con2).accept(s);
            }
 
            public static void main(String[] args) {
                //Call and method
                and("consumption", 
                s-> System.out.println("\t\t\t" + s + "1 Number"), 
                s-> System.out.println("\t\t\t" + s + "2 Number"));
            }
        }
        
③ Predicate(judge)
    * Judgement of incoming data
    * Abstract methods: test(T t) Judge a Data
    * Default Playback: and     --->     &&
                or      --->     ||
                negate     --->     !
    * Example    
        import java.util.function.Predicate;
 
        public class demo_Predicate {
            public static void show(String s, Predicate<String> pre) {
                if (pre.test(s)) {
                    System.out.println("yes java file");
                } else {
                    System.out.println("No java file");
                }
            }
 
            public static void main(String[] args) {
                show("test.abc.java.txt",str->{
                    String[] s = str.split("\\.");
                    return "java".equals(s[s.length - 1]);
                });
            }
        }
        
④ Function(Transformation)
    * Pass in one type to get another
    * Abstract methods: R apply(T t) afferent T Return R
    * Default method: anfThen(T t)
    * Example
        import java.util.function.Function;
 
        public class Demo {
            public static void method(String s, Function<String, Integer> fun) {
                System.out.println(fun.apply(s));
            }
            
            public static void main(String[] args) {
                method("123",str -> Integer.parseInt(str));
            }
        }

2. Stream Flow

1. Streaming Thought

*Similar to factory pipelining, when multiple elements need to be operated on, we first evaluate the model and use it directly in time

2. Get Stream

① Collection
    * stream() Method
        //All single-column collections can use Collection.stream() to get streams
② Map
    Set<T> set = map.keySet();
    Stream<T> stream = set.stream();
    
    Set<T> set = mao.values();
    Stream<T> stream = sset.stream();
    
③ aggregate
    * Static method of()
    * Example
        Stream<T> stream = Stream.of("a", "b", "c");
        
        String s = ("a", "b", "c");
        Stream<T> stream = Stream.of(s);

3. Common methods

① filter(filter)            //Delay Method
② map(mapping)             //Delay Method
③ limit(Intercept)            //Delay Method
④ skip(skip)            //Delay Method
⑤ concat(Combining two streams)    //Delay Method
⑥ count(count)            //End Method
⑦ forEach(Processing one by one)        //End Method
⑧ Example
    import java.util.stream.Stream;

    public class Demo_Map {
        public static void main(String[] args) {
            Stream<String> stringStream = Stream.of("1", "2", "3", "4", "5", "6", "7", "8", "9", "10");
 
            stringStream
                    .map(s -> Integer.parseInt(s))            //Map String to Integer
                    .filter(s -> s > 5)                        //Number less than 5 filtered
                    .skip(1)                                //Skip one
                    .limit(2)                                //Intercept the first two
                    .forEach(s -> System.out.println(s));    //ergodic
        }
    }
⑨ Be careful
    * After finalization method execution stream Loss effect

3. Method Citation

1. Concepts

*If an existing method can do the work in a functional interface, Lambda can be replaced with a method reference 
    For example: print (s-> System.out.println(s))
        → print(System.out::println)

2. Method reference (:)

@FunctionalInterface
public interface Inter {
    int show();
}

public class Demo {
    public static void method(Inter inter) {
        int a = inter.show();
        System.out.println(a);
    }

    public static void main(String[] args) {
        String s = "123";
        method(s::length);
    }
}

3. Attention

*Class names can only reference static methods, member methods need to use object name references
 * super::xxx refers to the parent class member method, this::xxx refers to this class member method
 *Class constructor references newPerson("King Hammer", Person::new) //creates an object with name = "King Hammer")
*Array constructor reference newArray(5,int::new)//create an integer array of length 5

4. Personal understanding

*A reference to a method starts by replacing an abstract method of a functional interface with a reference method
 * Take the above code as an example
    Replace the show() method with the length() method; equivalent to s.length();

JDK 9 and above features are not described yet

Posted by starnol on Thu, 13 Feb 2020 12:55:40 -0800