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?