From 63058bc24ceb73ca927dc1fac31245c6f853e3b0 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Thu, 13 Oct 2022 08:45:13 +0530 Subject: [PATCH] Difference Between Java Enumeration and Iterator by amitkumar88265@gmail.com (#12844) * added Enumertion and Iterator code example * formatted the code * formatted the code * move code examples to other modules * corrected the pkg name * corrected the pkg name * fixed compile time issue Co-authored-by: Amit Kumatr --- .../DataUtil.java | 18 +++++++++++++ .../EnumerationExample.java | 19 +++++++++++++ .../IteratorExample.java | 17 ++++++++++++ .../Person.java | 27 +++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/DataUtil.java create mode 100644 core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/EnumerationExample.java create mode 100644 core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/IteratorExample.java create mode 100644 core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/Person.java diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/DataUtil.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/DataUtil.java new file mode 100644 index 0000000000..5d03715b5b --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/DataUtil.java @@ -0,0 +1,18 @@ +package com.baeldung.enumerationiteratordifferences; + +import java.util.Arrays; +import java.util.List; + +public final class DataUtil { + + private DataUtil() { + } + + static List getPersons() { + Person person1 = new Person("amit", "kumar"); + Person person2 = new Person("yogi", "kuki"); + Person person3 = new Person("raj", "dosi"); + Person person4 = new Person("prakash", "kumar"); + return Arrays.asList(person1, person2, person3, person4); + } +} diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/EnumerationExample.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/EnumerationExample.java new file mode 100644 index 0000000000..87f068468a --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/EnumerationExample.java @@ -0,0 +1,19 @@ +package com.baeldung.enumerationiteratordifferences; + +import java.util.Enumeration; +import java.util.Vector; + +import static com.baeldung.enumerationiteratordifferences.DataUtil.getPersons; + +public class EnumerationExample { + public static void main(String[] args) { + + Vector people = new Vector<>(getPersons()); + Enumeration enumeration = people.elements(); + while (enumeration.hasMoreElements()) { + System.out.println("First Name = " + enumeration.nextElement() + .getFirstName()); + } + } + +} diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/IteratorExample.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/IteratorExample.java new file mode 100644 index 0000000000..6ee6af6f22 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/IteratorExample.java @@ -0,0 +1,17 @@ +package com.baeldung.enumerationiteratordifferences; + +import java.util.Iterator; +import java.util.List; + +import static com.baeldung.enumerationiteratordifferences.DataUtil.getPersons; + +public class IteratorExample { + public static void main(String[] args) { + List persons = getPersons(); + Iterator iterator = persons.iterator(); + while (iterator.hasNext()) { + System.out.println("First Name = " + iterator.next() + .getFirstName()); + } + } +} diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/Person.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/Person.java new file mode 100644 index 0000000000..ff7fbbc50f --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/enumerationiteratordifferences/Person.java @@ -0,0 +1,27 @@ +package com.baeldung.enumerationiteratordifferences; + +public class Person { + private String firstName; + private String lastName; + + public Person(String firstName, String lastName) { + this.firstName = firstName; + this.lastName = lastName; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +}