Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7986417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:50:58+00:00 2026-06-04T11:50:58+00:00

Is there an existing List implementation in Java that maintains order based on provided

  • 0

Is there an existing List implementation in Java that maintains order based on provided Comparator?

Something that can be used in the following way:

Comparator<T> cmp = new MyComparator<T>();
List<T> l = new OrderedList<T>(cmp);
l.add(someT);

so that someT gets inserted such that the order in the list is maintained according to cmp

(On @andersoj suggestion I am completing my question with one more request)

Also I want to be able to traverse the list in sorted order without removing the elements, i.e:

T min = Const.SMALLEST_T;
for (T e: l) {
  assertTrue(cmp.compare(min, e) >= 0);
  min = e;
}

should pass.

All suggestions are welcome (except telling me to use Collections.sort on the unordered full list), though, I would prefer something in java.* or eventually org.apache.* since it would be hard to introduce new libraries at this moment.

Note: (UPDATE4) I realized that implementations of this kind of list would have inadequate performance. There two general approaches:

  1. Use Linked structure (sort of) B-tree or similar
  2. Use array and insertion (with binary search)

No 1. has problem with CPU cache misses
No 2. has problem with shifting elements in array.

UPDATE2:
TreeSet does not work because it uses the provided comparator (MyComparator) to check for equality and based on it assumes that the elements are equal and exclude them. I need that comparator only for ordering, not “uniqueness” filtering (since the elements by their natural ordering are not equal)

UPDATE3:
PriorityQueue does not work as List (as I need) because there is no way to traverse it in the order it is “sorted”, to get the elements in the sorted order you have to remove them from the collection.

UPDATE:

Similar question:
A good Sorted List for Java
Sorted array list in Java

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:50:59+00:00Added an answer on June 4, 2026 at 11:50 am

    Response to new requirement. I see two potentials:

    • Do what the JavaDoc for PriorityQueue says:

      This class and its iterator implement all of the optional methods of the Collection and Iterator interfaces. The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

      I suspect this will yield the best performance given your requirements. If this is not acceptable, you’ll need to better explain what you’re trying to accomplish.

    • Build a List that simply sorts itself upon addition of new elements. This is a real pain… if you used a linked structure, you can do an efficient insertion sort, but locality is bad. If you used an array-backed structure, insertion sort is a pain but traversal is better. If iteration/traversal is infrequent, you could hold the list contents unsorted and sort only on demand.

    • Consider using a PriorityQueue as I suggested, and in the event you need to iterate in order, write a wrapper iterator:

      class PqIter implements Iterator<T>
      {
         final PriorityQueue<T> pq;
         public PqIter(PriorityQueue <T> source)
         {
           pq = new PriorityQueue(source); 
         }
      
         @Override
         public boolean hasNext()
         {
           return pq.peek() != null
         }
      
         @Override
         public T next()
         { return pq.poll(); }
      
         @Override
         public void remove()
         { throw new UnsupportedOperationException(""); }
      }
      
    • Use Guava’s TreeMultiSet. I tested the following code with Integer and it seems to do the right thing.

      import com.google.common.collect.TreeMultiset;
      
      public class TreeMultiSetTest { 
        public static void main(String[] args) {
          TreeMultiset<Integer> ts = TreeMultiset.create();
          ts.add(1);  ts.add(0); ts.add(2);
          ts.add(-1); ts.add(5); ts.add(2);
      
          for (Integer i : ts) {
            System.out.println(i);
          } 
        } 
      }
      

    The below addresses the uniqueness/filtering problem you were having when using a SortedSet. I see that you also want an iterator, so this won’t work.

    If what you really want is an ordered list-like thing, you can make use of a PriorityQueue.

    Comparator<T> cmp = new MyComparator<T>();
    PriorityQueue<T> pq = new PriorityQueue<T>(cmp);
    pq.add(someT);
    

    Take note of what the API documentation says about the time properties of various operations:

    Implementation note: this implementation provides O(log(n)) time for the enqueing and dequeing methods (offer, poll, remove() and add); linear time for the remove(Object) and contains(Object) methods; and constant time for the retrieval methods (peek, element, and size).

    You should also be aware that the iterators produced by PriorityQueue do not behave as one might expect:

    The Iterator provided in method iterator() is not guaranteed to traverse the elements of the priority queue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).

    I just noticed that Guava provides a MinMaxPriorityQueue. This implementation is array-backed, rather than the linked form provided in the JDK’s PriorityQueue, and thus likely has different timing behavior. If you’re doing something performance sensitive, you may wish to take a look. While the notes give slightly different (linear and logarithmic) big-O times, all those times should also be bounded, which may be useful.

    There is not a List implementation per se that maintains ordering, but what you are likely looking for are implementations of SortedSet. A TreeSet is the most common. The other implementation, a ConcurrentSkipListSet is for more specific uses. Note that a SortedSet provides ordering, but does not allow duplicate entries, as does a List.

    Refs:

    • Blog post PriorityQueue iterator is not ordered
    • SO question on PQ ordered iterator
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there something existing in python that can convert an increasing list of integers
Is there existing software for discriminative reranking, such as that used by the Charniak
Is there any existing implementation that compute this in Perl? nCk (n choose k),
Is there an existing application or library in Java which will allow me to
Are there any existing libraries in existence that will parse a datetime from a
I have generalized the existing Data.List.partition implementation partition :: (a -> Bool) -> [a]
Are there existing template extract libraries in either python or php? Perl has Template::Extract
Is there an existing solution to create a regular expressions dynamically out of a
Are there any existing solutions for remote execution of commands on a windows server
Is there any existing code in Apache HttpClient or in the servlet API to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.