WalzoneInterview Prep
๐Ÿ“ž Interviewing soon? Practice with a realistic AI mock phone interview โ€” it calls you, then scores you. First 15 min FREE โ†’

Java Collections ยท Expert ยท question 76 of 100

How do you implement a custom Iterator in Java Collections that supports removing elements, and what are some use cases for doing so?

๐Ÿ“• Buy this interview preparation book: 100 Java Collections questions & answers โ€” PDF + EPUB for $5

To implement a custom Iterator in Java Collections that supports removing elements, you need to implement the Iterator interface and override the remove() method.

The Iterator interface has three methods that must be implemented: hasNext(), next(), and remove(). The hasNext() method returns a boolean value that indicates whether there are more elements in the collection to iterate over. The next() method returns the next element in the collection. The remove() method removes the last element returned by the next() method from the collection.

Here is an example code that demonstrates how to implement a custom Iterator in Java Collections that supports removing elements:

import java.util.ArrayList;
import java.util.Iterator;

public class CustomIterator implements Iterable<String> {
    private ArrayList<String> list;

    public CustomIterator(ArrayList<String> list) {
        this.list = list;
    }

    public Iterator<String> iterator() {
        return new Iterator<String>() {
            int currentIndex = 0;

            public boolean hasNext() {
                return currentIndex < list.size();
            }

            public String next() {
                return list.get(currentIndex++);
            }

            public void remove() {
                list.remove(--currentIndex);
            }
        };
    }
}

In this example, we create a custom Iterable class called CustomIterator that takes an ArrayList of strings as its parameter. We then implement the iterator() method to return an instance of an anonymous class that implements the Iterator interface. This anonymous class overrides the hasNext(), next(), and remove() methods.

The hasNext() method simply checks if the current index is less than the size of the list. The next() method returns the element at the current index and increments the current index. The remove() method removes the element at the current index from the list and decrements the current index.

Here is an example of how to use this custom Iterator to remove elements from a list:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        list.add("one");
        list.add("two");
        list.add("three");

        CustomIterator customIterator = new CustomIterator(list);
        Iterator<String> iterator = customIterator.iterator();
        while (iterator.hasNext()) {
            String element = iterator.next();
            if (element.equals("two")) {
                iterator.remove();
            }
        }

        System.out.println(list); // prints [one, three]
    }
}

In this example, we create an ArrayList of strings and add three elements to it. We then create an instance of the CustomIterator class and call its iterator() method to get an iterator that supports removing elements. We use a while loop to iterate over the elements in the list, and if an element equals "two", we call the remove() method on the iterator to remove it from the list. Finally, we print the modified list to confirm that the element has been removed.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Java Collections interview โ€” then scores it.
๐Ÿ“ž Practice Java Collections โ€” free 15 min
๐Ÿ“• Buy this interview preparation book: 100 Java Collections questions & answers โ€” PDF + EPUB for $5

All 100 Java Collections questions ยท All topics