BAEL-6838-index-of-set-element (#14747)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
6978371d17
commit
42188fdc2d
|
@ -20,6 +20,12 @@
|
|||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>4.4</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.testng</groupId>
|
||||
<artifactId>testng</artifactId>
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.indexawareset;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.indexawareset;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
|
||||
public class InsertionIndexAwareSet<E> extends LinkedHashSet<E> {
|
||||
public InsertionIndexAwareSet() {
|
||||
super();
|
||||
}
|
||||
|
||||
public int getIndexOf(E element) {
|
||||
int index = 0;
|
||||
for (E current : this) {
|
||||
if (current.equals(element)) {
|
||||
return index;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,93 @@
|
|||
package com.baeldung.indexawareset;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.apache.commons.collections4.set.ListOrderedSet;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class IndexOfSetElementsUsingListUnitTest {
|
||||
@Test
|
||||
public void givenHashSet_whenIndexOfElement_thenGivesIndex() {
|
||||
Set<Integer> set = new HashSet<>();
|
||||
set.add(100);
|
||||
set.add(20);
|
||||
set.add(300);
|
||||
set.add(0);
|
||||
set.add(-1);
|
||||
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));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLinkedHashSet_whenIndexOfElement_thenGivesIndex() {
|
||||
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(0, integerIndexOfElementsInSet.getIndexOf(set, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTreeSet_whenIndexOfElement_thenGivesIndex() {
|
||||
Set<Integer> set = new TreeSet<>();
|
||||
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(0, integerIndexOfElementsInSet.getIndexOf(set, -1));
|
||||
Assert.assertEquals(3, integerIndexOfElementsInSet.getIndexOf(set, 100));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIndexAwareSet_whenIndexOfElement_thenGivesIndex() {
|
||||
InsertionIndexAwareSet<Integer> set = new InsertionIndexAwareSet<>();
|
||||
set.add(100);
|
||||
set.add(20);
|
||||
set.add(300);
|
||||
Assert.assertEquals(0, set.getIndexOf(100));
|
||||
Assert.assertEquals(2, set.getIndexOf(300));
|
||||
Assert.assertEquals(-1, set.getIndexOf(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIndexAwareSetWithStrings_whenIndexOfElement_thenGivesIndex() {
|
||||
InsertionIndexAwareSet<String> set = new InsertionIndexAwareSet<>();
|
||||
set.add("Go");
|
||||
set.add("Java");
|
||||
set.add("Scala");
|
||||
set.add("Python");
|
||||
Assert.assertEquals(0, set.getIndexOf("Go"));
|
||||
Assert.assertEquals(2, set.getIndexOf("Scala"));
|
||||
Assert.assertEquals(-1, set.getIndexOf("C++"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOrderedSet_whenIndexOfElement_thenGivesIndex() {
|
||||
ListOrderedSet<Integer> set = new ListOrderedSet<>();
|
||||
set.add(12);
|
||||
set.add(0);
|
||||
set.add(-1);
|
||||
set.add(50);
|
||||
|
||||
Assert.assertEquals(0, set.indexOf(12));
|
||||
Assert.assertEquals(2, set.indexOf(-1));
|
||||
Assert.assertEquals(-1, set.indexOf(100));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue