[JAVA] A little understanding of lambda expressions

I struggled with the lambda expression while studying Oracle Silver, so I will write down what I understood.

What can a lambda expression do?

You can treat the method like a variable. You can pass a method as a method argument. You can write the same thing as an anonymous class with a simpler description.

Lambda-style convention

You need to override the abstract method of the functional interface.

Basic writing

(Type argument name)-> {Process 1;};

(Object o) -> {System.out.println(o);};

Normal → Anonymous class → Write in the order of lambda expression

Lambda expressions are ** actually just overriding abstract methods **. To understand it ・ Override normally -Override with anonymous class -Override with lambda expression I will write with three.

This time, we will override and implement the abstract method accept of the functional interface Consumer.

usually

Consumer interface


@FunctionalInterface
public interface Consumer<T> {
   void accept(T t);
//...
}

The first is to implement the Consumer interface normally with "implements" and override the accept method.

Ordinary implementation


import java.util.function.Consumer;

public class Lambda2 implements Consumer<String> {

	@Override
	public void accept(String s) {
		System.out.println(s);
	}

	public static void main(String[] args) {
		Lambda2 lam2 = new Lambda2();
		lam2.accept("usually");
	}
}

Anonymous class

Next is the anonymous class. The point is @Override.

Implemented in anonymous class


import java.util.function.Consumer;

public class lambda3 {

	public static void main(String[] args) {
		Consumer<String> func = new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s);
			}
		};
		func.accept("Anonymous class");
	}
}

Lambda expression

Finally, how to implement a Consumer interface with a lambda expression. Overriding the accept method with a lambda expression.

Implemented by lambda expression


import java.util.function.Consumer;

public class Lambda1 {

	public static void main(String[] args) {

		Consumer<String> func = (s) -> {System.out.println(s);};
		func.accept("Lambda expression");

		//Abbreviation notation. Same process as above.
		Consumer<String> func2 = s -> System.out.println(s);
		func.accept("Lambda abbreviation");
			}
}

All you're doing is ** overriding the accept method ** for all normal, anonymous classes, and lambda expressions.

  1. Override the accept method Consumer <String> func = (s)-> {System.out.println (s);};
  2. Call accept method func.accept ("lambda expression");

Try the same with the Stream API

Using a lambda expression with the Stream API seems to be the strongest, so I'll try it.

As before, the order is as follows. ・ Implemented normally -Implemented by anonymous class ・ Implemented by lambda expression

Consumer interface


@FunctionalInterface
public interface Consumer<T> {
   void accept(T t);
//...
}

usually


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Normal implements Consumer<String>{
	@Override
	public void accept(String s) {
		System.out.println(s);
	}

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");

		//Extract the contents of list with forEach.
		Normal n = new Normal();
		list.forEach(n);//Consumer as an argument for forEach<String>Pass the mold
	}
}

Anonymous


import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;

public class Tokumei {

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");

		list.forEach(new Consumer<String>() {
			@Override
			public void accept(String s) {
				System.out.println(s);
			}
		});
	}
}

Lambda expression


import java.util.ArrayList;
import java.util.List;

public class Lambda4 {

	public static void main(String[] args) {
		List<String> list = new ArrayList<>();
		list.add("1");
		list.add("2");
		list.add("3");

		list.forEach(s -> System.out.println(s));
	}
}

A lambda expression requires a very short description! It's pretty easy because you don't have to write implements or @Override.

How forEach works

forEach takes a Consumer type as an argument.

forEach(Consumer<T> t)

The accept method is included in this argument and is passed to forEach. And it is an image that the accept method is repeatedly executed in forEach. This is a convenient point for lambda expressions that pass a method as a method argument.

Typical functional interface

You can write the functional interface yourself, but the ones you use most are already available.

Consumer It just receives an argument and has no return value. Argument type: T

@FunctionalInterface
public interface Consumer<T> {
   void accept(T t);
//...
}

Function Takes the argument T and returns R. Argument type: T Return type: R

@FunctionalInterface
public interface Function<T, R> {
   R apply(T t);
//...
}

Predicate Takes the argument T and returns a boolean. Argument type: T Return type: boolean

@FunctionalInterface
public interface Predicate<T> {
   boolean test(T t);
//...
}

Supplier Returns T without receiving any arguments. Return type: T

@FunctionalInterface
public interface Supplier<T> {
   T get();
//...
}

Summary

I found that using a lambda expression can significantly reduce the description. It seems that you can get used to it by rewriting it as normal → anonymous class → lambda expression. I will actively use it when using arrays and collections.

Recommended Posts

A little understanding of lambda expressions
The origin of Java lambda expressions
Name a group of regular expressions (Java)
[Java] Summary of how to abbreviate lambda expressions
Use Java lambda expressions outside of the Stream API
Understand Java 8 lambda expressions
[Java] When writing the source ... A memorandum of understanding ①
About Java lambda expressions
Explain Java 8 lambda expressions
Have fun programming with lambda expressions and a fluid interface
Aiming for a basic understanding of the flow of recursive processing
[Java] Beginner's understanding of Servlet-②
Creating a batch of Liferay
[Java] Beginner's understanding of Servlet-①
[Java] Summary of regular expressions
[Java] Introduction to lambda expressions
What is a lambda expression?
Memorandum of understanding about LOD.
Learn regular expressions little by little ①
A brief explanation of commitAllowingStateLoss
[Java beginner] I got a little deeper understanding of "It's time to use new", so make a note