[JAVA] [For beginners] About lambda expressions and Stream API

I will explain the description of lambda expressions introduced from Java 8 for beginners with a memorandum.

What is a lambda expression?

Lambda expressions are a description format introduced from Java8 that allows methods to be treated like variables. Lambda expressions can simplify your writing and make your code easier to read.

Also, using a lambda expression when using a functional interface such as the Stream API makes it very easy to write! !!

How to write a lambda expression

First of all, I will explain the actual description method of the lambda expression.

RunnableLamda.java


public static void main(String[] args) {
  Runnable runner = () -> System.out.println("Hello");
  runner.run(); //Hello
}

The above describes the runnable interface using a lambda expression. The anonymous class is abbreviated with "new Runnabl () {}" and "public void run".

Also note that lambda expressions can only use one interface with abstract methods.

The argument type is automatically estimated by the compiler and does not need to be described. However, even if you write the type as usual, you will not get a compile error.

StreamAPI Like the lambda expression, the function "Stream API" added in Java 8 is a convenient API when processing a set of elements such as List and Collection.

The Stream API is built on the premise of lambda expressions, so it's very compatible.

For example, the process of displaying only even numbers in 1 to 6 can be written as follows.

StreamLamda.java


List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
integerList.stream() 
        .filter(i -> i % 2 == 0) 
        .forEach(i -> System.out.println(i)); 

We will also introduce other Stream APIs. forEach Loop the contents of the List. Very easy to write.

ForEachSample.java


Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6 }).forEach(System.out::println);

filter It is an intermediate operation for filtering and serves as an if statement. Pass a lambda expression that is T-> boolean as an argument. Collect only elements whose expression is true.

FilterSample.java


Arrays.asList(new Integer[] { 1, 2, 3, 4, 5, 6 }).stream().filter(x -> x > 3).forEach(System.out::println);

map It is an intermediate operation that can convert elements, and you can perform four arithmetic operations on elements and even convert types.

MapSample.java


List<Integer> integerList = Arrays.asList(1, 2, 3, 4, 5, 6);
integerList.stream() 
        .map(i -> "The element is" + i + "is") 
        .forEach(i -> System.out.println(i)); 

Finally

The lambda expression and Stream API introduced this time can also be written in the conventional way. However, in the development field, there are many opportunities to read code written by others as well as coding by yourself. Others' programs may contain lambda expressions and Stream APIs, so get used to them!

Recommended Posts

[For beginners] About lambda expressions and Stream API
Nowadays Java lambda expressions and Stream API
Java for beginners, expressions and operators 1
Java for beginners, expressions and operators 2
About Java lambda expressions
About products using crud processing and devise (for beginners)
[For beginners] How to operate Stream API after Java 8
Use Java lambda expressions outside of the Stream API
About for statement and if statement
Java application for beginners: stream
[Introduction to Java] About lambda expressions
About Lambda, Stream, LocalDate of Java8
[Introduction to Java] About Stream API
[For Java beginners] About exception handling
Classes and instances Java for beginners
Articles to learn more about Stream API
[Java] for Each and sorted in Lambda
Until programming beginners can use lambda expressions
Java8 / 9 Beginners: Stream API addiction points and how to deal with them
Kantai Collection Java # 1 Classes and Objects [For Beginners]
C macros for watchOS and Swift API Availability
C macros for tvOS and Swift API Availability
Stream API memo
Chew about API
Java Stream API
Stream API basics
[For beginners] Quickly understand the basics of Java 8 Lambda
[In-house study session] Java basics-Lambda expression and Stream API- (2017/07/13)
Learn for the first time java # 3 expressions and operators
Array variables and associative arrays compiled by beginners for beginners
List of frequently used Java instructions (for beginners and beginners)
About RSpec directory structure and role for each spec