How to get index of an item in java.util.Set

I know the differences between Set and List(unique vs. duplications allowed, not ordered/ordered, etc). What I'm looking for is a set that keeps the elements ordered(that's easy), but I also need to be able to recover the index in which an element was inserted. So if I insert four elements, then I want to be able to know the order in which one of them was inserted.

MySet<String> set = MySet<String>();

int index = set.getIndex("two");

So at any given moment I can check if a String was already added, and get the index of the string in the set. Is there anything like this, or I need to implement it myself?

A small static custom method in a Util class would help:

 public static int getIndex(Set<? extends Object> set, Object value) {
   int result = 0;
   for (Object entry:set) {
     if (entry.equals(value)) return result;
   return -1;

If you need/want one class that is a Set and offers a getIndex() method, I strongly suggest to implement a new Set and use the decorator pattern:

 public class IndexAwareSet<T> implements Set {
   private Set<T> set;
   public IndexAwareSet(Set<T> set) {
     this.set = set;

   // ... implement all methods from Set and delegate to the internal Set

   public int getIndex(T entry) {
     int result = 0;
     for (T entry:set) {
       if (entry.equals(value)) return result;
     return -1;

Java Collections, Index of an element of a java.util.Set can be found by converting it to an a java.util​.List: package com.logicbig.example; import java.util.ArrayList; import java.util. Set<Integer> mySet = new HashSet<> (); mySet.add(5); mySet.add(7); mySet.add(-4); mySet.add(2); System.out.format("Set: %s%n", mySet); int i = new ArrayList<> (mySet).indexOf(2); System.out.format("index of 2: %d%n", i); }

After creating Set just convert it to List and get by index from List:

Set<String> stringsSet = new HashSet<>();

List<String> stringsList = new ArrayList<>(stringsSet);
stringsList.get(0); // "string1";
stringsList.get(1); // "string2";

Get Elements by Index from HashSet in Java Example, Example also shows how to get HashSet elements using index using 26. 27. 28. import java.util.HashSet;. import java.util.Set;. public class  The java.util.ArrayList.set(int index, E element) replaces the element at the specified position in this list with the specified element. Declaration. Following is the declaration for java.util.ArrayList.set() method. public E set(int index, E element) Parameters. index − This is the index of the element to replace.

you can extend LinkedHashSet adding your desired getIndex() method. It's 15 minutes to implement and test it. Just go through the set using iterator and counter, check the object for equality. If found, return the counter.

Java Set – Set in Java, The set() method of java.util.ArrayList class is used to replace the element at the specified position in this list with the catch (IndexOutOfBoundsException e) {. Important Note: As we can see from the output above, even though we could get the elements by index, it is not in the order of the insertion. For example, we added element “10” first in the set, but in the output, we can see that it is at index 4.

How about add the strings to a hashtable where the value is an index:

  Hashtable<String, Integer> itemIndex = new Hashtable<>();
  itemIndex.put("First String",1);
  itemIndex.put("Second String",2);
  itemIndex.put("Third String",3);

  int indexOfThirdString = itemIndex.get("Third String");

ArrayList set() method in Java with Examples, The get() method of ArrayList in Java is used to get the element of a specified index within the list. IndexOutOfBoundsException: Index: 5, Size: 4 at java.util. The get(int index) method is used to return the element at the specified index position in this Vector. Declaration. Following is the declaration for java.util.Vector

One solution (though not very pretty) is to use Apache common List/Set mutation

import org.apache.commons.collections.list.SetUniqueList;

final List<Long> vertexes=SetUniqueList.setUniqueList(new LinkedList<>());

it is a list without duplicates

ArrayList get(index) method in Java with examples, index − This is the index of the element to replace. element − This is the element to be stored at the specified position. Return Value. This method returns the  I've seen other questions about getting objects from Set's based on index value and I understand why that is not possible.But I haven't been able to find a good explanation for why a get by object is not allowed so thought I would ask.

Java.util.ArrayList.set() Method, o − The element to search for. Return Value. This method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not  There is no guarantee that you will always get the "first" element even if you use an iterator over a HashSet like you have done in the question. If you need to have predictable ordering, you need to use the LinkedHashSet implementation. When you iterate over a LinkedHashSet, you will get the elements in the order you inserted.

Java.util.ArrayList.indexOf() Method, Java.util.ArrayList class method indexOf(Object o) is used to find out the index of a indexOf() method to find the index of few specified elements in the ArrayList. A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element.As implied by its name, this interface models the mathematical set abstraction.

Java ArrayList indexOf() Method example, Use ArrayList.indexOf() method to get the first occurrence of the element. Java program for how to get first index of object in arraylist. In this import java.util. Java.util.ArrayList.get() Method - The java.util.ArrayList.get(int index) method returns the element at the specified position in this list.

  • What would you expect getIndex to return if you'd removed "one" in the meantime?
  • if it's not in commons-collections then I think you'll have to implement it yourself
  • Well, since the set should be ordered, if an element is removed all the elements inserted after that would be reallocated one position. I suppose that if I do a getIndex of an element that does not exist, or has been removed, I would get a -1.
  • Thanks for the example. One question, is it worth to call contains(T entry) before iterating through the Set?
  • @Jose - depends on the Set. HashSet: yes, contains runs in O(1). TreeSet - no, as contains need O(n) exactly like the getIndex algorithm. So it would take twice as long.
  • Note that if you decorate a HashSet, you won't get the desired behavior (i.e. index according to insertion order). You must provide a Set whose iteration order matched the insertion order. LinkedHashSet is capable of it.
  • -1: I still don't see how this response answers the original problem. There is no guarantee that the indices will reflect the insertion order. Iteration over TreeSet or HashSet will not necessarily follow the insertion order.
  • @Eyal - (1) TreeSet does not preserve insertion order others mentioned, OP should use LinkedHashSet. (2) both snippets/examples can be used with any Set implementation. You just need to say new IndexAwareSet(new LinkedHashSet()) and it should work as expected. BTW - I mentioned HashSet and TreeSet only to answer Jose's question in his comment, not in the answer.
  • I believe the question was how to get the index of an item in a set and not how to get an item by an index.
  • stringsList.indexOf("string1");
  • TreeSet will not keep the order in which the elements were added, they will be ordered in their natural order.