Java Stream API

Introduction

Java Are you using it! I often write something lately, but I feel that it has become quite convenient since the Stream API was implemented. Typical things like Stream.forEach and Stream.filter come out at once, Some things that I don't use often don't come out, so I'll keep them for memorandum.

Stream.generate Creates an unordered, infinite Stream. This is useful when creating irrelevant values, such as generating random numbers.

Document: docs

code

    public static void main(String[] args) {
        Stream<Double> stream = Stream.generate(() -> Math.random());
        stream.limit(5).forEach(System.out::println);
    }

result

0.022804976867977667
0.06422820749702451
0.7240936837411123
0.9070898332933144
0.6724389714182997

Stream.anyMatch With Predicate <T> as a parameter, true is returned if there is at least one value that matches the condition.

Documentation: docs

code

    public static void main(String[] args) {
        List<String> strList = Arrays.asList("dog", "cat", "bird");
        boolean match = strList.stream().anyMatch(e -> e.equals("dog"));
        System.out.println(match);
    }

result

true

Stream.allMatch With Predicate <T> as a parameter, true is returned if all the values match the conditions.

Documentation: docs

code

    public static void main(String[] args) {
        List<String> strList = Arrays.asList("dog", "cat", "bird");
        boolean match = strList.stream().allMatch(e -> e.equals("dog"));
        System.out.println(match);
    }

result

false

Stream.noneMatch With Predicate <T> as a parameter, it returns true if there is no value that matches the condition.

Documentation: docs

code

    public static void main(String[] args) {
        List<String> strList = Arrays.asList("dog", "cat", "bird");
        boolean match = strList.stream().noneMatch(e -> e.equals("fish"));
        System.out.println(match);
    }

result

true

Stream.takeWhile With Predicate <T> as a parameter, the value is returned while the conditions are met. In the example below, numbers less than 0 will be returned.

Document: docs

code

    public static void main(String[] args) {
        List<Integer> numList = Arrays.asList(
                -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
        );
        numList.stream().takeWhile(x -> x < 0).forEach(System.out::println);
    }

result

-5
-4
-3
-2
-1

Stream.dropWhile With Predicate <T> as a parameter, the value is returned while the conditions are not met. In the example below, numbers greater than or equal to 0 will be returned.

Document: docs

code

    public static void main(String[] args) {
        List<Integer> numList = Arrays.asList(
                -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5
        );
        numList.stream().dropWhile(x -> x < 0).forEach(System.out::println);
    }

result

0
1
2
3
4
5

Finally

StreamAPI It's very convenient, isn't it? I would like to actively use it and aim for a code that can be understood intuitively. It ’s short, but that ’s it. Thank you very much!

Recommended Posts

Java Stream API
[Java] Stream API / map
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
[Java] Stream API intermediate operation
Stream API memo
[java8] To understand the Stream API
[JAVA] Stream type
[Introduction to Java] About Stream API
I tried using Java8 Stream API
Studying Java 8 (Stream)
Java 8 ~ Stream API ~ to start now
Java Stream termination
[Java] Stream processing
Stream API basics
Java 9 Optional :: stream
Try using the Stream API in Java
Nowadays Java lambda expressions and Stream API
Try various Java Stream API methods (now)
[Java] Stream Collectors notes
Pack API response (java)
[Java] Stream API-Stream generation
Stream API (Collectors class)
Stream API map method
Docker-Client Java API Troubleshooting
Zabbix API in Java
Java8 Stream reduction operation
Java8 Stream Rough Summary
[Java] How to operate List using Stream API
[Java11] Stream Summary -Advantages of Stream-
[In-house study session] Java basics-Lambda expression and Stream API- (2017/07/13)
[For beginners] How to operate Stream API after Java 8
Use Java lambda expressions outside of the Stream API
Java8 stream, lambda expression summary
Java
Java Stream cannot be reused.
Use Redis Stream in Java
Java
[Java11] Stream Usage Summary -Basics-
Java Basic Learning Content 8 (Java API)
Java application for beginners: stream
Recent Java API specification generation
[Must-see for apprentice java engineer] How to use Stream API
Handle exceptions coolly with Java 8 lambda expressions and Stream API
Convert 2D array to csv format with Java 8 Stream API
[Java 8] Duplicate deletion (& duplicate check) with Stream
[Java] Stream (filter, map, forEach, reduce)
Export issues using JIRA's Java API
About Lambda, Stream, LocalDate of Java8
Java HTTP Client API timeout setting
[Java] Element existence check with Stream
Generate CloudStack API URL in Java
Create an easy-to-extend Stream API alternative
Hit Zaim's API (OAuth 1.0) in Java
Parsing the COTOHA API in Java
Basic processing flow of java Stream
Call TensorFlow Java API from Scala