Java
ArrayList
PriorityQueue
Sorting
Data Structures

ArrayList.sort vs PriorityQueue

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

ArrayList.sort and PriorityQueue both organize values, but they solve different problems. ArrayList.sort gives you a fully ordered list all at once. PriorityQueue gives you efficient repeated access to the smallest or highest-priority element without keeping the entire collection in globally sorted order.

Use ArrayList.sort When You Need Full Order

If your goal is to sort a collection and then iterate through it in order, ArrayList.sort is the right tool.

java
1import java.util.ArrayList;
2import java.util.Comparator;
3
4public class Main {
5    public static void main(String[] args) {
6        ArrayList<Integer> values = new ArrayList<>();
7        values.add(5);
8        values.add(1);
9        values.add(3);
10
11        values.sort(Comparator.naturalOrder());
12        System.out.println(values);
13    }
14}

After sorting, every element is in its correct position relative to every other element.

That is useful for:

  • final display ordering
  • batch reporting
  • one-time sorting before sequential processing

Use PriorityQueue When You Need Repeated Best-Element Access

A priority queue is better when the main operation is repeatedly extracting the next smallest or highest-priority value.

java
1import java.util.PriorityQueue;
2
3public class Main {
4    public static void main(String[] args) {
5        PriorityQueue<Integer> pq = new PriorityQueue<>();
6        pq.add(5);
7        pq.add(1);
8        pq.add(3);
9
10        while (!pq.isEmpty()) {
11            System.out.println(pq.poll());
12        }
13    }
14}

Internally, a priority queue is usually backed by a heap. That means peek and poll are efficient for the top-priority element, but the entire structure is not sorted the way a list is after sort.

The Key Mental Difference

Think of the two structures like this:

  • 'ArrayList.sort means "sort everything now"'
  • 'PriorityQueue means "always be able to get the next best item quickly"'

If you inspect the internal order of a priority queue directly, it may look unsorted. That is not a bug. The heap only guarantees that the root has the correct priority relative to the rest.

Performance Tradeoff

Typical behavior:

  • sorting a list of n elements is O(n log n)
  • inserting into a priority queue is usually O(log n)
  • polling the top element is usually O(log n)
  • peeking the top element is usually O(1)

So if you insert many items and only need the final full sorted order once, sorting a list is often simpler. If items arrive over time and you repeatedly need the current minimum or maximum, a priority queue is usually a better fit.

Example of the Wrong Tool

If you keep adding items to a list and sorting the whole list after every insertion, you are often simulating a weaker version of a priority queue.

On the other hand, if you use a priority queue but then need random indexed access in sorted order, you probably wanted a sorted list or another data structure entirely.

Common Pitfalls

  • Assuming a priority queue keeps all elements in fully sorted iteration order.
  • Using ArrayList.sort repeatedly inside a loop when the real need is prioritized retrieval over time.
  • Choosing PriorityQueue when the final result must be a completely sorted list and no incremental priority operations are needed.
  • Inspecting the internal heap representation and mistaking it for incorrect behavior.
  • Ignoring comparator design, which affects both structures.

Summary

  • 'ArrayList.sort is for producing a fully sorted list.'
  • 'PriorityQueue is for efficient access to the next highest-priority element.'
  • A priority queue is heap-ordered, not globally sorted for direct iteration.
  • Choose based on the main operation pattern, not only on whether both can eventually yield ordered values.
  • If the problem is "sort once", use a list. If the problem is "keep taking the next best item", use a priority queue.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.