What is difference between Iterator and Enumeration?

devquora
devquora

Posted On: Jun 25, 2024

 

Both Iterator and Enumeration are used in Java to iterate over a collection of elements, but they have some important differences.

  • Iterator was introduced in JDK 1.2, while Enumeration was available from the initial version of Java.
  • An iterator is a more modern and powerful way of traversing a collection, it has more functionality than Enumeration.
  • Iterator allows the caller to remove elements from the underlying collection during the iteration with well-defined semantics, Enumeration is read-only and does not provide this functionality.
  • Iterator has a remove() method to remove the current element from the collection, Enumeration does not have this method.
  • Iterator is used in the latest collection classes like ArrayList, LinkedList,

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Core Java Interview Questions

    Explain the significance of class loaders in Bootstrap?

    Classloaders in Java load classes into the virtual environment, converting named classes into binary form. They load essential classes, such as java.lang.Object, on demand. Java Runtime Environment in..

    Core Java Interview Questions

    What is the difference between JDK, JRE, and JVM?

    JVM (Java Virtual Machine) executes Java bytecode, providing a runtime environment. JRE (Java Runtime Environment) includes JVM and necessary libraries. JDK (Java Development Kit) contains JRE and dev..

    Core Java Interview Questions

    What are the various access specifiers in Java?

    Access specifiers in Java determine the access scope of classes, methods, and fields. They include public (accessible from anywhere), protected (accessible within the same package and subclasses), def..