Java Cursors-interview
Q1. What is a Cursor in Java?
A Cursor in Java is an interface used to iterate through collection objects. It allows traversal and sometimes modification of elements in a collection.
Q2. What are the types of Cursors in Java?
- Enumeration → Legacy cursor, only read operations, used in old classes like Vector.
- Iterator → Universal cursor, allows read and remove operations, works across all collections.
- ListIterator → Bi-directional cursor, allows read, remove, replace, and insert, works only with List classes.
Q3. What is the difference between Iterator and ListIterator?
- Iterator → Works in forward direction only, supports remove.
- ListIterator → Works in both directions (forward & backward), supports add, set, remove.
Q4. Can Enumeration remove elements from a collection?
No . Enumeration is a read-only cursor, it only allows traversing elements, not modifying them.
Q5. Which cursor is considered universal and why?
Iterator is called a universal cursor because it can be used with any collection class in Java (unlike Enumeration and ListIterator, which are limited).