Studying Java 8 (lambda expression)

The second stage. It's Lambda now. I haven't used it at work so I don't really get it God site here I tried to study with reference to.

Let's move for the time being

For the time being, I wrote the simplest one.

		/*Example 1. Let's move a simple one with no arguments for the time being*/
		Runnable runner = () -> {
			System.out.println("First lambda expression. I don't know what's great at the moment.");
		};

		Thread t = new Thread(runner);
		t.start();

When I do this, I'm not sure, but the message I wrote in the standard output appears on the console. It seems to be written as argument-> processing </ b>. In this example, there is no argument, so it does not come out, but when there is an argument,

(s) Type abbreviation (String s) Write both type and variable name s Write only the variable name

You can do various things. It seems that you can omit up to () only when there is only one argument.

If it is only example ①, it will not come out, so I will try a little more.

		/*Example 2. With arguments*/
		List<String> l = new ArrayList<>();
		l.add("hello");
		l.add("world");

		//I tried to make it look like lambda
		l.forEach(s -> {
			System.out.println("loop:" + s);
		});

Somehow it looks like lambda (mystery). When you run

loop:hello
loop:world

Will come out. Suddenly, a lambda expression appears in the argument of the forEach method, and while writing it at first? ?? ?? ?? However, the argument of the forEach method is a standard @FunctionalInterface called Consumer. It would be nice to think that it describes the implementation of this Consumer. (Functional Interface = functional interface will be described later) So, you can prepare the Consumer implementation class properly and do it like this.

/**
 *A class that implements ConsumerIF.
 * @author komikcomik
 *
 */
public class ConsumerImpl implements Consumer<Object> {

	@Override
	public void accept(Object t) {
		System.out.println("I implemented Consumer:" + t);
	}

}

Call this like this.

		/*Example 3. When using your own Functional Interface that implements Consumer. It's no longer lambda*/
		l.forEach(new ConsumerImpl());

When executed, the following will appear.

I implemented Consumer:hello
I implemented Consumer:world

You can enjoy the feeling of using the lambda expression in Example 2 (though I think it's a good idea to choose based on the feeling of using it). If it is only Example 1, it seems that it is only a version that changed the writing style of the anonymous class, I didn't really understand the image to use at the development site. A java standard method that takes a functional interface as an argument, as in Examples 2 and 3. When it comes to writing an implementation of the argument IF, it seems that there is a good use for it. However, even if I understand the theory, it will take some time to get used to it.

Functional interface

This is the one that came out on the way. For detailed conditions, please refer to This God Site, but if you explain appropriately An interface in which only one abstract method is defined. This IF seems to be the place to assign lambda expressions and method references (though I haven't covered them in my post yet). So, if you want to understand Java 8 elements such as lambda, understanding functional IF is indispensable.

As I wrote, the conditions for becoming a functional IF are simple, so you can make your own, but in Java 8 ・ Supplier ・ ・ ・ Returns a value with no arguments -Consumer: There is an argument and no value is returned (something is done). The one that came out in the previous example. ・ Predicate ・ ・ ・ Returns true / false with arguments -Function: Returns an arbitrary type with arguments (some are for primitive types) It seems that it provides various standard function IFs like It may be better for newcomers to the development project to choose the one that is already provided as standard rather than making it by themselves, so it may not be necessary to say "What is this original functional IF".

Recommended Posts

Studying Java 8 (lambda expression)
[Java] Lambda expression
Java lambda expression
Java lambda expression variations
Java 8 lambda expression Feature
Java lambda expression [memo]
Review java8 ~ Lambda expression ~
Java lambda expression again
Studying Java ―― 3
Studying Java ―― 9
[Java] Functional interface / lambda expression
Studying Java ―― 4
Studying Java -5
Studying Java # 0
Java8 stream, lambda expression summary
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Java basic learning content 9 (lambda expression)
What is a lambda expression (Java)
Studying Java 8 (Optional)
Studying java9 (jShell)
Hello Java Lambda
Studying Java 8 (Stream)
Now let's recap the Java lambda expression
Studying Java 8 (Collector / Collectors)
Quarkus saves Java Lambda! ??
Understand Java 8 lambda expressions
Studying Java 8 (see method)
How to use Java API with lambda expression
About Java lambda expressions
Studying Java ~ Part 8 ~ Cast
Java8 to start now ~ forEach and lambda expression ~
Introduction to lambda expression
Explain Java 8 lambda expressions
Java table expression injection
java regular expression summary
Java Lambda Command Pattern
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
[Java] Introduction to lambda expressions
What is a lambda expression?
Assign a Java8 lambda expression to a variable and reuse it
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
Studying Java # 6 (How to write blocks)
[Introduction to Java] About lambda expressions
About Lambda, Stream, LocalDate of Java8
Is Java on AWS Lambda slow?
Hello World on AWS Lambda + Java
Try an If expression in Java
Java
Studying Java 8 (StaticIF and Default methods)
Getting started with Java lambda expressions
Studying Java 8 (String Joiner and join)
Java
Studying java9 (dropWhile, takeWhile and Stream.ofNullable)
The origin of Java lambda expressions