Good programmers share java8 Lambda expressions What's new

Good programmers share java8 Lambda expressions What's new

⼀, Lambda Expressions Introduction

What is Lambda?

Lambda expressions are Java 8 launch causes a new feature. Essentially, Lambda expressions are ⼀ anonymous function.

Why To use the Lambda?

Using Lambda expressions can ⼀ two-connector into ⾏ comes in handy simple to achieve.

Before we give ⼀ a Connector Using the assignment of lead time, you can use the Connector implementation class or anonymous inner classes. But with

Lambda expressions, we can be more ⽅ will achieve this demand.

interface Comparator<T> {

int compare(T o1, T o2);

}

class Program {

public static void main(String[] args) {

1. Use // implementation class Connector to achieve

class Impl implements Comparator<Integer> {

@Override

public int compare(Integer o1, Integer o2) {

return o1 - o2;

}

}

Comparator<Integer> c1 = new Impl();

// 2. Using an anonymous inner class

Comparator<Integer> c2 = new Comparator<Integer>() {

@Override

public int compare(Integer o1, Integer o2) {

return o1 - o2;

}

};

// 3. Using Lambda expressions realization

Comparator<Integer> c3 = (o1, o2) -> o1 - o2;

}

}

From the submenus in the above example, we can see: for the same lead-connector Use a realization ⼀, Lambda simple!

Lambda dock-connector requirements? Although Lambda expressions can be very easy to achieve Connector, but not all of the Connector Using Lambda expressions can be achieved.

Using Lambda expressions can be simple to achieve Connector is required. Because the nature of Lambda expression is concerned, it is ⼀ anonymous

Function Use this function to implement anonymous Connector of Remedies. So, if there are multiple-connector must implement the abstract Remedies,

Lambda expressions will become inoperable is to apply it.

Note: Lambda Expressions Connector requires only ⼀ abstract Remedies must be achieved. But JAVA 8 dock-connector also has a ⼀

expand. Now, we can-connector, the default is Using a modified ⼀ Remedies, at this time, there is a real need for this Remedies

Now. Then, implementation class in the realization Connector, for the default Remedies can be achieved, may not be realized. and so,

Individual cases from said Connector only ⼀ abstract Remedies must be implemented, with the default Remedies ⽆ off.

@FunctionalInterface**

@FunctionalInterface

Lambda expressions required because Connector has one and only ⼀ abstract Remedies must realize, therefore, can use the dock-connector

Connector @FunctionalInterface to feed ⾏ defined. This annotation constrain Connector can only have ⼀ must implement abstract ⽅

law. Using this annotation modified Connector separate warranty is called functional-connector.

⼆, Lambda expressions basic grammar

Lambda expressions, essentially ⼀ anonymous Remedies, so get accustomed component of the necessary methods for the camera can not do without: the return value, Remedies

Name, parameters, Remedies body. But because he is ⼀ anonymous Remedies, so ⽅ Farmington negligible. Meanwhile, Lambda also not required

Explicitly declared return type. So, we write Lambda expressions, you can just shut Center Weighted parameters and Remedies body can be.

Parameters: In () surrounded by a plurality of parameters separated by commas

(int a, int b)

Remedies body: surrounded by {}

{ System.out.println("hello world"); }

->: Lambda operator, and the parameters to separate Use body Remedies

(int a, int b) -> {};

With this permit low a part of, we can use the Lambda into ⾏ achieve any functional-connector

// ⽆ participate, no matter if the return value

() -> { System.out.println("hello world"); };

// int, int argument, no matter if the return value

(int a, int b) -> { System.out.println(a + b); };

// int, int parameter, int return value

(int a, int b) -> { return a + b; };

Three, Lambda expression syntax streamline

Remedies Connector defined, has been declared parameter type methods for the camera, the number and type of return value. So, Using Lambda expressions

When implemented, the corresponding portion may be omitted parameter streamlined

  1. Type parameter can be streamlined

(int a, int b) -> { System.out.println(a + b); }

Could be trimmed to:

(a, b) -> { System.out.println(a + b); }

  1. If only ⼀ a number of parameters, ⼩ brackets can be streamlined

(int a) -> { System.out.println(a); }

Could be trimmed to:

a -> { System.out.println(a); }

Remedies streamlined body

  1. If the body only ⼀ Remedies statement, the bracket may be omitted ⼩

a -> { System.out.println(a); }

Could be trimmed to:

a -> System.out.println(a);

  1. If ⼀ statement Remedies ⼀ body is the only return values ​​in parentheses compacted zoomed out, return must be omitted

a -> { return a * 2; }

Could be trimmed to:

a -> a * 2;

Fourth, the grammar of Adv Lambda expressions Remedies Using lead

What is Remedies Using lead

If the logic has been implemented to be achieved in a number Remedies in ⼀ Use Lambda intake ⾏ Connector when implemented, can directly cause

Using primers with mode method, point designated Remedies.

interface Calculate {

int calculate(int a, int b);

}

public class Program {

public static void main(String[] args) {

// Use lead-connector

Calculate c = (a, b) -> a + b;

}

public static int add(int a, int b) {

return a + b;

}

} In the above code, the main function implemented Calculate lead portion c Use of a lower ⾯ Remedies have ⼀ ⾏ add into the implementation.

So this time, we do not need to achieve ⼀ times, just pointing directly already written can be achieved. Therefore, the following changes can perform while

Made:

Calculate c = Program::add;

On the screen for a Program :: add that ⼀ Remedies Using lead. Using a lead ⼀ static Remedies add Program class.

In how to use it when the need to pay attention to the lead Use

  1. Using primers Remedies number of parameters, parameter types, the return value must define the type of functional ⼀ Remedies Connector the actuator.

  2. Remedies must lead cited Use Using the body, that is, those who belong to ⾪ methods for the camera. For example: ⽅ on add Remedies is ⼀ static Remedies need to Use

Using the class to transfer. So Remedies Using primers that category :: Remedies, if it is ⼀ member Remedies, you will need to Use the Object :: Remedies

Use the form to lead.

Use of lead configuration Remedies

If desired a configuration uses one primer Remedies need Use :: new class Using primers into ⾏

interface CreatePerson {

Person getPerson();

}

class Person {}

class Program {

public static void main(String[] args) {

CreatePerson c = Person::new;

}

}

Fifth, the integrated case Lambda expressions: Sort Comparator

// Sort

list.sort((o1, o2) -> o2.age - o1.age);

// Using Lambda expressions to achieve Comparator Connector, and a TreeSet object is instantiated ⼀

TreeSet<Person> set = new TreeSet<>((o1, o2) -> {

if (o1.age >= o2.age) {

return -1;

}

else {

return 1;

}

});

Sixth, integrated case of Lambda expressions: forEach () // will be set for each element are ⼀ with START to accept the Remedies.

list.forEach(System.out::println);

// output set all even

list.forEach(ele -> {

if (it% 2 == 0) {

System.out.println (black);

}

});

Seven, Lambda expressions of integrated case: removeIf ()

// The collection of each element are ⼀ with START Remedies to test, if the return value is true, then delete this element

// delete the collection at the age of 10 years old zoomed elements

list.removeIf(ele -> ele.age > 10);

Integrated case be eight, Lambda expressions of: instantiating thread

new Thread(() -> {

for (int i = 0; i < 100; i++) {

System.out.println(i);

}

}).start();

Nine, functional system built-connector

The system has given us a lot of functional-connector, then our ⽅ Use. Therefore, if we need to Use the following methods for the camera

Waiting, you do not need to design Connector, directly Using the Connector can be specified. Functional Connector

Ginseng

number

Return value Special Instructions: submenus to get accustomed to achieve special-connector

Predicate T boolean

IntPredicate: Parameters: int, return value: boolean

LongPredicate: Parameters: long, return value: boolean

DoublePredicate: Parameters: double, return value: boolean

Consumer T void

IntConsumer: Parameters: int, Return Value: void LongConsumer:

Parameters: int, Return Value: void DoubleConsumer: Parameters: int, back

Return value: void

Function<T, R> T R

IntFunction <R>: Parameters: int, return values: R

IntToDoubleFunction: Parameters: int, return value: double

IntToLongFunction: Parameters: int, return value: long

LongFunction <R>: Parameters: long, return values: R

LongToDoubleFunction: Parameters: long, return value: double

LongToIntFunction: Parameters: long, return value: int

DoubleFunction <R>: Parameters: double, return values: R

DoubleToIntFunction: Parameters: double, Return Value: int

DoubleToLongFunction: Parameters: double, Return Value: long

Supplier ⽆ T

BooleanSupplier: Parameters: ⽆, return value: boolean

IntSupplier: Parameters: ⽆, Return Value: int LongSupplier: reference

Number: ⽆, Return Value: long DoubleSupplier: Parameters: ⽆, return value:

double

UnaryOperator T T

IntUnaryOperator: Parameters: int, return value: int

LongUnaryOperator: Parameters: long, return value: long

DoubleUnaryOperator: Parameters: double, return value: double

BinaryOperator T,

T T

IntBinaryOperator: Parameters: int, int, return value: int

LongBinaryOperator: Parameters: long, long, return value: long

DoubleBinaryOperator: Parameters: double, double, return value:

double

BiPredicate<L,

R>

L,

R boolean

BiConsumer<T,

U>

T,

U void

BiFunction<T,

U, R>

T,

U R

Connector above, the most commonly Using the Predicate, Consumer,

Guess you like

Origin blog.51cto.com/14479068/2433704