Assign a Java8 lambda expression to a variable and reuse it

Lambda expressions, like objects, can be assigned to variables and reused. Let's use the filter () method as an example to assign a lambda expression to a variable and reuse it. From the list below, I would like to filter out only strings of 8 or more characters.

final List<String> months = 
Arrays.asList("January", "February", "March", "April", "May", "June", "July", "Augast", "September", "October", "November", "December");

final List<String> jewelries = 
Arrays.asList("garnet", "amethyst", "aquamarine", "diamond", "emerald", "pearl", "ruby", "sardonyx", "sapphire", "opal", "topaz", "turquoise");

Assign a lambda expression to the longName variable that filters only strings that are 8 or more characters.

final Predicate<String> longName = name -> name.length() >= 8;

The lambda expression assigned to the same variable (longName) is applied to the filter () expressions in the two separate lists as shown below.

//Filter and output
final List<String> longNameMonths = months.stream().filter(longName).collect(Collectors.toList());
final List<String> longNameJewelries = jewelries.stream().filter(longName).collect(Collectors.toList());

longNameMonths.forEach(System.out::println);
longNameJewelries.forEach(System.out::println);

As a result, only a character string of 8 characters or more was output as shown below.

February
September
November
December
amethyst
aquamarine
sardonyx
sapphire
turquoise

Recommended Posts

Assign a Java8 lambda expression to a variable and reuse it
Java8 to start now ~ forEach and lambda expression ~
How to convert a value of a different type and assign it to another variable
Assign evaluation result to Java variable
What is a lambda expression (Java)
[Introduction to Java] Variable declarations and types
[Java] Lambda expression
Java lambda expression
I wrote a Lambda function in Java and deployed it with SAM
I want to ForEach an array with a Lambda expression in Java
Transform from a normal class to a lambda expression
How to use Java API with lambda expression
How to ZIP a JAVA CSV file and manage it in a Byte array
java neutral lambda expression 1
Java lambda expression variations
Java 8 lambda expression Feature
java lambda expression memo
Java lambda expression [memo]
Introduction to lambda expression
Studying Java 8 (lambda expression)
Review java8 ~ Lambda expression ~
Java lambda expression again
Write a class in Kotlin and call it in Java
How to convert A to a and a to A using AND and OR in Java
Pass the condition to be used in the Java8 lambda expression filter () as a parameter
[Ruby] When you want to assign the result obtained by conditional branching to a variable and put it in the argument
Memorandum No.4 "Get a character string and decorate it" [Java]
[Java] Functional interface / lambda expression
I want to make a list with kotlin and java!
I want to make a function with kotlin and java!
Java8 stream, lambda expression summary
Pass a variable to Scope.
[Java] Introduction to lambda expressions
[Java] Make it a constant
What is a lambda expression?
Java Language Feature and How it Produced a Subtle Bug
A Java user over a dozen years ago tried to study the functions of Java8 (Lambda expression).
How to develop and register a Sota app in Java
Convert Excel to Blob with java, save it, read it from DB and output it as a file!
[Introduction to Java] Variables and types (variable declaration, initialization, data type)
How to read a file and treat it as standard input
Replace with a value according to the match with a Java regular expression
Introduction to Scala from a Java perspective, decompiled and understood (basic)
Java variable declaration, initialization, and types
[Introduction to Java] About lambda expressions
How to make a Java container
Java basic learning content 9 (lambda expression)
[Java] How to create a folder
How to write Java variable declaration
How to make a Java array
How to use Java lambda expressions
Inexperienced create a weather app using OpenWeatherMap and deploy it to Netlify
How to test a private method in Java and partially mock that method
The story of forgetting to close a file in Java and failing
Introduction to Scala from a Java perspective (Class-Tuple edition) to decompile and understand
A shell script that builds a Docker image and pushes it to ECR
How to record JFR (Java Flight Recorder) and output a dump file
Create a program to post to Slack with GO and make it a container
Comparison of thread implementation methods in Java and lambda expression description method
<java> Split the address before and after the street address with a regular expression
How to make an app with a plugin mechanism [C # and Java]