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.
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.
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