The lambda expression of java

Keywords: Programming Lambda Java Junit

Why use Lambda expressions

Lambda is an anonymous function. We can understand lambda expression as a piece of code that can be passed (passing code like data). Use it to write more concise and flexible code. As a more compact code style, Java language expression ability has been improved.

Lambda expression

Lambda expression syntax

Lambda expression: a new syntax element and operator introduced in Java 8. This operator is "- >", which is called lambda operator or arrow operator. It divides lambda into two parts:

 

Left: Specifies the list of parameters required by the Lambda expression

Right: Specifies the body of the Lambda, which is the implementation logic of the abstract method, that is, the function to be performed by the Lambda expression.

 

 

package day26_1;

import org.junit.Test;

//Write an interface B, which has an abstract method string upper (string string string);
//Anonymous inner class is used to implement this interface, in which the string in the parameter is capitalized and returned
//This interface is implemented by lambda expression, in which the string in the parameter is capitalized and returned
interface B {
	String upper(String string);
}


// Most conventional approach
class B2 implements B {
	@Override
	public String upper(String string) {
		return string.toUpperCase();
	}
}
/*
interface C {
	Integer max(Integer n1, Integer n2); // Maximum value
}
*/
interface C<X extends Comparable> { // X is a generic here, indicating a type
	X max(X n1, X n2); // Take the maximum of two objects
}
/*
 * Write an interface C to define the abstract method Integer max(Integer n1, Integer n2);
 *In the test method, anonymous inner class objects are used to complete the implementation of this interface, and methods are called to print out
 *Use lambda expressions to do the same 
 * */
public class LambdaExer {
	
	@Test
	public void test2() {
		C<Integer> c = new C<Integer>() {
			@Override
			public Integer max(Integer n1, Integer n2) {
				return n1 > n2 ? n1 : n2;
			}
		};
		Integer max = c.max(10, 50);
		System.out.println(max);
		
		// lambda expression: focus on parameter list - > method body 
		//C c2 = (Integer n1, Integer n2) -> {return n1 > n2 ? n1 : n2;};
		C<String> c2 = (n1, n2) -> n1.compareTo(n2) > 0 ? n1 : n2; // Most versatile
		String max2 = c2.max("asfj", "ASFJ");
		System.out.println(max2);
	}
	
	@Test
	public void test1() {
		B b1 = new B2();
		String string1 = b1.upper("abcdefg");
		System.out.println(string1);
		
		// Anonymous Inner Class 
		B b2 = new B() {// Class body 
			@Override
			public String upper(String string) {
				return string.toUpperCase();
			}
		};
		String string2 = b2.upper("abcdefg");
		System.out.println(string2);
		// lambda expression, only focusing on abstract method parameter list - > method body
		B b3 = string -> string.toUpperCase();
		String string3 = b3.upper("abcdefg");
		System.out.println(string3);
	}
}


package day26_1;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.junit.Test;

/**
 * lambda Expression: interface is required. There must be only one abstract method in the interface and only one line of statement in the method body of method implementation
 * 	Syntax: parameter list - > method body 
 * lambda Expression is essentially an object of an anonymous inner class, and the type can only be interface type
 * 
 * 	Mainly used to replace anonymous inner class objects
 */

@FunctionalInterface
interface A {
	public String test(Integer num);
	//public Integer test2(String num);
}



public class LambdaTest {
	
	@Test
	public void test3() {
		A a1 = new A() {
			@Override
			public String test(Integer num) {
				return String.valueOf(num);
			}

		};
		// Parameter types can be omitted, and () is also omitted if there is only one parameter in the parameter list
		A a2 = num -> String.valueOf(num);
		String string = a2.test(200);
		System.out.println(string);
	}
	
	@Test
	public void test2() {
		List<Student> list = StudentData.getList();
		// Comparator is used to compare two student objects
		Comparator<Student> comparator = new Comparator<Student>() {
			// The class body is equivalent to the implementation subclass of the interface
			@Override
			public int compare(Student s1, Student s2) {
				return (int)(s1.getScore() - s2.getScore());
			}
		};
		Collections.sort(list,comparator);
		
		for (Student student : list) {
			System.out.println(student);
		}
		
		System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
		// lambda expression to do this requires the score to be in descending order
		// If there is only one line in our method body, you can omit {} or return
		//Comparator<Student> comparator2 = (Student s1, Student s2) -> {return (int)(s2.getScore() - s1.getScore());};
		// Because generics already exist in the type on the left, the type in all expressions on the right can be inferred, so it can be omitted
		//Comparator<Student> comparator2 = (Student s1, Student s2) -> (int)(s2.getScore() - s1.getScore());
		Comparator<Student> comparator2 = (s1, s2) -> (int)(s2.getScore() - s1.getScore());
		Collections.sort(list, comparator2);
		
		for (Student student : list) {
			System.out.println(student);
		}
	}
	
	@Test
	public void test1() {
		Runnable r1 = new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + " : hello");
			}
		};
		
		new Thread(r1).start();
		
		// lambda expression: omit parent class or interface, method modifier and return value, method name, only parameter list, and method body
		// lambda expression is essentially an object of an anonymous inner class
		Runnable r2 = () -> System.out.println(Thread.currentThread().getName() + " : Hello2 ");
		
		new Thread(r2).start();
	}

}

Posted by merrittj on Wed, 13 Nov 2019 06:34:03 -0800