[JAVA] Stream API basics

Introduction

-Shows typical methods of StreamAPI and basic coding examples. -The idea, application examples, and techniques of StreamAPI will be delegated to other entries, and we will try to write them as simply as possible here. -Stream is generally written by giving a lambda expression or method reference, but I dare to declare a functional interface.

What is Stream API?

-API for handling a set of data added in SE8. The main purpose is to support parallel processing. Using the Stream API makes it easy to switch from sequential processing to parallel processing.

-The interface under the java.util.stream package, which is different from the stream used in the java.io package.

-The java.util.stream package is based on the BaseStream interface and consists of a Stream interface for handling reference types and three interfaces for handling primitive types.

-Stream is generated based on a data set such as List or Map, and the result is obtained by executing 0 or more intermediate operations and 1 termination operation.

More details

Java (tm) Platform Standard Edition 8 Package java.util.stream Introduction to Java Stream API-Basic Rough summary of Java8 Stream

Generate

Reference type Stream

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();

Primitive type Stream

IntStream intStream = IntStream.of(1, 2, 3, 4, 5);

From the array

int[] array = { 1, 2, 3, 4, 5 };
IntStream intStream = Arrays.stream(array);

Specify the range

IntStream intStream = IntStream.range(1, 5);

From file

Path path = Paths.get("D:\\hoge.txt");
Stream<String> fileStream = Files.lines(path)

The simplest Stream operation

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
stream.forEach(System.out::println);

Generates an Integer type Stream and executes standard output processing with 0 intermediate operations and 1 termination operation. forEach is a termination operation. The termination operation will be described later. Basically, this is combined with any number of different intermediate operations and one of the same various termination operations.

More details

Try to stream text to Java 8 Stream API (generation)

Intermediate operation

filtering

Stream<String> stream = Stream.of("A", "B", "A", "B", "B", "A");
Predicate<String> isA = "A"::equals;
stream.filter(isA).forEach(System.out::println);

An image of thinning out False with a predicate type isHogeHoge in between. Predicate is a functional interface. The functional interface is as follows. Basic functional interface in 3 minutes

mapping

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
Function<Integer, Integer> toDouble = param -> param * 2;
stream.map(toDouble).forEach(System.out::println);

Returns a Stream with the corresponding value, using each element of the Stream as a key. In short, it's like conversion processing. The sample used Function for clarity, but in this case Unary Operator is smart. The lambda expression is below. Summary of Java8 Lambda extension Until the interface implementation becomes lambda

sort

Stream<Integer> stream = Stream.of(3, 2, 1, 5, 4);
Comparator<Integer> comparator = Comparator.reverseOrder();
stream.sorted(comparator).forEach(System.out::println);

Specify the order by giving a comparator to sorted. The following is about Comparator. (o1, o2)-> o1 --o2 Stop spells! --How to use Comparator in Java 8

Termination operation

matching

Stream<String> stream = Stream.of("George", "John", "Ringo", "Paul");
Predicate<String> isRingo = "Ringo"::equals;
boolean isMatch = stream.anyMatch(isRingo);

There are also variations such as allMatch and noneMatch.

count

Stream<String> countSt = Stream.of("George", "John", "Ringo", "Paul");
long result = countSt.count();

reduction

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
BinaryOperator<Integer> sum = Integer::sum;
Optional<Integer> result = stream.reduce(sum);

Process the result into a single value, such as a convolution. The return value is Optional. The following is for Optional. Java 8 "Optional" -How to deal with null in the future-

Variable reduction

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = list.stream();
List<Integer> resultList = stream.collect(Collectors.toCollection(ArrayList::new));
resultList.forEach(System.out::print);

Process into a set such as ArrayList.

More details

I tried to stream the text to Java 8 Stream API (Termination operation) About reduction operation of Java8 Stream

Parallel processing

Stream<String> stream= Stream.of("A", "B", "C", "D", "E", "F");
stream.parallel().forEach(param -> System.out.println(param + " thread-id: " + Thread.currentThread().getId()));

Parallel processing can be achieved simply by using pararelStream () at the time of generation or by biting parallel () in an intermediate operation.

More details

Now, let's start parallel programming About the proper use of Stream and Parallel Stream in Java 8 Discussion on stream and parallelStream How different is the performance of each Java writing style? I compared the measurements. Stream and loop

I can't write parallel processing, so I won't go into detail.

Recommended Posts

Stream API basics
Stream API memo
Java Stream API
Stream API (Collectors class)
Stream API map method
Java8 Stream API practice
Java Stream API cheat sheet
Java Stream API in 5 minutes
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
[Java] Introduction to Stream API
[Java11] Stream Usage Summary -Basics-
[Java] Stream API intermediate operation
[java8] To understand the Stream API
[Introduction to Java] About Stream API
Create an easy-to-extend Stream API alternative
I tried using Java8 Stream API
Java 8 ~ Stream API ~ to start now
Rails basics
Articles to learn more about Stream API
Rails API
Ruby basics
Ruby basics
Data processing using stream API from Java 8
Fragment basics
Try using the Stream API in Java
Nowadays Java lambda expressions and Stream API
Docker basics
ViewPager basics
Stream play
Java basics
Java basics
RSpec Basics
Stream termination
Anonymous class (aiming to introduce stream api)
I tried to summarize the Stream API
JavaScript basics
Notes on Java's Stream API and SQL
JPA Basics 2
Hash basics
Java basics
Ruby basics
RecyclerView Basics
Memo Stream
[For beginners] About lambda expressions and Stream API
[Java] How to operate List using Stream API