This example shows how to find the element index in Java LinkedHashSet. This example also shows how to find the index of an element in LinkedHashSet using ArrayList, array, and iterator.
How to find the element index in LinkedHashSet in Java?
The LinkedHashSet class does not have any publicly accessible methods using which we can work with the element index. It internally manages a linked list for all of its elements, but there are no public methods to get LinkedHashSet elements by index. However, we can still find the index of the elements using approaches given below.
1. By converting to a List
We can convert LinkedHashSet object to a List like ArrayList or a LinkedList and then we can use the indexOf method to find the element index as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.ArrayList; import java.util.LinkedHashSet; import java.util.List; import java.util.Set; public class LinkedHashSetFindIndexExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); lhSetColors.add("white"); lhSetColors.add("black"); //0 System.out.println("Index of red: " + indexOf(lhSetColors, "red")); //2 System.out.println("Index of blue: " + indexOf(lhSetColors, "blue")); //-1 (does not exist) System.out.println("Index of yellow: " + indexOf(lhSetColors, "yellow")); } private static int indexOf(Set<String> set, String element){ //convert LinkedHashSet to List List<String> list = new ArrayList<String>(set); //get index using indexOf method return list.indexOf(element); } } |
Output
1 2 3 |
Index of red: 0 Index of blue: 2 Index of yellow: -1 |
Note: If you want to search for many elements, convert linked hash set to List only once and then use the indexOf
method to increase the performance. In general, this approach should not be used as it creates a new List from the LinkedHashSet elements and may slow down the performance if the LinkedHashSet has a very large number of elements.
2. Using the iterator or enhanced for loop
We can iterate through elements of LinkedHashSet object using an iterator and get the index of the desired element as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
import java.util.Iterator; import java.util.LinkedHashSet; import java.util.Set; public class LinkedHashSetFindIndexExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); lhSetColors.add("white"); lhSetColors.add("black"); //0 System.out.println("Index of red: " + indexOf(lhSetColors, "red")); //2 System.out.println("Index of blue: " + indexOf(lhSetColors, "blue")); //-1 (does not exist) System.out.println("Index of yellow: " + indexOf(lhSetColors, "yellow")); } private static int indexOf(Set<String> set, String element){ int index = -1; //get an iterator Iterator<String> iterator = set.iterator(); int currentIndex = 0; while(iterator.hasNext()){ if( iterator.next().equals(element) ){ index = currentIndex; break; } currentIndex++; } return index; } } |
Output
1 2 3 |
Index of red: 0 Index of blue: 2 Index of yellow: -1 |
3. Using Apache Commons
We can also convert the LinkedHashSet object to an array and then we can use the indexOf
method of the ArrayUtils class of the Apache commons library to find the index in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import java.util.LinkedHashSet; import java.util.Set; import org.apache.commons.lang3.ArrayUtils; public class LinkedHashSetFindIndexExample { public static void main(String[] args) { LinkedHashSet<String> lhSetColors = new LinkedHashSet<String>(); lhSetColors.add("red"); lhSetColors.add("green"); lhSetColors.add("blue"); lhSetColors.add("white"); lhSetColors.add("black"); //0 System.out.println("Index of red: " + indexOf(lhSetColors, "red")); //2 System.out.println("Index of blue: " + indexOf(lhSetColors, "blue")); //-1 (does not exist) System.out.println("Index of yellow: " + indexOf(lhSetColors, "yellow")); } private static int indexOf(Set<String> set, String element){ //convert LinkedHashSet to an array String[] array = new String[ set.size() ]; array = set.toArray(array); //use indexOf method of the ArrayUtils return ArrayUtils.indexOf(array, element); } } |
Output
1 2 3 |
Index of red: 0 Index of blue: 2 Index of yellow: -1 |
Note: Similar to the List approach, this approach needs to allocate a new array having linked hash set elements and may not perform well for large LinkedHashSet object.
Consider using an index based data structure like a List (ArrayList or LinkedList) instead of LinkedHashSet class.
This example is a part of the LinkedHashSet in Java Tutorial with Examples.
Please let me know your views in the comments section below.
References:
Java 8 LinkedHashSet