diff --git a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/indexawareset/IndexOfElementsInSet.java b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/indexawareset/IndexOfElementsInSet.java index 736c5c91e4..abedf147a3 100644 --- a/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/indexawareset/IndexOfElementsInSet.java +++ b/core-java-modules/core-java-collections-set-2/src/main/java/com/baeldung/indexawareset/IndexOfElementsInSet.java @@ -1,13 +1,29 @@ package com.baeldung.indexawareset; -import java.util.ArrayList; -import java.util.List; +import java.util.Iterator; import java.util.Set; public class IndexOfElementsInSet { - public int getIndexOf(Set set, E element) { - List arrayList = new ArrayList<>(); - arrayList.addAll(set); - return arrayList.indexOf(element); + public int getIndexUsingIterator(Set set, E element) { + Iterator iterator = set.iterator(); + int index = 0; + while (iterator.hasNext()) { + if (element.equals(iterator.next())) { + return index; + } + index++; + } + return -1; + } + + public int getIndexUsingForEach(Set set, E element) { + int index = 0; + for (E current : set) { + if (element.equals(current)) { + return index; + } + index++; + } + return -1; } } diff --git a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/indexawareset/IndexOfSetElementsUsingListUnitTest.java b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/indexawareset/IndexOfSetElementsUsingListUnitTest.java index 6382db5aee..a1f961a5a4 100644 --- a/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/indexawareset/IndexOfSetElementsUsingListUnitTest.java +++ b/core-java-modules/core-java-collections-set-2/src/test/java/com/baeldung/indexawareset/IndexOfSetElementsUsingListUnitTest.java @@ -21,9 +21,10 @@ public class IndexOfSetElementsUsingListUnitTest { set.add(300); IndexOfElementsInSet integerIndexOfElementsInSet = new IndexOfElementsInSet<>(); - int index100 = integerIndexOfElementsInSet.getIndexOf(set, 100); - Assert.assertEquals(index100, integerIndexOfElementsInSet.getIndexOf(set, 100)); - Assert.assertEquals(-1, integerIndexOfElementsInSet.getIndexOf(set, 12)); + int index100 = integerIndexOfElementsInSet.getIndexUsingIterator(set, 100); + Assert.assertEquals(index100, integerIndexOfElementsInSet.getIndexUsingIterator(set, 100)); + + Assert.assertEquals(-1, integerIndexOfElementsInSet.getIndexUsingIterator(set, 12)); } @Test @@ -37,7 +38,7 @@ public class IndexOfSetElementsUsingListUnitTest { set.add(300); IndexOfElementsInSet integerIndexOfElementsInSet = new IndexOfElementsInSet<>(); - Assert.assertEquals(0, integerIndexOfElementsInSet.getIndexOf(set, 100)); + Assert.assertEquals(0, integerIndexOfElementsInSet.getIndexUsingIterator(set, 100)); } @Test @@ -51,8 +52,8 @@ public class IndexOfSetElementsUsingListUnitTest { set.add(300); IndexOfElementsInSet integerIndexOfElementsInSet = new IndexOfElementsInSet<>(); - Assert.assertEquals(0, integerIndexOfElementsInSet.getIndexOf(set, -1)); - Assert.assertEquals(3, integerIndexOfElementsInSet.getIndexOf(set, 100)); + Assert.assertEquals(0, integerIndexOfElementsInSet.getIndexUsingIterator(set, -1)); + Assert.assertEquals(3, integerIndexOfElementsInSet.getIndexUsingIterator(set, 100)); } @Test @@ -90,4 +91,18 @@ public class IndexOfSetElementsUsingListUnitTest { Assert.assertEquals(2, set.indexOf(-1)); Assert.assertEquals(-1, set.indexOf(100)); } + + @Test + public void givenLinkedHashSet_whenIndexUsingUtilityMethod_thenReturnsIndex() { + Set set = new LinkedHashSet<>(); + set.add(100); + set.add(20); + set.add(300); + set.add(0); + set.add(-1); + set.add(300); + + IndexOfElementsInSet integerIndexOfElementsInSet = new IndexOfElementsInSet<>(); + Assert.assertEquals(-1, integerIndexOfElementsInSet.getIndexUsingForEach(set, 150)); + } }