Sorted collection vs ordered collection

Sorted collection vs ordered collection

Sorted Collection

A sorted collection is one that maintains its elements in a specific order determined by a sorting algorithm. The sorting order can be natural (defined by the Comparable interface) or custom (defined by a Comparator). The key point is that the elements are always arranged according to some sorting criteria.

Examples of sorted collections in Java:

  • TreeSet
  • TreeMap

Sorted collection vs ordered collection

Ordered Collection

An ordered collection, on the other hand, maintains the order in which elements are inserted. It does not necessarily sort the elements but retains the sequence of insertion. The order is predictable based on how elements are added to the collection.

Examples of ordered collections in Java:

  1. ArrayList
  2. LinkedList
  3. LinkedHashSet
  4. LinkedHashMap

Comparison

 Use Case :

  • Sorted Collection : Useful when you need elements to be in a specific order based on their value (e.g., alphabetically, numerically).
  • Ordered Collection : Useful when the insertion order of elements needs to be preserved (e.g., maintaining a history of added elements).

 Performance

  • Sorted Collection : Operations like insertion and lookup might be slower due to the need to maintain order according to the sorting criteria.
  • Ordered Collection : Generally faster for insertion and lookup as they do not involve sorting, only maintaining insertion order.

 Flexibility

  •   Sorted Collection : Offers flexibility in defining the order through custom comparators.
  •   Ordered Collection : Limited to the order in which elements are added.

Which One is Better Sorted collection vs ordered collection?

The choice between sorted and ordered collections depends on the specific requirements of your use case. If you need elements to be in a particular order based on their natural or custom order, a sorted collection is better. If maintaining the order of insertion is more important, an ordered collection is preferable.

Sorted Collection Example:
java
import java.util.Set;
import java.util.TreeSet;

public class SortedCollectionExample {
    public static void main(String[] args) {
        Set<String> sortedSet = new TreeSet<>();
        sortedSet.add("Banana");
        sortedSet.add("Apple");
        sortedSet.add("Mango");

        // Elements are sorted in natural order
        System.out.println("Sorted Set: " + sortedSet);
    }
}


Output:

Sorted Set: [Apple, Banana, Mango]


  Ordered Collection Example:

java
import java.util.LinkedList;
import java.util.List;

public class OrderedCollectionExample {
    public static void main(String[] args) {
        List<String> orderedList = new LinkedList<>();
        orderedList.add("Banana");
        orderedList.add("Apple");
        orderedList.add("Mango");

        // Elements are in insertion order
        System.out.println("Ordered List: " + orderedList);
    }
}

Output:

Ordered List: [Banana, Apple, Mango]

Homepage

Readmore