Skip to content
Related Articles

Related Articles

Difference Between Streams and Collections in Java
  • Last Updated : 11 Nov, 2020

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

A Stream is a fixed data structure, in which the elements are computed on demand. 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 Java stream are:

  • It takes input from the Collections, Arrays or I/O channels and is not a data structure.
  • Streams only provide the result as per the pipelined methods and don’t change the original data structure.

    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.

Example of Collections

Java




// Java program to demonstrate
// Collection example
  
import java.util.*;
import java.io.*;
  
class CollectionsExample {
    public static void main(String[] args)
    {
        // create an instance of list
        List<String> CompanyList = new ArrayList<>();
  
        // add elements
        CompanyList.add("Google");
        CompanyList.add("Apple");
        CompanyList.add("Microsoft");
  
        // create a comparator
        Comparator<String> com
            = (String o1, String o2) -> o1.compareTo(o2);
  
        // sort the list
        Collections.sort(CompanyList, com);
  
        // print the elements
        for (String name : CompanyList) {
            System.out.println(name);
        }
    }
}
Output
Apple
Google
Microsoft

Example of Streams

Java




// Java program to demonstrate streams
  
import java.io.*;
import java.util.*;
  
class StreamsExample {
    public static void main(String[] args)
    {
        // create a list
        List<String> CompanyList = new ArrayList<>();
  
        // add elements
        CompanyList.add("Google");
        CompanyList.add("Apple");
        CompanyList.add("Microsoft");
  
        // sort the list
        CompanyList.stream().sorted().forEach(
            System.out::println);
    }
}
Output
Apple
Google
Microsoft

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :