Method reference, built-in functional interface

From the beginning, all references are done for reference types, that is, only arrays, classes and connectors have reference operations.
The combination of function programming with normal method makes
Static method
valueOf() method of String class
IUtil1 function interface (function): integer - > string
String.valueOf(x): object oriented content, static method of string class
Iutil1 - > lambda expression implementation function specific logic
IUtil1 function can reuse the existing logic in OO

package com.changehyh.github.function;

public class TestStaticMethodRef {
    public static void main(String[] args) {
    //        IUtil<Integer,String> iUtil = (p)->{
//            //p calculation + character splicing
//          return String.valueOf(p);
//        };
        //The iUtil1 variable is now the alias of the String.valueOf method.
        // y  = f(x)
        // f -> { }
        // f = String::valueOf
        // f(x)
        IUtil<Integer,String> iUtil1 = String::valueOf;
        System.out.println(iUtil1.convert(10));
    }
}
@FunctionalInterface
interface IUtil<P,R>{
    //Convert parameter of parameter type Integer to String
    //y = f(x)  x: Integer   y: String
    //f: transformation logic
    R convert(P p);
}

Reference object method:
The toUpperCase() method in String is the object method

package com.changehyh.github.function;

//Reference to image method

public class TestMemberMethodRef {
    public static void main(String[] args) {
//        IUtil2<String> iUtil2 = new IUtil2<String>() {
//            @Override
//            public String switchPara() {
//                return "hello world".toUpperCase();
//            }
//        };
//        System.out.println(iUtil2.switchPara());

        //String member method toUpperCase()
        IUtil2<String> iUtil21 = "hello world"::toUpperCase;
        System.out.println(iUtil21.switchPara());
    }
}
@FunctionalInterface
interface IUtil2<R>{
    R switchPara();
}

In the reference class, the common method is as follows:
String has two compareTo methods, which are common methods

package com.changehyh.github.function;
// Common method in the citation class
public class TestClassMemberMethodRef {
    public static void main(String[] args) {
        //p1 > p2  return  >0
        //p1 = p2  return  0
        //p1 < p2  return  <0
        IUtil3<Integer,Integer> iUtil3 = (p1,p2)->{
            return p1-p2;
        };
        System.out.println(iUtil3.compare(20,10));

        //int -> Integer
//        Integer   Integer(object).compareTo(Integer)
//        Integer   compare(Integer,Integer)
//        Integer a = new Integer(10);
//        a.compareTo(new Integer(12));

        //Method reference by type:: member method
        IUtil3<Integer,Integer> iUtil31 = Integer::compareTo;
        System.out.println(iUtil31.compare(20,10));
    }
}
@FunctionalInterface
interface IUtil3 <P, R>{
  //Compare parameters p1 and p2 of type P to return R
  //Y = f (m, n) [M N same type]
  R compare(P p1,P p2);
}

Construction method:

package com.changehyh.github.function;

public class TestConstructorMethodRef {
    public static void main(String[] args) {

        Person person1 = new Person();
        System.out.println(person1);
        Person person2 = new Person(22,"hyh");
        System.out.println(person2);
        // y = f(m, n)
        // y = Person type
        // m = Integer type
        // n = String type
        // f = create a Person based on m n

        //new Person()
        //new Person(Integer,String)
        ObjectFactory<Integer,String,Person> factory = (p1,p2)->{
          return new Person(p1,p2);
        };
        Person person3 = factory.createObject(20,"Alice");
        System.out.println(person3);

        //Construction method reference
        ObjectFactory<Integer,String,Person> factory1 = Person::new;
        Person person4 = factory1.createObject(20,"TOM");
        System.out.println(person4);

        //Method reference: open function implementation and object-oriented method

    }
}
class Person{
    private Integer age;
    private String name;
    public Person(){

    }
    public Person(Integer age,String name){
        this.age = age;
        this.name = name;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {

        return age;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}
@FunctionalInterface
interface ObjectFactory<P1,P2,R>{
    R createObject(P1 p1,P2 p2);
}

Built in function connection

package com.changehyh.github.function;

import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

public class TestBuildInFunction {
      /**
        y = f(x) : After the input parameter x is processed, the result y is returned
        y = f()  : Function mentioned a result y
        y(void) = f(x) : Give the input parameter x to handle it
        y(boolean) = f(x) :  Returns the result of the boolean class after processing the input parameter x (true false)

     */

      //Functional connection
      // y = f(x): the processed result y of the given parameter x
      public static void function(){
//          Function<Integer,String> function = (x)->{
//              return String.valueOf(x);
//          };
          Function<Integer,String> function = String::valueOf;
          System.out.println(function.apply(10));

          //Lambda
          print((p)->{
              System.out.println("xxx");
              return String.valueOf(p);
              },20);
      }

      // Supply type connection
      //y = f(): function mentions a result y
      public static void supplier(){
          Supplier<String> supplier = ()->{
            return "Hello World";
          };
          System.out.println(supplier.get());
          //get() of T;
          // Object();-> object
          print(Object::new);
      }

      //Consumer connection
      public  static void consumer(){
          Consumer<String> consumer = (x)->{
              System.out.println(x);
          };
          consumer.accept("Hello");
          consumer = System.out::println;
      }

      //Assertion interface
      public static void predicate(){
          Predicate<String> predicate = (x) ->{
              if (x==null){
                  return false;
              }else {
                  return x.length()> 2;
              }
          };
          System.out.println(predicate.test("hello"));
          System.out.println(predicate.test(" "));
      }


    public static void main(String[] args) {
          //y = f()
        Supplier<String> supplier = ()->{
            Random random = new Random();
          return String.valueOf(random.nextInt(200));
        };

        //z = g(x)
        Predicate<Supplier<String>> predicate = (x) ->
        {
            String value = x.get();
            System.out.println(value);
            if (value==null){
                return false;
            }else {
                return value.length()>2;
            }
        };

        //z = g(f())
        boolean rs = predicate.test(supplier);
        System.out.println(rs);
    }

      public static void print (Function<Integer,String> function,Integer p){
        System.out.println(function.apply(p));
      }

      public static void print(Supplier<?> supplier){
          System.out.println(supplier.get());
      }
}

Tags: github Java Lambda Programming

Posted on Sat, 09 Nov 2019 12:25:25 -0800 by railgun