Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference Between Streams and Collections in Java

  • Last Updated : 20 Nov, 2021

A 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. A stream is a sequence of objects that supports various methods that can be pipelined to produce the desired result.

The features of the Java stream are:

  • 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. Streams only provide the result as per the pipelined methods and don’t change the original data structure.
  • Each intermediate operation is lazily executed and returns a 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.They don’t use functional interfaces.
Java Streams are consumable i.e; to traverse the stream, it needs to be created every time.They are non-consumable i.e; can be traversable multiple times without creating it again.
Java streams support both sequential and parallel processing.It 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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!