Open In App

Difference Between Streams and Collections in Java

Improve
Improve
Like Article
Like
Save
Share
Report

Collection is an in-memory data structure, which holds all the values that the data structure currently has. Every element in the Collection has to be computed before we add it to the Collection. Operations such as searching, sorting, insertion, manipulation, and deletion can be performed on a Collection. It provides many interfaces like (Set, List, Queue, Deque) and Classes like (ArrayList, Vector, LinkedList, PriorityQueue, HashSet).

Difference-Between-Streams-and-Collections-in-Java

On the other hand, IStream is an API that is introduced in Java 8 which is used to process collections of objects. A stream is a sequence of objects that supports various methods which can be pipelined to produce the desired result. The Stream API is used to process collections of objects.

Main features of the Java stream are as follows:

  • A stream is not a data structure instead it takes input from the Collections, Arrays, or I/O channels.
  • Streams don’t change the original data structure, they only provide the result as per the pipelined methods.
  • Each intermediate operation is lazily executed and returns another stream as a result, hence various intermediate operations can be pipelined. Terminal operations mark the end of the stream and return the result.

Example 1: Collections

Java




// Java Program to Illustrate Collection
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GfG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an instance of list
        List<String> companyList = new ArrayList<>();
 
        // Adding elements using add() method
        companyList.add("Google");
        companyList.add("Apple");
        companyList.add("Microsoft");
 
        // Now creating a comparator
        Comparator<String> com
            = (String o1, String o2) -> o1.compareTo(o2);
 
        // Sorting the list
        Collections.sort(companyList, com);
 
        // Iterating using for each loop
        for (String name : companyList) {
 
            // Printing the elements
            System.out.println(name);
        }
    }
}


Output

Apple
Google
Microsoft

 Example: Streams

Java




// Java Program to Demonstrate streams
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Creating an empty Arraylist
        List<String> companyList = new ArrayList<>();
 
        // Adding elements to above ArrayList
        companyList.add("Google");
        companyList.add("Apple");
        companyList.add("Microsoft");
 
        // Sorting the list
        // using sorted() method and
        // printing using for-each loop
        companyList.stream().sorted().forEach(
            System.out::println);
    }
}


Output

Apple
Google
Microsoft

Let us tabulate the difference between them which is as follows:

    STREAMS        

  COLLECTIONS          

It doesn’t store data, it operates on the source data structure i.e collection. It stores/holds all the data that the data structure currently has in a particular data structure like Set, List or Map,
They use functional interfaces like lambda which makes it a good fit for programming language. Don’t use functional interfaces.
Java Streams are consumable i.e; to traverse the stream, it needs to be created every time. Non-consumable i.e; can be traversable multiple times without creating it again.
Java streams support both sequential and parallel processing. Supports parallel processing and parallel processing can be very helpful in achieving high performance.
All the Java stream API interfaces and classes are in java.util.stream package. Specific classes for primitive types such as IntStream, LongStream, and DoubleStream are used in collections since primitive data types such as int, long in the collections using auto-boxing and these operations could take a lot of time.
Streams are not modifiable i.e one can’t add or remove elements from streams. These are modifiable i.e one can easily add to or remove elements from collections.
Streams are iterated internally by just mentioning the operations. Collections are iterated externally using loops.


Last Updated : 07 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads