Getting Started with Legacy Java Engineers (Stream + Lambda Expression)

Now that I'm using Java 8 in the field, I'm using streams and lambda expressions. I will write what I can write without checking (will be updated at any time)

Change log

Thank you for your advice. 2019/07/19: Changed title (added stream) 2019/07/19: Changed the description of IntStream to ** stream ** with int type element

reference

If there is a reference site, I will add it. Qiita --Introduction to Java Stream API

Lambda Anonymous Introduction

Use the function interface. Generate a stream. It can be used from Java 8. Process in the order of generation operation → intermediate operation → termination operation.

Generate operation

If you can understand it, increase it.

stream() Create a Stream. The sample creates a Stream from the list.

        List<String> list = Arrays.asList("hoge","fuga","poyo");
        list.stream()

IntStream.range(startInclusive, endExclusive) Create a stream with an int type element. ʻEnd Exclusive` is not included. Generate 0-9 IntStream.

IntStream.range(0, 10)

IntStream.rangeClosed(startInclusive, endInclusive) Create a stream with an int type element. Includes this ʻend Inclusive`. Generate 1 to 10 IntStream.

IntStream.rangeClosed(1,10)

Intermediate operation

If you can understand it, increase it.

filter Create a new stream of only the elements that match the conditions. Generate even-numbered streams.

IntStream.range(0,10).filter(i -> i % 2 == 0)

mapToObj Convert to Object. The sample converts ~ Integer ~ int to String.

IntStream.range(0,10).mapToObj(i -> String.valueOf(i))

Termination operation

forEach Loop processing. No return value.

IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).forEach(System.out::println);

collect You can create lists and maps. The sample creates a List.

List<String> list = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).collect(Collectors.toList());

toArray Create an array.

String[] array = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).toArray(String[]::new);

count Returns the number of elements.

long count = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).count();

in conclusion

I will update it from time to time.

Recommended Posts

Getting Started with Legacy Java Engineers (Stream + Lambda Expression)
Getting Started with Ruby for Java Engineers
Java8 stream, lambda expression summary
Getting Started with Java Basics
Getting Started with Java Starting from 0 Part 1
[Java] Lambda expression
Java lambda expression
Links & memos for getting started with Java (for myself)
How to use Java API with lambda expression
Java8 Lambda expression & Stream design pattern reconsideration --Command pattern -
java neutral lambda expression 1
Getting Started with DBUnit
Java lambda expression variations
Java 8 lambda expression Feature
java lambda expression memo
Getting Started with Swift
Java lambda expression [memo]
Studying Java 8 (lambda expression)
Getting Started with Docker
Review java8 ~ Lambda expression ~
Java lambda expression again
Getting Started with Doma-Transactions
I've reviewed Java's lambda expression, stream API, six months after I started Java.
Getting started with Java programs using Visual Studio Code
Java8 Lambda Expression & Stream Design Pattern Rethinking --Null Object Pattern -
Java8 Lambda expression & Stream design pattern reconsideration --Template Method pattern -
Use Lambda Layers with Java
Getting Started with Doma-Annotation Processing
[Java] Functional interface / lambda expression
Memoization recursion with lambda expression
Getting Started with JSP & Servlet
Getting Started with Spring Boot
Getting Started with Ruby Modules
Getting started with Java and creating an AsciiDoc editor with JavaFX
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Rethinking design patterns with Java8 lambda expressions & Stream --Builder pattern -
Java8 Lambda Expression & Stream Design Pattern Rethinking --Chain of Responsibility Pattern -
[Java 8] Duplicate deletion (& duplicate check) with Stream
Java lambda expressions learned with Comparator
Returning to the beginning, getting started with Java ② Control statements, loop statements
Getting Started with Java_Chapter 5_Practice Exercises 5_4
About Lambda, Stream, LocalDate of Java8
Java basic learning content 9 (lambda expression)
[Google Cloud] Getting Started with Docker
Getting Started with Docker with VS Code
Java8 list conversion with Stream map
Getting Started with Micronaut 2.x ~ Native Build and Deploy to AWS Lambda ~
I want to ForEach an array with a Lambda expression in Java
Getting Started with Machine Learning with Spark "Price Estimate" # 1 Loading Datasets with Apache Spark (Java)
Getting Started with Doma-Criteria API Cheat Sheet
Now let's recap the Java lambda expression
Getting Started with Docker for Mac (Installation)
Getting Started with Parameterization Testing in JUnit
Getting Started with Ratpack (4)-Routing & Static Content
Getting started with the JVM's GC mechanism
Getting Started with Language Server Protocol with LSP4J
Getting Started with Creating Resource Bundles with ListResoueceBundle
Getting Started with Java_Chapter 8_About Instances and Classes
Interact with LINE Message API using Lambda (Java)
How to use Java framework with AWS Lambda! ??
Getting Started with Doma-Using Subqueries with the Criteria API