Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8170438

Reverse Iteration of LinkedHashSet

XMLWordPrintable

      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.

            smarks Stuart Marks
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: