Lambda function first appears in Java8. It can be easily used to traverse with forEach function. Moreover, the code will be concise if we use it instead of Runnable function.
1. Iteration:
Here are three ways to print each element. First method is for loop, second and third is Lambda function. Last two functions look more concise.
Solution 1:1
2
3
4
5
6
7List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
for (int element : numbers) {
System.out.prinln(element);
}
Solution 2:1
2
3List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(x -> System.out.println(x));
Solution 3:1
2
3List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach(System.out::println);
2. Event listener:
Using Lambda function has less code line but it has more curly braces.
Solution 1:1
2
3
4
5
6
7
8
9
10
11button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
//handle the event
}
});
Solution 2:1
2
3
4
5button.addActionListener(e -> {
//handle the event
});
3. Predicate Interface:
Predicate interface in java.util.function can be used to filter. If you need to process multiple objects and execute the same process logic, these logics can be encapsulated in function filter. Here also has three methods to make a comparison.
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<String> words = Arrays.asList("a", "ab", "abc");
numbers.forEach( x -> {
if (x % 2 == 0) {
//process logic
}
})
words.forEach( x -> {
if (x.length() > 1) {
//process logic
}
})
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13
14public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<String> words = Arrays.asList("a", "ab", "abc");
filter(numbers, x -> (int)x % 2 == 0);
filter(words, x -> ((String)x).length() > 1);
}
public static void filter(List list, Predicate condition) {
list.forEach(x -> {
if (condition.test(x)) {
//process logic
}
})
}
Solution 3:1
2
3
4
5public static void filter(List list, Predicate condition) {
list.stream().filter(x -> condition.test(x)).forEach(x -> {
//process logic
})
}
4. Map:
Use function map convert data to another list, then reverse it to list type using Collect.1
2
3List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> mapped = numbers.stream().map(x -> x * 2).collect(Collectors.toList());
mapped.forEach(System.out::println);
5. Reduce:
Reduce action means obtaining an output based on two variables. For example, two variables execute add operation then return a sum.1
2
3
4
5List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce((x, y) -> x + y).get();
System.out.println(sum);
6. Replace Runnable:
Makes code more compact.
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Runnable r = new Runnable() {
@Override
public void run() {
//to do something
}
};
Thread t = new Thread(r);
t.start();
Solution 2:1
2
3
4
5
6
7
8
9Runnable r = () -> {
//to do something
};
Thread t = new Thread(r);
t.start();
Solution 3:1
2
3
4
5
6
7Thread t = new Thread(() -> {
//to do something
});
t.start;