Difference Between Streams and Collections in Java

27 May, 2022
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).
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 classesimport java.io.*;import java.util.*; // Main classclass 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);        }    }}
Apple
Google
Microsoft
 Example: Streams
Java
// Java Program to Demonstrate streams // Importing required classesimport java.io.*;import java.util.*; // Main classclass 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);    }}
Apple
Google
Microsoft
Let us tabulate the difference between them which is as follows:
    STREAMS        
  COLLECTIONS          
solankimayank
niteeshasd
Java-Collections
java-stream
Difference Between
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Differences between JDK, JRE and JVM
Stack vs Heap Memory Allocation
Difference between Clustered and Non-clustered index
Arrays in Java
Arrays.sort() in Java with examples
Split() String method in Java with examples
Reverse a string in Java
Object Oriented Programming (OOPs) Concept in Java