Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Fix the driver behind crashes, sound loss and screen glitches3Repair Windows errors before they cause bigger problemsiTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
Use a List when sequence and position matter, a Set when elements must be unique, and a Map when you need to look up values by keys. The interface sets the basic contract; the implementation determines details such as iteration order. Map is a separate branch of the Java Collections Framework, not a subtype of Collection.
How List, Set, and Map differ
| Type | What it represents | Duplicates | Order and access |
|---|---|---|---|
List |
An ordered sequence of elements | Usually allowed | Elements can be accessed by integer index, and insertion position is under the caller’s control. |
Set |
A collection of unique elements | Not allowed | Iteration order depends on the implementation; not every set is unordered. |
Map |
Associations between keys and values | Keys are unique; values may repeat | Values are retrieved by key. Encounter order, if defined, depends on the implementation. |
In the interface hierarchy, List and Set specialize Collection. Map has its own interface hierarchy and provides views of its keys, values, and key-value entries. [Oracle Java SE 21 collections framework overview]
When to use a List
Choose a List for a sequence where position matters, where the same value can appear more than once, or both. A list lets your code control where elements are inserted and address elements by index. [Oracle Java SE 26 List API]
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute- Represent steps in a procedure or items in display order.
- Keep ranked results so each item’s position conveys priority.
- Store repeated values when each occurrence is meaningful.
ArrayList is the framework’s general-purpose resizable list implementation. Use it as a starting point for an ordinary list unless your needs call for a different implementation. The right choice still depends on the operations and workload; the framework overview does not establish a universally fastest collection.
When to use a Set
Choose a Set when each element should occur at most once, such as a collection of unique names or IDs, or when you want to eliminate duplicates. A set’s ordering is not determined by the interface alone:
HashSetdoes not guarantee iteration order. Do not rely on the sequence you happen to observe.LinkedHashSetpreserves insertion order during iteration.TreeSetorders elements by value.
Use the implementation whose ordering contract matches what the rest of your program needs. For example, if you remove duplicates but want to keep the original insertion sequence, choose LinkedHashSet rather than relying on a HashSet‘s observed order. [Oracle Java SE 21 collections framework overview]
Rank #2
When to use a Map
Choose a Map when a key should identify or retrieve an associated value: for example, an ID-to-record lookup, a word-to-definition mapping, or a grouping of items by key. A map cannot contain duplicate keys, and each key maps to at most one value; different keys can still have equal values. A map exposes its keys, values, and entries as views. [Oracle Java SE 26 Map API]
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →As with sets, iteration order depends on the implementation. The Java SE 21 framework overview identifies HashMap as a general-purpose map, TreeMap as sorted by key, and LinkedHashMap as insertion ordered. Choose based on whether you need no particular order, sorted keys, or insertion order; do not assume every map iterates alike. [Oracle Java SE 21 collections framework overview]
Keep Map keys stable
A map relies on key equality and hashing to find an association. Oracle’s Java SE 26 Map API warns that behavior is unspecified if a key changes in a way that affects equality while it is in the map. Prefer stable key fields, and do not mutate equality-relevant state while an object is being used as a key. [Oracle Java SE 26 Map API]
How to choose an implementation
Start with the operation your code needs, then check duplicates, access pattern, and ordering requirements. Use the interface to express the role of the data and an implementation to supply the needed behavior.
Rank #4
- Does position matter? Choose a
Listif you need an ordered sequence, indexed access, or meaningful repeated values. - Must every element be unique? Choose a
Set; selectLinkedHashSetfor insertion order orTreeSetfor sorted values. - Do you need to retrieve a value from a key? Choose a
Map; selectLinkedHashMapfor insertion order orTreeMapfor sorted keys. - Is no special encounter order required? Consider the general-purpose
ArrayList,HashSet, orHashMapfor the corresponding role, without relying on hash-based iteration order or assuming a universal performance winner.
What changed with sequenced collections in JDK 21
JDK 21 introduced SequencedCollection, SequencedSet, and SequencedMap to represent collection types with a defined encounter order and provide common ways to access or process elements from either end. This addition makes encounter order more explicit in the type hierarchy; it does not mean every set or map promises an order. Check the API documentation for the Java release you target when using version-specific interfaces. [Oracle Java SE 21 collections framework overview]

