How to use Java lambda expressions

Overview

Lambda expressions were introduced from Java 8. This page is a memo page about how to use Java lambda expressions. Lambda expressions are easy to use when used in combination with the Stream API.

Extract values from the list under certain conditions

Lambda expression, no use of Stream API

List<Integer> list = Arrays.asList(2, 9, 5, 7, 4);

for (Integer i : list) {
    if (i >= 5) {
        System.out.println(i);
    }
}

Lambda expression, using Stream API

List<Integer> list = Arrays.asList(2, 9, 5, 7, 4);

list.stream()
        .filter(i -> i >= 5)
        .forEach(System.out::println);

The lambda expression and Stream API can be used for clearer and more readable description. Below are examples of using lambda expressions and Stream API.

Sort by dictionary

List<String> list = Arrays.asList("b", "c", "a", "c", "b");

list.stream()
        .sorted()
        .forEach(System.out::println);

Inverse dictionary sort

List<String> list = Arrays.asList("b", "c", "a", "c", "b");

list.stream()
        .sorted((s1, s2) -> s2.compareTo(s1))
        .forEach(System.out::println);

Duplicate deletion

List<String> list = Arrays.asList("b", "c", "a", "c", "b");

list.stream()
        .distinct()
        .forEach(System.out::println);

Count the number of values 5 and above

List<Integer> list = Arrays.asList(2, 9, 5, 7, 4);

long count = list.stream()
        .filter(i -> i >= 5)
        .count();

System.out.println(count);

Add up values of 5 and above

List<Integer> list = Arrays.asList(2, 9, 5, 7, 4);

int sum = list.stream()
        .filter(i -> i >= 5)
        .mapToInt(i -> i)
        .sum();

System.out.println(sum);

Output values from 1 to 10 in multiple threads

IntStream.range(0, 10)
        .parallel()
        .forEach(System.out::println);

Recommended Posts

How to use Java lambda expressions
[Java] How to use Map
[Java] How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] Introduction to lambda expressions
[Java] How to use string.format
How to use Java variables
[Java] How to use Optional ①
[Java] Summary of how to abbreviate lambda expressions
How to use Java HttpClient (Get)
[Introduction to Java] About lambda expressions
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
Notes on how to use regular expressions in Java
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
[Java] How to use the hasNext function
I tried to summarize Java lambda expressions
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
[Java] How to use the Calendar class
[Java] Learn how to use Optional correctly
[Easy-to-understand explanation! ] How to use Java overload
try-catch-finally exception handling How to use java
[Easy-to-understand explanation! ] How to use Java encapsulation
How to use Map
How to use rbenv
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use map
How to use collection_select
Understand Java 8 lambda expressions
How to use Twitter4J
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
How to use identity
How to use hashes