How can I iterate through items of a LinkedHashSet
from the last item to the first one?

- 4,165
- 12
- 42
- 51
-
2Why in Java Collection Framework it is not included. LinkedHashSet maintains FIFO order so an utility to convert the order into LIFO should be there which seems to be vary handy when requirement is to maintain insertion order at the same time we may need to iterate in reverse order. In my project there were couple time I needed this already. Unfortunately, I have to use List as intermediary to take advantage of Collections.reverse() utility. This is dirty isn't it! – Bhavesh Apr 03 '14 at 12:15
-
This has been a problem since forever. There are [proposals](http://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076461.html) floating around to address it. – Hollis Waite Apr 17 '21 at 02:39
6 Answers
If you want to continue to use collections, you could use the following:
LinkedHashSet<T> set = ...
LinkedList<T> list = new LinkedList<>(set);
Iterator<T> itr = list.descendingIterator();
while(itr.hasNext()) {
T item = itr.next();
// do something
}
If you're fine with using an array instead, you could take a look at hvgotcodes' answer.
er, assuming you mean LinkedHashSet...
I would use toArray and just use a reverse for loop.
There might be a better way to do it, but that should work. toArray
guarantees any order is preserved
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order.
Something like
Set<MyType> mySet = new LinkedHashSet();
...
MyType[] asArray = mySet.toArray(new MyType[0]);
for (int i = asArray.length - 1; i>=0; i--){
..
}

- 5,178
- 2
- 23
- 37

- 118,147
- 33
- 203
- 236
-
1your code seems cann't compile: `MyType[] asArray = mySet.toArray();` – ZhaoGang Jun 28 '20 at 13:34
-
Object array cannot be converted to MyType array. This code won't compile. – Soham De Nov 29 '20 at 06:02
-
This is another way:
LinkedHashSet<T> set = ...
List<T> list = new ArrayList<>(set);
Collections.reverse(list);
for( T item : list ){
...
}

- 23,114
- 6
- 54
- 68
If you really meant LinkedHashSet, you could put the elements into an ArrayList and then use the ArrayList's ListIterator.
ListIterator<T> l = new ArrayList<T>(yourLinkedHashList).listIterator();
// ListIterator can iterate in reverse
while(l.hasPrevious()) {
T obj = l.previous();
}

- 22,940
- 6
- 79
- 92
JEP 431: Sequenced Collections in the upcoming version 21 of Java adds a reversed()
method to LinkedHashSet
. This reversed view of the set can be used to iterate in reverse order.
for (Object o : myLinkedHashSet.reversed()) {
System.out.println(o);
}

- 14,487
- 7
- 91
- 130
From the javadoc: "This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order)."
So you can simply:
LinkedHashSet<Integer> numbers = new LinkedHashSet<Integer>();
numbers.add(1);
numbers.add(2);
numbers.add(33);
numbers.add(44);
numbers.add(108);
for (Integer i : numbers) {
System.out.println(i);
}

- 112
- 4