-
Enhancement
-
Resolution: Duplicate
-
P4
-
None
-
8, 9
-
generic
-
generic
A DESCRIPTION OF THE REQUEST :
I would like LinkedHashSet to provide a descendingIterator() method like LinkedList does.
Thank you for your consideration.
JUSTIFICATION :
Many times it would be useful to iterate over a LinkedHashSet in reverse. This would allow for the LinkedHashSet to support deque-like behavior while allowing for fast access to entries in the set. The current implementation already "maintains a doubly-linked list running through all of its entries." Thus support for reverse iteration, like LinkedList, should be relatively easy to provide.
CUSTOMER SUBMITTED WORKAROUND :
One workaround is
LinkedHashSet<T> set = ...
LinkedList<T> list = new LinkedList<>(set);
Iterator<T> itr = list.descendingIterator();
while(itr.hasNext()) {
T item = itr.next();
// do something
}
However, this makes a copy of the items in the set and is not practical for large sets. My current workaround is to build my own Collection, which is less desirable than a solution given directly by the SDK.
I would like LinkedHashSet to provide a descendingIterator() method like LinkedList does.
Thank you for your consideration.
JUSTIFICATION :
Many times it would be useful to iterate over a LinkedHashSet in reverse. This would allow for the LinkedHashSet to support deque-like behavior while allowing for fast access to entries in the set. The current implementation already "maintains a doubly-linked list running through all of its entries." Thus support for reverse iteration, like LinkedList, should be relatively easy to provide.
CUSTOMER SUBMITTED WORKAROUND :
One workaround is
LinkedHashSet<T> set = ...
LinkedList<T> list = new LinkedList<>(set);
Iterator<T> itr = list.descendingIterator();
while(itr.hasNext()) {
T item = itr.next();
// do something
}
However, this makes a copy of the items in the set and is not practical for large sets. My current workaround is to build my own Collection, which is less desirable than a solution given directly by the SDK.
- duplicates
-
JDK-4848853 (coll) Modify LinkedHashMap and LinkedHashSet to include first() and last()
-
- Open
-
-
JDK-8266571 Sequenced Collections
-
- Resolved
-
- relates to
-
JDK-7098674 (coll) LinkedHashSet does not implement Queue
-
- Open
-
-
JDK-6310255 (coll) LinkedHashSet should implement List.
-
- Closed
-