Home > Java > Javagetting Started > Detailed introduction to Lambda expressions in java

Detailed introduction to Lambda expressions in java

王林
Release: 2019-11-27 15:22:04
forward
2834 people have browsed it

Detailed introduction to Lambda expressions in java

What is a lambda expression?

A lambda expression is a transitive block of code that can be executed one or more times later.

Recommend java related video tutorials: java learning video

For example:

class action implements ActionListener{
	@Override
	public void actionPerformed(ActionEvent e){
		System.out.println("now time is"+new Date());
		Toolkit.getDefaultToolkit().beep();
	}

}
public class Main{
	public static void main(String[] args){
		action a=new action();
		Timer timer=new Timer(2000,a);
		timer.start();
		JOptionPane.showMessageDialog(null, "is quit?");
		//这个窗口用于防止main线程执行完毕直接结束
	}

}
Copy after login

The a object of the action class in this code is actually just a code The segment is passed to the constructor of Timer. Because Java is a language based on object-oriented thinking, there is no function call and no code segment transfer, so the required code must be encapsulated in a class, which is the action class above. With lambda expressions, the code can be made more concise.

The above is represented by lambda as follows:

public class Main{
	public static void main(String[] args){
		Timer timer=new Timer(2000,(ActionEvent e)->{
			System.out.println("now the time is"+new Date());
			Toolkit.getDefaultToolkit().beep();
		});
		timer.start();
		//结束
		
	}

}
Copy after login

Using lambda expressions can make the code very concise. If the type of e can be deduced through the following, then the parameter type can be omitted. If there is only one parameter, you can omit the parentheses, as follows:

e->{
System.out.println(“now the time is”+new Date());
Toolkit.getDefaultToolkit().beep();
}
Copy after login

This lambda expression can be equivalent to:

ActionListener al=e->{
System.out.println(“now the time is”+new Date());
Toolkit.getDefaultToolkit().beep();
}
Copy after login

Then throw al into the Timer constructor.

ActionListener As an interface, it has only one abstract method. This is a condition that must be met as a functional interface. Functional interfaces can be written as lambda expressions.

Then implement a code that sorts String arrays by length for easy understanding:

public class Main{
	public static void main(String[] args){
		String[] con=new String[]{"apple","cat","bannane","foot","do"};
		Arrays.sort(con,(str1,str2)->str1.length()-str2.length());
		//第一种实现  这儿因为直接返回值所以没有加"{"和"}"所以也不用加";" 上下文能推断出这是String类型参数,
		//所以不用加参数类型
		Comparator<String> compare = (str1,str2)->s1.length()-s2.length();
		Arrays.sort(con,compare);//第二种实现,还可以采用写一个类实现Comparator中的compare方法
		//如:
		com com1=new com();
		Arrays.sort(con.com1);//其本质都是传递一段代码
	}
	class com implements Compartor<String>{
		@Override
		public void compare(String str1,String str2){
			return str1.length()-str2.length();
		}
	}

}
Copy after login

This article is provided by the java Quick Start column to introduce lambda expressions in java in detail. Everyone is welcome to come and learn together!

The above is the detailed content of Detailed introduction to Lambda expressions in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template