By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Iterating collections is a fundamental skill in Java programming. It involves traversing elements in a collection, such as lists or sets, using different techniques like Iterator, ListIterator, and for-each loops. Mastering these methods is crucial for efficient data processing, especially in applications requiring complex data manipulation. Incorrect iteration can lead to runtime errors, inefficient code, and incorrect data processing, which can severely impact application performance and accuracy. For example, improper use of Iterator can result in ConcurrentModificationException, causing your program to crash.
java List<String> list = new ArrayList<>(); list.add("A"); list.add("B"); list.add("C"); Iterator<String> iterator = list.iterator();
java while (iterator.hasNext()) { System.out.println(iterator.next()); }
java while (iterator.hasNext()) { if (iterator.next().equals("B")) { iterator.remove(); } }
java ListIterator<String> listIterator = list.listIterator();
java while (listIterator.hasNext()) { System.out.println(listIterator.next()); } while (listIterator.hasPrevious()) { System.out.println(listIterator.previous()); }
java for (String item : list) { System.out.println(item); }
Experts view iteration as a toolbox with different tools for different jobs. They choose Iterator for general traversal, ListIterator for bidirectional needs, and for-each for simplicity and readability. They also understand the underlying mechanics of each method to avoid common pitfalls and exceptions.
Scenario: You need to remove all even numbers from a list of integers.Question: How would you safely remove the even numbers? Solution: 1. Create an Iterator for the list.2. Use hasNext() and next() to traverse the list.3. Check if the number is even.4. Use remove() to delete even numbers.Answer:
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5)); Iterator<Integer> iterator = numbers.iterator(); while (iterator.hasNext()) { if (iterator.next() % 2 == 0) { iterator.remove(); } }
Why it works: Iterator's remove() method safely modifies the collection.
Scenario: You need to print elements of a list in reverse order.Question: How would you achieve this using ListIterator? Solution: 1. Create a ListIterator for the list.2. Move to the end of the list using hasNext() and next().3. Use hasPrevious() and previous() to traverse backward.Answer:
List<String> list = new ArrayList<>(Arrays.asList("A", "B", "C")); ListIterator<String> listIterator = list.listIterator(); while (listIterator.hasNext()) { listIterator.next(); } while (listIterator.hasPrevious()) { System.out.println(listIterator.previous()); }
Why it works: ListIterator supports bidirectional traversal.
Scenario: You need to print all elements of a set.Question: How would you do this using a for-each loop? Solution: 1. Use a for-each loop to iterate over the set.2. Print each element.Answer:
Set<String> set = new HashSet<>(Arrays.asList("A", "B", "C")); for (String item : set) { System.out.println(item); }
Why it works: For-each loops provide a simple and readable way to iterate.
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.