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
How to use java Optional
How to use java class
[Java] How to use removeAll ()
[Java] Introduction to lambda expressions
How to use Java Map
How to use Java variables
How to use Java framework with AWS Lambda! ??
How to use Java API with lambda expression
How to use Java HttpClient (Get)
[Introduction to Java] About lambda expressions
[Java] How to use join method
[Processing × Java] How to use variables
[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
I tried to summarize Java lambda expressions
How to use submit method (Java Silver)
[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
[Java] [Maven3] Summary of how to use Maven3
[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 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 TreeSet
[How to use label]
How to use identity
How to use hashes
About Java lambda expressions
Explain Java 8 lambda expressions
How to use Dozer.mapper
How to use Gradle
How to use org.immutables
How to use java.util.stream.Collector