Writing in the front

Jdk8 is a new feature in jdK8. In fact, WHEN I was in school, I didn’t use Lambda expressions and Stream programming. But when I was in the internship, I found that the company’s code was written in this way. I couldn’t help it.

Lambda expressions

First of all, we need to understand what Lambda expressions do. Lambda expressions are mainly used for operations on collections or on functional interfaces, which have only one method, usually denoted by @functionInterface. Let’s start with grammar

The syntax of a Lambda expression is simple: () -> {}, and then there is an operation that can be omitted. That is, if there is only one argument in (), then you can omit (). Similarly, if {} has only one sentence, you can omit {}.

Iterate over the List collection

List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
list.forEach(s -> {
    System.out.println(s);
});
Copy the code

Traversal the Map collection

Map<String, String> map = new HashMap<>();
map.put("a", "aaa");
map.put("b", "bbb");
map.put("c", "ccc");
map.forEach((k, v) -> {
    System.out.println(k + "===" + v);
});
Copy the code

Defines a functional interface with no return value and no arguments for the operation of a functional interface

@FunctionalInterface
public interface GetHelloWorld {
    void getHelloWorld();
}
Copy the code

implementation

GetHelloWorld getHelloWorld = () -> {
    System.out.println("hello world");
};
getHelloWorld.getHelloWorld();
Copy the code

Defines a parameter that returns a value

@FunctionalInterface
public interface GetHelloWorld {
    int getHelloWorld(int a, int b);
}
Copy the code

implementation

GetHelloWorld getHelloWorld = (a, b) -> {
    System.out.println("hello world");
    return a + b;
};
System.out.println(getHelloWorld.getHelloWorld(1, 2));
Copy the code

Take another example of creating a thread

Runnable runnable = () -> { System.out.println("hello world"); }; new Thread(runnable).start(); New Thread(() -> {system.out.println ("hello world"); }).start();Copy the code

So much for lambda expressions, let’s talk about Stream programming.

Stream programming

Streaming programming is divided into three phases: creating a stream(collections can be created using the.stream() method), performing intermediate operations, and performing final operations. Streaming programming is typically operations on collections.

Example 1 let’s say we de-weigh the set elements

List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("bbb");
list.add("ccc");
Stream<String> stream = list.stream();
stream.distinct().forEach(s -> System.out.println(s));
Copy the code

Case two filters the collection

List<Integer> list = new ArrayList<>();
list.add(20);
list.add(18);
list.add(25);
list.add(16);
Stream<Integer> stream = list.stream();
stream.filter(x -> x >= 20).forEach(s -> System.out.println(s));
Copy the code

In case three, sort sets from smallest to largest by default, and here we show from largest to smallest

List<Integer> list = new ArrayList<>();
list.add(20);
list.add(18);
list.add(25);
list.add(16);
Stream<Integer> stream = list.stream();
stream.sorted((x, y) -> y - x).forEach(s -> System.out.println(s));
Copy the code

Case 4: Mapping output to collection

List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("bbb");
list.add("zzz");
list.add("ddd");
Stream<String> stream = list.stream();
stream.map(s -> s.length()).forEach(s -> System.out.println(s));
Copy the code

Case 5 Number statistics of pairs of sets after deduplication

List<Integer> list = new ArrayList<>();
list.add(22);
list.add(13);
list.add(15);
list.add(15);
Stream<Integer> stream = list.stream();
long count = stream.distinct().parallel().count();
System.out.println(count);
Copy the code

There are a lot of operations we can call directly, which reduces the amount of code and improves the development efficiency. Of course, there are more operations in streaming programming, and others can be studied if you are interested.