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; + } +}