[Java] Java Stream API

2 minute read

Introduction

Java Are you using it! I often write something recently, but I think it has become quite convenient since the implementation of the Stream API. Typical ones such as Stream.forEach and Stream.filter come out, Some things that you don’t use often don’t come out, so leave them as a memo.

Stream.generate

Creates an unordered, infinite Stream. This is useful for creating irrelevant values such as random number generation.

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

If Predicate<T> is used as a parameter and there is at least one value that matches the conditions, true is returned.

Document: 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);
    }

Results

true

Stream.allMatch

If Predicate<T> is used as a parameter, true is returned if all 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.

Document: 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 while the condition is met is returned. In the example below, numbers less than 0 are 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

- Five
- Four
- 3
- 2
- 1

Stream.dropWhile

With Predicate<T> as a parameter, the value while the condition is not met is returned. 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);
    }

Results

0
1
2
3
Four
Five

Finally

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