Skip to content
Ameen Abozaid
BEG-030

Collections

  • 9 Lessons
  • 99 min of reading
  • 5 h practice (estimate)
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

  1. Use List, Set, Map, and Queue and choose the right one for a task.
  2. Choose between ArrayList/LinkedList, HashSet/TreeSet, HashMap/TreeMap based on ordering and access needs.
  3. Iterate safely, including removing elements during iteration.
  4. Declare variables by interface type (List<String> x = new ArrayList<>()).

Lessons

  1. 01Beyond arrays: the Collections Framework11 min read
  2. 02Lists12 min read
  3. 03Sets10 min read
  4. 04Maps12 min read
  5. 05Queues and deques10 min read
  6. 06Choosing a collection: decision chart11 min read
  7. 07Iteration and ConcurrentModificationException11 min read
  8. 08Demo: word frequency with Map10 min read
  9. 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)

IDOutcomeTask
BEG-030-EX011Create a List<String> of five names; add, remove one by value, and print its size and the second element.
BEG-030-EX021Remove duplicates from a list of numbers using a Set; print the result in the original order.
BEG-030-EX032For each need, pick the class: sorted unique names; fast key lookup; insertion-ordered unique tags; frequent insert at the front.
BEG-030-EX041Use a Queue to simulate three customers being served in arrival order.
BEG-030-EX053Remove all negative numbers from a list, first showing the failing for-each version, then a safe fix.
BEG-030-EX064Rewrite 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 (use addFirst).
  • EX04 Queue<String> q = new ArrayDeque<>(); q.offer(...) three times, then while (!q.isEmpty()) System.out.println(q.poll());.
  • EX05 For-each with remove throws ConcurrentModificationException; 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.

  1. 1Which collection does not allow duplicate elements?O1
  2. 2Which stores key-value pairs?O1
  3. 3A first-in, first-out waiting line is a:O1
  4. 4You want unique names kept sorted. Use:O2
  5. 5Default general-purpose list:O2
  6. 6Fast lookup of an order by its id:O2
  7. 7Which map keeps keys in sorted order?O2
  8. 8Removing from a list inside a for-each usually causes:O3
  9. 9A simple safe way to remove matching elements:O3
  10. 10Which declaration is best style?O4

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.

Type to search levels, phases, modules and lessons.