Introduction of the concept of java learn lambda expression

The essence of lambda expression is for convenience
The essence of expression is the formula we usually write when coding code, such as conditional statement or judgment statement
1. Common methods
For example, the enhanced for loop is used to traverse the entire container class, and the generic type of the container class is Integer. if you want to add an if statement to it, you must have a formula with corresponding constraints to determine the number that meets the requirements

2. Anonymous class
After providing an interface, define a class implements the interface

Te t=new Te() {
			  public void sf()
			  {
				  
			  }
		  };

The method sf() is that you need to add conditional statements to achieve code functions
Note that the data type returned by the method and return are generally the statements you write rather than a specific variable, and then put the object instantiated by the interface into another method of the main implementation to set parameters

private static void filter(List<Hero> heros,Te R)
{
}

When calling the above methods, you can put the expression directly without passing Te parameters

3. The above two methods introduce lambda expressions step by step
The following is the correct way to write a lambda expression
Code example - easy to explain:

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

public class collection_test  {
	
	public static void main(String[] args) {
		collection_test df=new collection_test();
		 Random sd=new Random();
		List<Hero> L=new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			L.add(new Hero("Hero-"+i,sd.nextInt(100),sd.nextInt(100)));
		}
		System.out.println(L);
		Te t=new Te() {
			public boolean sf(Hero h)
			{
				if(h.blood<80&&h.damage<90)
				{
					return  true;
				}
				else
				{
					return false;
				}
			}
		};
		 Comparator<Hero> h=new Comparator<Hero>() {
			 public int compare(Hero o1,Hero o2)
			 {
				 if(o1.blood<o2.blood)
					 return 1;
				 else
					 return -1;
			 }
		};
		
		Collections.sort(L,h);
		System.out.println(L);
		df.seek(L, t);
		
		
	}
	public void seek(List<Hero> k,Te e)
	{
		for (Hero hero : k) {
			if(e.sf(hero))
			{
			System.out.println(hero);
			}
		}
	}

}

Hero class

public class Hero{
	public  String name;
	public int blood;
	public int damage;


	public Hero() {
		
	}
	public Hero(String name,int blood,int damage)
	{
		this.name=name;
		this.blood=blood;
		this.damage=damage;
	}
	
	public String toString()
	{
		return name+"-"+blood+"-"+damage;
	}
	
}

Emphasize the following:
Moreover, I noticed the previous rewriting of toString() method in Hero class, but this time I really understand the function of the return statement of toString method - it returns the data to be output after instantiating the object, and connects each String through '+' to achieve the purpose. Therefore, the subsequent toString is not only the return of String type data, but also the attributes in this class can be output together. Therefore, why should we pay attention to rewriting toString according to the output requirements

Explanation of lambda expression:
The statements in the code -- h.blood < 80 & & h.damage < 90 are the expressions that are really needed
But there are many ways to achieve it
The correct lambda expression is: H - > h.blood < 80 & & h.damage < 90
Therefore, it is not necessary to write an anonymous class as in the example code, and put its reference as a parameter into the seek method -- seek (L, H - > h.blood < 80 & & h.damage < 90)
It can be understood in combination with common methods——

for (Hero hero : k) {
			if(hero.blood<80&&hero.damage<90)
			{
			System.out.println(hero);
			}
		}

Note that h in the equation H - > h.blood < 80 & & h.damage < 90 is also a variable. Do not repeat with other variables - > it is the characteristic variable of lambda expression plus - > followed by its statement
There are also multiple parameters (h, e) - >... The preceding parameters and the following parameters are of the same type

From the anonymous class writing method to realize the compression into lambda expression, we can see that the essence is to call the method of user-defined interface, and only write the conditional statements in the method directly
Therefore, it is concluded that lambda expression is also an anonymous method, and directly passing anonymous methods as parameters is also a programming idea, just like the previous space for time

Solutions to the problem that eclipse cannot recognize lambda expressions normally:
Menu - > window - > Preferences - > java compiler - > compiler compliance level: set it to 1.8
But now JDK versions are very high and should not exist. Therefore, Java will convert the anonymous method into anonymous class method and concise code.

For lambda expressions, short code can be used with caution in the face of large and complex code volume of enterprise projects, because it is difficult to debug due to poor readability

Tags: Java Back-end

Posted on Fri, 12 Nov 2021 15:24:01 -0500 by DamianTV