BAEL-6838-index-of-set-element (#14771)

Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
Bhaskar Ghosh Dastidar 2023-09-15 04:47:36 +05:30 committed by GitHub
parent 12ba505cb4
commit c1b2a9d4f7
2 changed files with 43 additions and 12 deletions

View File

@ -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<E> {
public int getIndexOf(Set<E> set, E element) {
List<E> arrayList = new ArrayList<>();
arrayList.addAll(set);
return arrayList.indexOf(element);
public int getIndexUsingIterator(Set<E> set, E element) {
Iterator<E> iterator = set.iterator();
int index = 0;
while (iterator.hasNext()) {
if (element.equals(iterator.next())) {
return index;
}
index++;
}
return -1;
}
public int getIndexUsingForEach(Set<E> set, E element) {
int index = 0;
for (E current : set) {
if (element.equals(current)) {
return index;
}
index++;
}
return -1;
}
}

View File

@ -21,9 +21,10 @@ public class IndexOfSetElementsUsingListUnitTest {
set.add(300);
IndexOfElementsInSet<Integer> 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<Integer> 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<Integer> 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<Integer> set = new LinkedHashSet<>();
set.add(100);
set.add(20);
set.add(300);
set.add(0);
set.add(-1);
set.add(300);
IndexOfElementsInSet<Integer> integerIndexOfElementsInSet = new IndexOfElementsInSet<>();
Assert.assertEquals(-1, integerIndexOfElementsInSet.getIndexUsingForEach(set, 150));
}
}