🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
List. Each method has its own use cases, advantages, and trade-offs. This guide will cover the most common methods to iterate over a List in Java, including detailed explanations and code examples.Table of Contents
- Introduction
- For-Loop
- Enhanced For-Loop
- Iterator
- ListIterator
- forEach Method (Java 8)
- Stream API (Java 8)
- Conclusion
1. Introduction
Iterating over a List is a common operation in Java programming. Depending on the requirements, such as readability, performance, or functional programming style, you can choose the most suitable iteration method.
2. For-Loop
The traditional for-loop is a basic and flexible way to iterate over a list.
Example: Using For-Loop
import java.util.Arrays;
import java.util.List;
public class ForLoopExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
for (int i = 0; i < fruits.size(); i++) {
System.out.println(fruits.get(i));
}
}
}
3. Enhanced For-Loop
The enhanced for-loop (or for-each loop) is a simpler and more readable way to iterate over a list.
Example: Using Enhanced For-Loop
import java.util.Arrays;
import java.util.List;
public class EnhancedForLoopExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
for (String fruit : fruits) {
System.out.println(fruit);
}
}
}
4. Iterator
The Iterator provides a way to iterate over a collection and remove elements during iteration.
Example: Using Iterator
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class IteratorExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
Iterator<String> iterator = fruits.iterator();
while (iterator.hasNext()) {
String fruit = iterator.next();
System.out.println(fruit);
}
}
}
5. ListIterator
The ListIterator provides additional functionality compared to the Iterator, such as bidirectional traversal and modification of elements.
Example: Using ListIterator
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
public class ListIteratorExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
ListIterator<String> listIterator = fruits.listIterator();
while (listIterator.hasNext()) {
String fruit = listIterator.next();
System.out.println(fruit);
}
System.out.println("Reverse Iteration:");
while (listIterator.hasPrevious()) {
String fruit = listIterator.previous();
System.out.println(fruit);
}
}
}
6. forEach Method (Java 8)
The forEach method is part of the Java 8 Stream API and provides a functional approach to iteration.
Example: Using forEach Method
import java.util.Arrays;
import java.util.List;
public class ForEachMethodExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
// Using forEach with lambda expression
fruits.forEach(fruit -> System.out.println(fruit));
// Using forEach with method reference
fruits.forEach(System.out::println);
}
}
7. Stream API (Java 8)
The Stream API provides a powerful way to process sequences of elements, including iteration.
Example: Using Stream API
import java.util.Arrays;
import java.util.List;
public class StreamAPIExample {
public static void main(String[] args) {
List<String> fruits = Arrays.asList("Apple", "Banana", "Orange");
// Using stream and forEach
fruits.stream().forEach(fruit -> System.out.println(fruit));
// Using parallel stream and forEach
fruits.parallelStream().forEach(fruit -> System.out.println("Parallel: " + fruit));
}
}
8. Conclusion
In this guide, we covered various methods to iterate over a List in Java:
- For-Loop: Basic and flexible.
- Enhanced For-Loop: Simplifies code and improves readability.
- Iterator: Allows element removal during iteration.
- ListIterator: Supports bidirectional traversal and element modification.
- forEach Method (Java 8): Provides a functional programming approach.
- Stream API (Java 8): Offers powerful operations for processing sequences of elements.
Each method has its own use cases and advantages. Choose the one that best fits your requirements for readability, functionality, and performance.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment