Collections
On this page 8 topics
Overview
Topics
- List
- Set
- Map
- Queue
- ArrayList
- LinkedList
- HashSet
- HashMap
- TreeMap
Learn first
99 min of reading · 5 h of practice (estimate)
Learning outcomes
- Use
List,Set,Map, andQueueand choose the right one for a task. - Choose between
ArrayList/LinkedList,HashSet/TreeSet,HashMap/TreeMapbased on ordering and access needs. - Iterate safely, including removing elements during iteration.
- Declare variables by interface type (
List<String> x = new ArrayList<>()).
Lessons
- 01Beyond arrays: the Collections Framework11 min read
- 02Lists12 min read
- 03Sets10 min read
- 04Maps12 min read
- 05Queues and deques10 min read
- 06Choosing a collection: decision chart11 min read
- 07Iteration and ConcurrentModificationException11 min read
- 08Demo: word frequency with Map10 min read
- 09Demo: the wrong collection, measured12 min read
Lab and exercises
Lab
BEG-030-LAB1 · Word frequency
Outcomes: 1–4 · Time: ~3 h Read a paragraph (a file or a text block). Count word frequencies with a Map, list the unique words alphabetically, and show the top 10 by count. Write one line explaining each collection you chose. Acceptance: correct counts; justified collection choices.
Exercises (assessment: six exercises plus a collection-choice quiz)
| ID | Outcome | Task |
|---|---|---|
| BEG-030-EX01 | 1 | Create a List<String> of five names; add, remove one by value, and print its size and the second element. |
| BEG-030-EX02 | 1 | Remove duplicates from a list of numbers using a Set; print the result in the original order. |
| BEG-030-EX03 | 2 | For each need, pick the class: sorted unique names; fast key lookup; insertion-ordered unique tags; frequent insert at the front. |
| BEG-030-EX04 | 1 | Use a Queue to simulate three customers being served in arrival order. |
| BEG-030-EX05 | 3 | Remove all negative numbers from a list, first showing the failing for-each version, then a safe fix. |
| BEG-030-EX06 | 4 | Rewrite ArrayList<String> x = new ArrayList<String>(); using interface type and diamond; explain the benefit. |
Show solutions
- EX01
List<String> l = new ArrayList<>(List.of("a","b","c","d","e")); l.add("f"); l.remove("c"); l.size(); l.get(1); - EX02
new ArrayList<>(new LinkedHashSet<>(numbers))keeps first-seen order. - EX03
TreeSet;HashMap;LinkedHashSet;ArrayDeque/LinkedList(useaddFirst). - EX04
Queue<String> q = new ArrayDeque<>(); q.offer(...)three times, thenwhile (!q.isEmpty()) System.out.println(q.poll());. - EX05 For-each with
removethrowsConcurrentModificationException; fix:list.removeIf(n -> n < 0);. - EX06
List<String> x = new ArrayList<>();The variable type is the interface, so the implementation can change in one place. - The collection-choice quiz is the module quiz; it also covers outcome 2.
Quiz
Outcomes: O1 List/Set/Map/Queue · O2 choosing implementations · O3 safe iteration · O4 interface types.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Six exercises BEG-030-EX01 to EX06 plus the lab. Pass: at least five exercises correct, quiz 8/10, and every collection in the lab justified.
Common mistakes
List where a Set or Map is needed (O(n) lookups); modifying a list while iterating.
Assessment
6 exercises plus a collection-choice quiz.
Career relevance
Used in every Java program; internals in BEG-040/041.