JAVA FOUNDATIONS · JAVA 17
Java List, Set and Map: choose the right collection
Compare repeated values, uniqueness and key-based lookup with deterministic output.
Forsk Coding School learning resources · Updated 2026-10-26
What you will learn
- Choose a collection from a data requirement
- Predict duplicate handling
- Avoid depending on unspecified iteration order
Before you start: Objects, generic type notation such as List<String>, and a basic loop.
Ask what relationship the data represents
A List represents an ordered sequence and can contain repeated elements. A Set represents membership without duplicate elements. A Map associates keys with values and is not itself a subtype of Collection. These are different requirements: a list of submitted answers may preserve repeats, a set of selected course names may remove them, and a map may connect a learner ID to a name.
The sample begins with the names Asha, Aman and Asha. ArrayList preserves all three entries in insertion order. LinkedHashSet preserves the order in which distinct names were first inserted while eliminating the duplicate. It was chosen deliberately so the displayed output is reproducible. HashSet does not provide that insertion-order guarantee.
Understand what uniqueness means
For these String elements, equality is based on their character content through equals. Two references to equal text therefore describe one set member. With your own object type, the equality and hashCode contract matters when using hash-based collections. Distinct objects are not automatically considered equal just because some fields look similar to a reader.
A Map has at most one value associated with an equal key. Calling put again with the same key replaces its associated value. That is not the same as appending another row to a List. In this example ID 101 is updated from Asha to Asha Singh, so looking up 101 returns the updated name. Use containsKey when you must distinguish a missing key from a key mapped to null.
Choose an implementation after choosing a contract
Start with the required behaviour, then choose a suitable implementation. Do not assume that every List is resizable: Arrays.asList returns a fixed-size list, and List.of returns an unmodifiable list. Wrapping values in a new ArrayList creates a mutable list for these exercises. Mutation support is an implementation detail worth checking explicitly.
Tests should reflect the guarantees you rely on. Assert list positions when order matters, membership for a set when order does not matter, and values returned by known keys for a map. If a user interface must show a stable order, select an implementation or sorting step that provides it rather than hoping a hash-based iteration happens to look consistent.
Run the worked example
Save as Main.java. Run javac Main.java, then java Main.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> names = new ArrayList<>(Arrays.asList("Asha", "Aman", "Asha"));
Set<String> unique = new LinkedHashSet<>(names);
Map<Integer, String> learners = new LinkedHashMap<>();
learners.put(101, "Asha");
learners.put(101, "Asha Singh");
System.out.println(names);
System.out.println(unique);
System.out.println(learners.get(101));
}
}Expected result
[Asha, Aman, Asha]
[Asha, Aman]
Asha Singh- The List retains the repeated name and all three positions.
- LinkedHashSet retains two distinct names in insertion order.
- The second put replaces the value for the existing key 101.
Common mistakes to check
- A Set removes duplicates according to its equality rules, not by visual similarity.
- Do not assume a HashSet will print in insertion order.
- Use a mutable implementation before calling add or remove.
Try it yourself
Remove duplicates from the integer sequence 4, 2, 4, 3, 2 while preserving the order of first appearance. Print the result.
Give me a hint
Construct a LinkedHashSet from a list of the five values.
Compare with one working solution
import java.util.*;
public class Main {
public static void main(String[] args) {
Set<Integer> unique = new LinkedHashSet<>(Arrays.asList(4, 2, 4, 3, 2));
System.out.println(unique);
}
}Expected result
[4, 2, 3]Quick check
Choose an answer, then check the explanation. Your answer stays in this page and is not saved as a student record.
Keep learning
Explain the result in your own words, change one input and predict the new output. For additional structured assignments, explore the workbook sample before requesting the full pack.
Explore the practice workbookReference documentation
Use the official reference to check language details. The explanations, examples and exercises on this page are written for this learning resource.
