Difference between an ordered and a sorted collection in Java? Example Tutorial

Hello guys, if you are working in Java collection framework then you may have heard about ordered as well as sorted collection classes. One of the related question to this concept is what is difference between ordered and sorted collection in Java, which is often asked to junior developers. In the past, I have shared 25 Java Collection interview questions as well as 130+ Java questions and in this article, I will answer this popular question. An ordered collection maintains the order of elements e.g. ArrayList maintains the insertion order of elements. Similarly LinkedHashMap can keep element in the order they are inserted or accessed. 

On the other hand a sorted collection keeps elements in the sorting order defined by their Comparable implementation i.e. the compareTo() method or the specific Comparator they passed while creating instance of sorted collection e.g. TreeMap. You can pass a Comparator while creating instance of TreeMap to keep the elements in the order you want. 

Alternatively, TreeMap will store elements in the sorting order defined by the compareTo() method of Key object. Remember, for sorted collection its important for key objects to implement Comparable if you are not providing alternative Comparator instance. 

Example of Ordered and Sorted Collection in Java

Let's see an example to illustrate the difference between an ordered and a sorted collection in Java:

import java.util.*;

public class OrderedVsSortedExample {
    public static void main(String[] args) {
        // Example of an ordered collection (maintains insertion order)
        List<String> orderedList = new LinkedList<>();

        System.out.println("Ordered List:");
        for (String fruit : orderedList) {

        // Example of a sorted collection (arranges elements in natural order)
        SortedSet<Integer> sortedSet = new TreeSet<>();

        System.out.println("\nSorted Set:");
        for (int num : sortedSet) {


Difference between an ordered and a sorted collection in Java?

In this example, we have an ordered collection represented by a LinkedList of strings. When we add elements ("Apple," "Banana," and "Orange"), they retain their insertion order, and when we iterate through the list, they are displayed in the same sequence they were added.

On the other hand, we have a sorted collection represented by a TreeSet of integers. As we add elements (20, 10, and 30), the TreeSet automatically arranges them in their natural order (ascending order for integers). When we iterate through the TreeSet, the elements are displayed in sorted order.

By running this example, you can observe how the two types of collections behave differently regarding the arrangement and retrieval of elements.

In short:

1) ordered collection maintains order but sorted collection impose sorting which means they re-arrange elements.

2) One example of ordered collection is ArrayList and LinkedHashMap, while TreeMap is an example of sorted collection

3) The object must implement Comparable if you want to store them in TreeSet or TreeMap without alternative Comparator. Since, Collection class has to find a proper place for new elements as per the sorting order. 

That's all about difference between ordered and sorted collection in Java. In conclusion, understanding the difference between an ordered and a sorted collection is crucial for Java developers to design efficient and effective data structures for their applications.

In Java, an ordered collection maintains the elements in a specific sequence, which could be the insertion order or another defined order. This preservation of insertion order makes it easy to iterate through the elements in the same order they were added. 

The primary data structures that provide an ordered collection are List and Queue interfaces, where List implementations like ArrayList and LinkedList maintain insertion order, and Queue implementations like LinkedList maintain the order based on elements' arrival time.

On the other hand, a sorted collection arranges elements in a specific order, typically based on natural ordering (Comparable interface) or a custom comparator (Comparator interface). The main data structure that provides a sorted collection is the SortedSet interface, with TreeSet being a commonly used implementation. Sorted collections offer efficient retrieval of elements in a specific order, making them suitable for scenarios where sorting is frequently required.

It is essential to consider the requirements of a particular use case while choosing between an ordered and a sorted collection. If maintaining the insertion order is important, an ordered collection like ArrayList or LinkedList is a suitable choice. Conversely, when the focus is on efficient sorting and retrieval based on a specific order, using a sorted collection like TreeSet can significantly improve performance.

In summary, the key distinction lies in the way elements are organized and accessed in ordered and sorted collections in Java. Developers must carefully analyze their application's needs and performance requirements to make the appropriate choice between these two types of collections to achieve optimal results.

Other Java Collection tutorials you may like
  • The difference between HashMap and ConcurrentHashMap in Java? (answer)
  • What is the difference between HashSet and HashMap in Java? (answer)
  • Difference between HashMap and Hashtable in Java? (answer)
  • The difference between HashMap and LinkedHashMap in Java? (answer)
  • Difference between ConcurrentHashMap and HashMap in Java? (answer)
  • How HashSet internally works in Java? (answer)
  • The best way to iterate over HashMap in Java? (answer)
  • How ConcurrentHashMap internally works in Java? (answer)
  • What is the difference between ArrayList and HashMap in Java? (answer)
  • 3 ways to loop over a Map in Java? (example)
  • How to sort the HashMap on keys and values in Java? (solution)
  • What is the difference between ArrayList and HashMap? (difference)
  • How does get() method of HashMap work in Java? (answer)
  • How to convert Map to List in Java? (solution)

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.