Skip to the content.
Intro Lists Sets Queues and Deques Conclusion

Collectables Toolkit

AP CSA

Summary Diagram

Image

Image2

Java Collections Homework (due the Wednesday After Spring break)

Objectives

  • Practice using List, Set, and Deque
  • Understand when and why to use each
  • Apply key methods from each collection type
  • Practice iteration and conditional logic with collections

Part 1: Working with Lists

Task:
Create a method that takes a List and returns a new list containing only the **even numbers**, in the same order.

Requirements:

  • Use ArrayList
  • Use add, get, and size
  • Use a loop (not streams)

Part 2: Working with Sets

Task:
Create a method that takes two Set objects and returns a new Set with only the **common elements** (i.e. the intersection).

Requirements:

  • Use HashSet
  • Use contains, add, and iteration
  • Final result should have no duplicates

Part 3: Working with Deques

Task:
Simulate a line of customers using a Deque. Implement the following steps in order:

  1. Add 3 customers to the end
  2. Add 1 customer to the front (VIP)
  3. Remove the customer at the front
  4. Show the current front and back of the line
  5. Print the size of the line

Requirements:

  • Use ArrayDeque
  • Use addFirst, addLast, removeFirst, peekFirst, peekLast, and size

Challenge Question (Bonus +0.01)

Question:
You need to store a collection of student IDs where:

  • Order doesn’t matter
  • You must prevent duplicates
  • You often need to check if an ID exists

Which collection type would be most efficient to use and why?

Part 1

import java.util.ArrayList;
import java.util.List;

public static List<Integer> getEvenNumbers(List<Integer> numbers) {
    List<Integer> evenNumbers = new ArrayList<>();
    for (int i = 0; i < numbers.size(); i++) {
        if (numbers.get(i) % 2 == 0) {
            evenNumbers.add(numbers.get(i));
        }
    }
    return evenNumbers;
}

List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
numbers.add(4);
numbers.add(5);

List<Integer> evenNumbers = getEvenNumbers(numbers);
System.out.println("Even Numbers: " + evenNumbers);
Even Numbers: [2, 4]

Part 2

import java.util.HashSet;
import java.util.Set;

public static Set<String> getCommonElements(Set<String> set1, Set<String> set2) {
    Set<String> commonElements = new HashSet<>();
    for (String element : set1) {
        if (set2.contains(element)) {
            commonElements.add(element);
        }
    }
    return commonElements;
}
Set<String> set1 = new HashSet<>();
set1.add("apple");
set1.add("banana");
set1.add("cherry");

Set<String> set2 = new HashSet<>();
set2.add("banana");
set2.add("cherry");
set2.add("date");

Set<String> commonElements = getCommonElements(set1, set2);
System.out.println("Common Elements: " + commonElements);
Common Elements: [banana, cherry]

Part 3

import java.util.ArrayDeque;
import java.util.Deque;

Deque<String> customerLine = new ArrayDeque<>();

// Add 3 customers to the **end**
customerLine.addLast("Customer 1");
customerLine.addLast("Customer 2");
customerLine.addLast("Customer 3");

// Add 1 customer to the **front** (VIP)
customerLine.addFirst("VIP Customer");

// Remove the customer at the **front**
customerLine.removeFirst();

// Show the current front and back of the line
System.out.println("Front of the line: " + customerLine.peekFirst());
System.out.println("Back of the line: " + customerLine.peekLast());

// Print the size of the line
System.out.println("Size of the line: " + customerLine.size());
Front of the line: Customer 1
Back of the line: Customer 3
Size of the line: 3

Challenge Question (Bonus +0.01)

The collection type that is most efficient to use in this case is a HashSet.

  • A HashSet does not maintain a specific order, which satisfies the “Order doesn’t matter” requirement.
  • It automatically prevents duplicates, as it does not allow duplicate elements, unlike ArrayLists and LinkedLists
  • It provides constant-time performance for basic operations like add, remove, and contains, making it efficient for checking if an ID exists. The operations are fast due to the quick hashing.