Lambda expressions five minutes to learn in Java

Lambda expressions in Java

What is Lambda expressions

Lambda expressions are an anonymous function is added in jdk1.8
before jdk1.8, we can only use anonymous methods and inheritance interfaces to implement the method defined in the interface
using Lambda expressions are also limited, Lambda expression type can only have one and only method that is not implemented in the interface used
Therefore, in jdk1.8 also added a keyword default, the default method is marked by the need to write a default implementation

Simple use of Lambda expressions

First, let's define a few interfaces, these interfaces are defined only for convenience Lambda expressions show

/**
 * 			无返回值的接口
 * @author Administrator
 *
 */
interface SingleParameter
{
	  void printin(Object msg);
}
/**
 * 			有返回值的接口
 * @author Administrator
 *
 */
interface SingleParameterReturn
{
	Integer triploid(Integer num);
}
/**
 * 			多参数有返回值的接口
 * @author Administrator
 *
 */
interface MultiParameterReturn
{
	Integer triploid(Integer num,Integer num2);
}
/**
 *      无参有返回值的接口
 * @author Administrator
 *
 */
interface PersonCreater  {
	Person getPerson();
}
/**
 * 返回值对象
 * @author Administrator
 *
 */
class Person{
	private LambdaDemo demo;
	public Person()
	{
		demo=LambdaDemo.getLambdaDemo();
	}
	public LambdaDemo getDemo() {
		return demo;
	}
}

After defined the interfaces we use to operate the Lambda Expressions

SingleParameter singleParameter=s->{System.err.println(s);};			  // 这是无返回值的lambda表达式
singleParameter.printin("这是无返回值的lambda表达式");				

Because
SingleParameter interface only a
void printin (Object msg); method is not implemented
so
s -> {System.err.println (s) ;}; this we can be viewed as the code that implements the method
s for methods msg
-> represents the point the method body
{System.err.println (s);} implement the representative method

	无返回值的方法实现了我们再来看有返回值的SingleParameterReturn是怎么实现的
SingleParameterReturn singleParameterReturn=s->s*3;					  // 这是无返回值的lambda表达式
System.err.println(singleParameterReturn.triploid(12));		

SingleParameterReturn in unrealized method is
Integer triploid (Integer num);
however, in the above code we write only a small piece of code so you can replace this method
S-> S 3
S is still the same on behalf of num methods
-> point method body
S
. 3 is actually equal return s * 3; only the above shorthand

Since the single parameters are understood, then the Lambda expressions we look at multi-parameter

MultiParameterReturn multiParameterReturn=(s,m)->s*m;				 // 这是有返回值的多参数的lambda表达式 多参数时必须加上小括号
System.err.println(multiParameterReturn.triploid(12,30));	

MultiParameterReturn in unrealized method is
Integer triploid (NUM Integer, Integer num2);
the Lambda expressions are
(s, m) -> s m
really smart people have already seen this is what it means
s representatives NUM
m Code num2
-> directed to a method body
S
m is actually returned multiply two numbers

Then call the methods of other classes since multi-parameter single parameter we have to understand the difficulty of upgrading Lambda expressions

SingleParameter singleParameter2=System.err::println;					// 这是调用其他类的方法的lambda表达式
singleParameter2.printin("这是调用其他类的方法的lambda表达式");    

System.err :: println;
this is actually very simple meaning of the code
before :: is called object
method is invoked after ::
This is equivalent to using System.err.println (); instead our approach

Seemingly difficult to upgrade, in fact, it is as easy to understand
that we come last to create objects using object implementation methods Lambda expressions

PersonCreater p1=()->new Person();													//这是创建对象的lambda表达式
PersonCreater p2=Person::new;															//这是创建对象的lambda表达式  这种写法等价于上面那种写法
System.err.println(p1.getPerson().getDemo()==p2.getPerson().getDemo());   

The first one is simply not a thief
() method on behalf empty
-> execution method body
objects new Person () method returns the implement to create a
second possible a little bit difficult
but the two are to see him create the interface objects, it shows
( ;> new Person () -) ; and Person :: new
internal implementation is the same
Person is to represent the operation object
new object is created on behalf of
all final return is the object created
simple use of the above is Lambda

Lambda expressions usage scenarios

The simplest example
List of forEach
he is to show the most Lambda use
his speed not only reduces the amount of code that also improves code
from List view using
Lambda expressions Not surprisingly, then will be the future development of trend

Released two original articles · won praise 5 · Views 124

Guess you like

Origin blog.csdn.net/qq_41961553/article/details/105368970