

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between Streams and Collections in Java 8
Java Collections framework is used for storing and manipulating group of data. It is an in-memory data structure and every element in the collection should be computed before it can be added in the collections.
Stream API is only used for processing group of data. It does not modify the actual collection, they only provide the result as per the pipelined methods.
Sr. No. | Key | Collections | Streams |
---|---|---|---|
1 | Basic | It is used for storing and manipulating group of data | Stream API is only used for processing group of data |
2 | Package | All the classes and interfaces of this API is in the Java.util package | All the classes and interfaces of this API is in the java.util.stream package |
3 | Eager/Lazy | All the elements in the collections are computed in the beginning. | In streams, intermediate operations are lazy. |
4. | Data Modification | In collections, we can remove or add elements. | We can’t modify streams. |
5 | External /Internal iterator | Collections perform iteration over the collection. | Stream perform iteration internally. |
Example of Collections
public class CollectiosExample { public static void main(String[] args) { List<String> laptopList = new ArrayList<>(); laptopList.add("HCL"); laptopList.add("Apple"); laptopList.add("Dell"); Comparator<String> com = (String o1, String o2)->o1.compareTo(o2); Collections.sort(laptopList,com); for (String name : laptopList) { System.out.println(name); } } }
Example of Streams
public class StreamsExample { public static void main(String[] args) { List<String> laptopList = new ArrayList<>(); laptopList.add("HCL"); laptopList.add("Apple"); laptopList.add("Dell"); laptopList.stream().sorted().forEach(System.out::println); } }
- Related Questions & Answers
- Difference between Traditional Collections and Concurrent Collections in java
- Java 8 Streams and its operations
- Difference between collection and collections in java
- Difference between next() and hasNext() in java collections?
- Streams on Arrays in Java 8
- Difference between Function and Predicate in Java 8
- Difference between intermediate and terminal operations in Java 8
- What is the difference between System.out, System.in and System.err streams in Java?
- Difference between default and static interface method in Java 8.
- Differences between Collection and Collections in Java?
- What is the difference between cerr and clog streams in c++?
- What is the difference between cerr and cout streams in c++?
- What is the difference between cin and cout streams in c++?
- Differences between Java 8 and Java 9?
- Streams and Byte Streams in C#
Advertisements