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 7031843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:51:22+00:00 2026-05-28T00:51:22+00:00

In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java

  • 0

In Java there are the SortedSet and SortedMap interfaces. Both belong to the Java Collections framework and provide a sorted way to access the elements.

However, in my understanding there is no SortedList in Java. You can use java.util.Collections.sort() to sort a list.

Any idea why it is designed like that?

  • 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-05-28T00:51:23+00:00Added an answer on May 28, 2026 at 12:51 am

    List iterators guarantee first and foremost that you get the list’s elements in the internal order of the list (aka. insertion order). More specifically it is in the order you’ve inserted the elements or on how you’ve manipulated the list. Sorting can be seen as a manipulation of the data structure, and there are several ways to sort the list.

    I’ll order the ways in the order of usefulness as I personally see it:

    1. Consider using Set or Bag collections instead

    NOTE: I put this option at the top because this is what you normally want to do anyway.

    A sorted set automatically sorts the collection at insertion, meaning that it does the sorting while you add elements into the collection. It also means you don’t need to manually sort it.

    Furthermore if you are sure that you don’t need to worry about (or have) duplicate elements then you can use the TreeSet<T> instead. It implements SortedSet and NavigableSet interfaces and works as you’d probably expect from a list:

    TreeSet<String> set = new TreeSet<String>();
    set.add("lol");
    set.add("cat");
    // automatically sorts natural order when adding
    
    for (String s : set) {
        System.out.println(s);
    }
    // Prints out "cat" and "lol"
    

    If you don’t want the natural ordering you can use the constructor parameter that takes a Comparator<T>.

    Alternatively, you can use Multisets (also known as Bags), that is a Set that allows duplicate elements, instead and there are third-party implementations of them. Most notably from the Guava libraries there is a TreeMultiset, that works a lot like the TreeSet.

    2. Sort your list with Collections.sort()

    As mentioned above, sorting of Lists is a manipulation of the data structure. So for situations where you need "one source of truth" that will be sorted in a variety of ways then sorting it manually is the way to go.

    You can sort your list with the java.util.Collections.sort() method. Here is a code sample on how:

    List<String> strings = new ArrayList<String>()
    strings.add("lol");
    strings.add("cat");
    
    Collections.sort(strings);
    for (String s : strings) {
        System.out.println(s);
    }
    // Prints out "cat" and "lol"
    

    Using comparators

    One clear benefit is that you may use Comparator in the sort method. Java also provides some implementations for the Comparator such as the Collator which is useful for locale sensitive sorting strings. Here is one example:

    Collator usCollator = Collator.getInstance(Locale.US);
    usCollator.setStrength(Collator.PRIMARY); // ignores casing
    
    Collections.sort(strings, usCollator);
    

    In Java 8+, more simply call List#sort.

    strings.sort( usCollator ) ;
    

    Sorting in concurrent environments

    Do note though that using the sort method is not friendly in concurrent environments, since the collection instance will be manipulated, and you should consider using immutable collections instead. This is something Guava provides in the Ordering class and is a simple one-liner:

    List<string> sorted = Ordering.natural().sortedCopy(strings);
    

    3. Wrap your list with java.util.PriorityQueue

    Though there is no sorted list in Java there is however a sorted queue which would probably work just as well for you. It is the java.util.PriorityQueue class.

    Nico Haase linked in the comments to a related question that also answers this.

    In a sorted collection you most likely don’t want to manipulate the internal data structure which is why PriorityQueue doesn’t implement the List interface (because that would give you direct access to its elements).

    Caveat on the PriorityQueue iterator

    The PriorityQueue class implements the Iterable<E> and Collection<E> interfaces so it can be iterated as usual. However, the iterator is not guaranteed to return elements in the sorted order. Instead (as Alderath points out in the comments) you need to poll() the queue until empty.

    Note that you can convert a list to a priority queue via the constructor that takes any collection:

    List<String> strings = new ArrayList<String>()
    strings.add("lol");
    strings.add("cat");
    
    PriorityQueue<String> sortedStrings = new PriorityQueue(strings);
    while(!sortedStrings.isEmpty()) {
        System.out.println(sortedStrings.poll());
    }
    // Prints out "cat" and "lol"
    

    4. Write your own SortedList class

    NOTE: You shouldn’t have to do this.

    You can write your own List class that sorts each time you add a new element. This can get rather computation heavy depending on your implementation and is pointless, unless you want to do it as an exercise, because of two main reasons:

    1. It breaks the contract that List<E> interface has because the add methods should ensure that the element will reside in the index that the user specifies.
    2. Why reinvent the wheel? You should be using the TreeSet or Multisets instead as pointed out in the first point above.

    However, if you want to do it as an exercise here is a code sample to get you started, it uses the AbstractList abstract class:

    public class SortedList<E> extends AbstractList<E> {
    
        private ArrayList<E> internalList = new ArrayList<E>();
    
        // Note that add(E e) in AbstractList is calling this one
        @Override 
        public void add(int position, E e) {
            internalList.add(e);
            Collections.sort(internalList, null);
        }
    
        @Override
        public E get(int i) {
            return internalList.get(i);
        }
    
        @Override
        public int size() {
            return internalList.size();
        }
    
    }
    

    Note that if you haven’t overridden the methods you need, then the default implementations from AbstractList will throw UnsupportedOperationExceptions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using gwt framework called smartgwt (however, problem concerns gwt and java) There, you
For example, in Java there is Functional Java and Higher-Order Java . Both essentially
In Java is there a way to check the condition: Does this single character
In Java is there a way to extract the value of an unknown subset
Is there any way to see the native code generated by CLR? In java
I was wondering if in Java there was any way to give a new
I am very curious that whether in Java there is any way to release
In java is there any elegant way to compare two different StringBuilders and copy
In java is there a faster way of doing this? if (keyCode != 66
In Java is there a way to find out if first character of a

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.