Sub topic 2
What Encounter Order Means
Prerequisites
You should know the basic List, Set, Map, and Deque interfaces. You should also know the difference between an ordered collection, a sorted collection, and an unordered collection. The examples use Java 21 or newer.
Java 21's sequenced collection interfaces work with encounter order. Encounter order is the order in which a collection presents its elements when you iterate over it. For a list, this is usually the index order. For a linked hash set, it is the insertion order. A sorted set uses its comparator or natural ordering.
Encounter order does not always mean “the order in which the program first saw the values.” It means the collection's defined iteration order.
Lists and insertion-ordered sets
An ArrayList preserves the order of its elements by position. If you add "red", then "green", then "blue", iteration encounters those values in that order.
A LinkedHashSet also has a defined encounter order. It normally preserves insertion order, while still preventing duplicate elements. Adding an element that is already present does not create a second copy or move the existing element.
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
public class EncounterOrder {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("red");
list.add("green");
list.add("blue");
Set<String> set = new LinkedHashSet<>();
set.add("red");
set.add("green");
set.add("blue");
set.add("green");
System.out.println(list); // [red, green, blue]
System.out.println(set); // [red, green, blue]
}
}The Set interface does not promise an encounter order for every implementation. HashSet, for example, does not promise insertion order. Its output can vary between runs, Java versions, or changes to the set's capacity. Do not use the observed output of a HashSet as a contract.
Sorted order is also encounter order
A TreeSet has an encounter order, but that order comes from sorting rather than insertion. If its elements use natural ordering, iteration visits the smallest element first.
This difference matters when using first and last operations. For a LinkedHashSet, the first element is the earliest element in insertion order. For a TreeSet, the first element is the lowest element according to its ordering.
| Collection | Defined encounter order | Meaning of first element |
|---|---|---|
| `ArrayList` | Element position | Element at index `0` |
| `LinkedHashSet` | Insertion order | Earliest element in the set's order |
| `TreeSet` | Comparator or natural order | Lowest element in sorted order |
| `HashSet` | None promised | No reliable meaning |
A collection can therefore be ordered without being sorted. LinkedHashSet is ordered, but it does not arrange values alphabetically or numerically. A sorted collection is ordered by a comparison rule.
Why Java 21 uses this concept
Before Java 21, collection classes exposed similar ideas through different methods. A list used get(0) and get(size() - 1). A deque used getFirst() and getLast(). A linked hash set had no common first-element API through the general Set type.
The SequencedCollection interface provides a shared vocabulary for collections that have a defined encounter order. Later, SequencedSet and SequencedMap extend this idea to sets and maps. These interfaces let a method describe its requirement directly: it needs a collection with a meaningful first element, last element, and reverse order.
Exercise
A program adds 8, 3, and 5 to both a LinkedHashSet and a TreeSet. What is the encounter order of each collection?
Show answer
The LinkedHashSet encounters 8, 3, 5 because it preserves insertion order. The TreeSet encounters 3, 5, 8 because it uses natural numeric ordering.