java8 - method reference

 

java8 new features - method reference

Method references introduced by java8:

There are 4 types:

 * 1. Constructor reference - class name::new
 * 2. Static method reference - class name:: method name
 * 3. Common method reference - instance name:: method name
 * 4. Any reference to a certain type Ordinary methods of object instances-class name:: instance method name (more difficult to understand) is actually 3. ordinary method reference

Method reference is also to simplify the cumbersome code, similar to Lambda expressions, it is easier to understand after learning Lambda method references

 

Use code to reflect:

      1 Constructor reference Format Class name: new

   Create function interface

	/*
	 * 获取类的实例
	 */
	@FunctionalInterface
	interface function01<T>{
		 T getObject();
	}
	

 Create entity class

package com.xiaodu.java8.methodcite;

public class Person  {

	private String name;
	
	public Person(){
		this.name = "xiaoming";
	}

	@Override
	public String toString() {
		return "Person [name=" + name + "]";
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public void hello(String str){
		System.out.println("hello " + str);
	}

}

 Test constructor method reference

	/**
	 * 构造器引用
	 * 	类名::new   (Person::new)
	 */
	@Test
	public void test01() {
//		使用构造器引用
		function01<Person> p = Person::new;	 
		Person pserson1 = p.getObject();
		System.out.println(pserson1);   //Person [name=xiaoming]
//		使用lambda表达式
		function01<Person> p2 = () -> new Person();
		Person person2 = p2.getObject();
		System.out.println(person2);    //Person [name=xiaoming]
		
	}

 2. Static method reference Format class name:: static method

     Create static method

	private static void printMess(String str){
		System.out.println("printMess  = " + str);
	}
	

Create function interface (function interface can also use java8 built-in, don’t need to create it yourself)

	@FunctionalInterface
	interface function02{
		void print(String s);
	}

 Test static method references

	/**
	 * 静态方法引用
	 * 	  类名::方法名
	 */
	@Test
	public void test02() {
//		静态方法引用
		function02 f1 = TestMethodCite::printMess;
		f1.print("xiaohong");  //printMess  = xiaohong
//		lambda
		function02 f2 = mess -> TestMethodCite.printMess(mess);;
		f2.print("xiaoming");   //printMess  = xiaoming
		
	}

 Common method reference Format: Object instance:: Common method

    Create ordinary methods

	public int add(int a,int b) {
		return a+b;
	}

Create function interface

	@FunctionalInterface
	interface function03{
		int operation(int a,int b);
	}

 Use normal method references

	
	/**
	 * 普通方法引用
	 */
	@Test
	public void test03() {
//		普通方法引用
		function03 f = new TestMethodCite() :: add;
		int operation = f.operation(12, 23);
		System.out.println(operation);  //35
//		lambda
		function03 f2 = (a,b) -> a+b;
		System.out.println(f2.operation(12, 12));  // 24
		
	}

 4. A common method that refers to an instance of any object of a certain type, looks the same as the class name:: static method, in fact it is still an object instance name:: common method name

                Format class name: common method name

/**
	 * 引用某个类型的任意对象的实例的普通方法
	 *   和类名:: 静态方法一样的格式
	 *   格式:
	 *     类名:: 普通方法
	 */
	@Test 
	public void test04() {
		/* public int compareToIgnoreCase(String str) 是String 的普通方法
		/  接受一个参数,而sort 的第二个参数为Comparator<? super T> c 类型		
		/  而comparator 是接受两个参数的函数接口  int compare(T o1, T o2);
		 * 总结:一个参数的方法,使用了两个参数的函数方法,不合理,原因是,在排序的时候,每个元素已经是对象了,
		 * 其实底层还是用的   类的实例::普通方法名 这样的模式  -- 也就是普通方法引用只是表面看起来像类名::静态方法这样的引用
		*/		
		Arrays.sort(new String[]{"asd","ddd"}, String::compareToIgnoreCase);
//		是用lambda 表达式 就可以看出来端倪了 
		Arrays.sort(new String[]{"asd","ddd"},(o1,o2) -> o1.compareToIgnoreCase(o2));
		
		/**
		 * 第二种分析,
		 */
//		创建一个 包含TestMethodCite 的集合
		List<TestMethodCite> asList = Arrays.asList(new TestMethodCite());
//		使用方法引用
		asList.forEach(TestMethodCite::str);
//		使用lambda   o 是从集合中取出来的 对象
		asList.forEach(o -> o.str());
		
	}

 

Guess you like

Origin blog.csdn.net/xiaodujava/article/details/84316759