Tutorial Playlist
Streams in Java are a relatively newer addition to Java. It was introduced in Java 8. Put simply, it is a series of objects that supports several methods. These methods can be pipelined to achieve the desired result. Here is a brief tutorial about “Streams” in Java for learners who want to know more about the Stream API in Java.
This article deals with various topics such as the different operations on Streams, Java Stream interface methods, features of Stream, and a lot more! Read on to unveil more about Streams in Java.
Intermediate operations are operations that change one stream into another stream. These operations are called "intermediate" because they do not produce a final result or a terminal operation but instead return a new stream that can be further operated upon. The following are a few common intermediate operations in Java Streams:
Terminal operations in Java streams are those operations that initiate the processing of the stream elements and return a non-stream result. Here are explanations for three common terminal operations:
Here is a program to demonstrate the use of Stream with the stream() method:
import java.util.Arrays;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    int sum = numbers.stream().mapToInt(Integer::intValue).sum();
    System.out.println("Sum: " + sum);
  }
}
In this program, we have a list of integers called numbers that contains the values 1, 2, 3, 4, and 5.
We use the Stream API to create a stream from the numbers list by calling the stream() method. Then, we use the mapToInt() method to convert the stream of Integer objects to an IntStream, which allows us to perform numeric operations. Finally, we call the sum() method on the IntStream to calculate the sum of the numbers in the stream and then print it.
Java Streams provide several features that make it easy to process data collections concisely and efficiently. Here are some of the main features that streams provide:
Parallelism: Streams can be easily parallelized. This means that they can be split into multiple parts. Then, they are processed in parallel across multiple threads or processors. This can enhance performance for large datasets to a great extent.
Lazy Evaluation: As already mentioned, streams use lazy evaluation. This means intermediate operations are not executed until a terminal operation is called on the stream. This allows for more efficient use of resources and can improve performance for complex stream pipelines.
Method Chaining: Streams support method chaining. This allows multiple operations to be chained together into a single stream pipeline. This makes writing concise and readable codes that perform complex data transformations easy.
Non-mutating Operations: Streams provide a set of non-mutating operations that do not modify the original collection. However, these operations return a new stream with the desired changes instead. This can make it easier to reason about the code. It may also avoid unexpected side effects.
Functional Programming: Streams use functional programming concepts, such as higher-order functions and lambda expressions. It makes it easy to write expressive and reusable code.
Support for Different Data Sources: Streams can be created from various data sources, such as collections, arrays, or files. They can be easily converted into other data structures or formats.
The Java Stream interface provides several methods that allow you to perform various operations on a stream. Here are some of the Java stream interface methods:
First, let us look at an example of filtering a collection in Java without using the Stream API:
Now, let us look at an example of filtering a collection in Java using the Stream API:
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
public class Main {
  public static void main(String[] args) {
    // Create a list of strings
    List<String> names = new ArrayList<>();
    names.add("Alice");
    names.add("Bob");
    names.add("Charlie");
    names.add("David");
    names.add("Eve");
    // Filter the list to get names starting with "A"
    List<String> filteredNames = names.stream()
        .filter(name -> name.startsWith("A"))
        .collect(Collectors.toList());
    // Print the filtered list
    for (String name : filteredNames) {
      System.out.println(name);
    }
  }
}
Here is an example of iterating over a Stream in Java:
import java.util.stream.Stream;
public class Main {
  public static void main(String[] args) {
    // Create a Stream of integers from 1 to 5
    Stream<Integer> stream = Stream.iterate(1, n -> n + 1)
        .limit(5);
    // Iterate over the Stream and print the elements
    stream.forEach(System.out::println);
  }
}
In this example, we create a Stream of integers using the Stream.iterate() method. The iterate() method takes an initial value (1 in this case) and a lambda expression that defines the function to generate the next value based on the previous value (n -> n + 1 in this case). We limit the stream to contain only 5 elements using the limit() method.
Here is an example of using the reduce() method with Stream in Java on a collection:
import java.util.Arrays;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    // Create a list of integers
    List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
    // Use reduce() to calculate the sum of the numbers
    int sum = numbers.stream()
        .reduce(0, Integer::sum);
    System.out.println("Sum: " + sum);
  }
}
Now, here is an example of using the Collectors method with Stream in Java for also calculating the sum of 1, 2, 3, 4, and 5:
Here's an example (download the code) of using Stream in Java to find the maximum and minimum product prices from a collection:
Here is an example of using the count() method with Stream in Java to count the number of elements in a collection:
import java.util.Arrays;
import java.util.List;
public class Main {
  public static void main(String[] args) {
    // Create a list of strings
    List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David", "Eve");
    // Use count() to get the number of elements
    long count = names.stream().count();
    System.out.println("Count: " + count);
  }
}
Here is an example of using Stream in Java to convert a list into a set:
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
public class Main {
  public static void main(String[] args) {
    // Create a list of integers
    List<Integer> numbers = new ArrayList<>();
    numbers.add(1);
    numbers.add(2);
    numbers.add(3);
    numbers.add(3); // Duplicate element
    // Convert the list to a set using Collectors.toSet()
    Set<Integer> uniqueNumbers = numbers.stream()
        .collect(Collectors.toSet());
    // Print the unique numbers in the set
    for (Integer number : uniqueNumbers) {
      System.out.println(number);
    }
  }
}
Now, here is an example of using Stream in Java to convert a list into a map:
All learners must know Java streams in detail. This tutorial will serve as a helpful guide for computer science and programming enthusiasts. If you still have doubts about this concept in Java, consider enrolling in online learning programs.
Platforms like upGrad offer well-designed courses on programming and computer science. Check out their courses to learn more!
1. What is the difference between a stream and a collection in Java?
A collection is a data structure that stores and manages a group of objects. In contrast, a stream is a way to process a group of objects functionally and declaratively.
2. Are Java Streams thread-safe?
No, Java Streams are not thread-safe by default. If a stream is used by multiple threads concurrently, it can lead to race conditions and other concurrency issues.
3. Can Java Streams be infinite?
Yes, Java Streams can be created with an infinite number of elements. This is done without loading all the data into memory at once, allowing for efficient processing of large datasets.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...