Java Stream Terminal Operations Examples

In this tutorial, we will learn Java 8 Stream terminal operations with examples.

Stream operations are divided into intermediate and terminal operations

The terminal operations of the Java Stream interface typically return a single value. Terminal operations can not be chained together. Intermediate operations return another stream as a result, they can be chained together to form a pipeline of operations.

Check out Java Stream Intermediate Operations Examples tutorial.

Terminal Stream operations:

  • anyMatch()
  • allMatch()
  • noneMatch()
  • collect()
  • count()
  • findAny()
  • findFirst()
  • forEach()
  • min()
  • max()
  • reduce()
  • toArray()

anyMatch()

The Java Stream anyMatch() method is a terminal operation that takes a single Predicate as a parameter, starts the internal iteration of the Stream, and applies the Predicate parameter to each element.

Here is a Java Stream anyMatch() example:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<String>();

        stringList.add("Java Guides");
        stringList.add("Python Guides");
        stringList.add("C Guides");

        Stream<String> stream = stringList.stream();

        boolean anyMatch = stream.anyMatch((value) -> { return value.startsWith("Java"); });
        System.out.println(anyMatch);

    }
}

Output:

true

allMatch()

The Java Stream allMatch() method is a terminal operation that takes a single Predicate as the parameter, starts the internal iteration of elements in the Stream, and applies the Predicate parameter to each element.

If the Predicate returns true for all elements in the Stream, the allMatch() will return true. If not all elements match the Predicate, the allMatch() method returns false.

Here is a Java Stream allMatch() example:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<String>();

        stringList.add("Java Guides");
        stringList.add("Python Guides");
        stringList.add("C Guides");

        Stream<String> stream = stringList.stream();

        boolean allMatch = stream.allMatch((value) -> { return value.contains("Guides"); });
        System.out.println(allMatch);

    }
}

Output:

true

noneMatch()

The Java Stream noneMatch() method is a terminal operation that will iterate the elements in the stream and return true or false, depending on whether no elements in the stream match the Predicate passed to noneMatch() as the parameter. 

The noneMatch() method will return true if no elements are matched by the Predicate, and false if one or more elements are matched. 

Here is a Java Stream noneMatch() example:

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<String>();

        stringList.add("john");
        stringList.add("tom");

        Stream<String> stream = stringList.stream();

        boolean noneMatch = stream.noneMatch((element) -> {
            return "Ramesh".equals(element);
        });

        System.out.println("noneMatch = " + noneMatch);

    }
}

Output:

noneMatch = true

collect()

The Java Stream collect() method is a terminal operation that starts the internal iteration of elements and collects the elements in the stream in a collection or object of some kind. 

Here is a simple Java Stream collect() method example:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<String>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("four");
        stringList.add("five");

        Stream<String> stream = stringList.stream();

        List<String> stringsAsUppercaseList = stream
                .map(value -> value.toUpperCase())
                .collect(Collectors.toList());

        System.out.println(stringsAsUppercaseList);

    }
}

Output:

[ONE, TWO, THREE, FOUR, FIVE]

count()

The Java Stream count() method is a terminal operation that starts the internal iteration of the elements in the Stream and counts the elements. 

Here is a Java Stream count() example:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<String>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("four");
        stringList.add("five");

        Stream<String> stream = stringList.stream();

        long coutElements = stream
                .map(value -> value.toUpperCase())
                .count();

        System.out.println(coutElements);

    }
}

Output:

5

findAny()

The Java Stream findAny() method can find a single element from the Stream. The element found can be from anywhere in the Stream. There is no guarantee about from where in the stream the element is taken. 

Here is a Java Stream findAny() example:

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> anyElement = stream.findAny();

        System.out.println(anyElement.get());

    }
}

Output:

one

findFirst()

The Java Stream findFirst() method finds the first element in the Stream if any elements are present in the Stream. The findFirst() method returns an Optional from which you can obtain the element if present. 

Here is a Java Stream findFirst() example:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> result = stream.findFirst();

        System.out.println(result.get());

    }
}

Output:

one

forEach()

The Java Stream forEach() method is a terminal operation that starts the internal iteration of the elements in the Stream and applies a Consumer (java.util.function.Consumer) to each element in the Stream. The forEach() method returns void. 

Here is a Java Stream forEach() example:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        stream.forEach( element -> { System.out.println(element); });
    }
}

Output:

one
two
three
one

min()

The Java Stream min() method is a terminal operation that returns the smallest element in the Stream. 

Here is a Java Stream min() example:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> min = stream.min((val1, val2) -> {
            return val1.compareTo(val2);
        });

        String minString = min.get();

        System.out.println(minString);
    }
}

Output:

one

max()

The Java Stream max() method is a terminal operation that returns the largest element in the Stream.

Here is a Java Stream max() example:
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> max = stream.max((val1, val2) -> {
            return val1.compareTo(val2);
        });

        String maxString = max.get();

        System.out.println(maxString);
    }
}

Output:

two

reduce()

The Java Stream reduce() method is a terminal operation that can reduce all elements in the stream to a single element. 

Here is a Java Stream reduce() example:

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("one");

        Stream<String> stream = stringList.stream();

        Optional<String> reduced = stream.reduce((value, combinedValue) -> {
            return combinedValue + " + " + value;
        });

        System.out.println(reduced.get());
    }
}

Output:

one + three + two + one

toArray()

The Java Stream toArray() method is a terminal operation that starts the internal iteration of the elements in the stream and returns an array of Objects containing all the elements. 

Here is a Java Stream toArray() example:

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

public class Main {
    public static void main(String[] args)
    {
        List<String> stringList = new ArrayList<>();

        stringList.add("one");
        stringList.add("two");
        stringList.add("three");
        stringList.add("four");

        Stream<String> stream = stringList.stream();

        Object[] objects = stream.toArray();

        System.out.println(objects[0]);
        System.out.println(objects[1]);
        System.out.println(objects[2]);
        System.out.println(objects[3]);
    }
}

Output:

one
two
three
four

Free Spring Boot Tutorial | Full In-depth Course | Learn Spring Boot in 10 Hours


Watch this course on YouTube at Spring Boot Tutorial | Fee 10 Hours Full Course

Comments