Learn and master Java Collections Framework at https://www.javaguides.net/p/java-collections-tutorial.html
Collections vs Streams in Java
Main Use
Collections are used to store and group the data in a particular data structure like List, Set, or Map. Whereas Streams are used to perform complex data processing operations like filtering, matching, mapping, etc on stored data such as arrays, collections, or I/O resources.// Creating an ArrayList of String using List<String> fruits = new ArrayList<>(); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("mango"); fruits.add("orange");
List<String> lines = Arrays.asList("java", "c", "python");
List<String> result = lines.stream() // convert list to stream
.filter(line -> !"c".equals(line)) // we dont like c
.collect(Collectors.toList()); // collect the output and convert streams to a List
result.forEach(System.out::println); Modification
For example:
// Creating an ArrayList of String using List < String > fruits = new ArrayList < > (); // Adding new elements to the ArrayList fruits.add("Banana"); fruits.add("Apple"); fruits.add("Mango"); fruits.add("Orange"); fruits.add("Pineapple"); fruits.add("Grapes"); System.out.println(fruits); // Remove the element at index `5` fruits.remove(5); System.out.println("After remove(5): " + fruits);
Iteration
Streams perform iteration internally behind the scene for us (using the forEach() method). We just have to mention the operations to be performed on a source. On the other hand, we have to do the iteration externally over collections using loops.Example 1: External iterations of Collections using for loops
// Creating an ArrayList of String using
List < String > fruits = new ArrayList < > ();
// Adding new elements to the ArrayList
fruits.add("Banana");
fruits.add("Apple");
fruits.add("mango");
fruits.add("orange");
fruits.add("Watermelon");
fruits.add("Strawberry");
System.out.println("\n=== Iterate using for loop with index ===");
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
System.out.println("=== Iterate using Java 8 forEach and lambda ===");
fruits.forEach(fruit - > {
System.out.println(fruit);
});List<String> lines = Arrays.asList("java", "c", "python");
List<String> result = lines.stream() // convert list to stream
.filter(line -> !"c".equals(line)) // we dont like c
.collect(Collectors.toList()); // collect the output and convert streams to a List
result.forEach(System.out::println); Traversability
List<Integer> numbers = Arrays.asList(4, 2, 8, 9, 5, 6, 7);
Stream<Integer> numbersGreaterThan5 = numbers.stream().filter(i -> i > 5);
//Traversing numbersGreaterThan5 stream first time
numbersGreaterThan5.forEach(System.out::println);
//Second time traversal will throw error
//Error : stream has already been operated upon or closed
numbersGreaterThan5.forEach(System.out::println);Construction
Collections are eagerly constructed i.e. all the elements are computed at the beginning itself. But, streams are lazily constructed i.e. intermediate operations are not evaluated until the terminal operation is invoked.YouTube Video
Cheat Sheet: Collections vs Streams
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment