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?

import java.util.*;

public class CollectablesHW {
    public static ArrayList<Integer> question1(ArrayList<Integer> list) {
        ArrayList<Integer> list2 = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            if (list.get(i) % 2 != 0) {
                list2.add(list.get(i));
            }
        }

        return list2;
    }

    public static Set<Integer> question2(Set<Integer> set1, Set<Integer> set2) {
        Set<Integer> set3 = new HashSet<>();
        for (Integer i : set1) {
            if (set2.contains(i)) {
                set3.add(i);
            }
        }
        return set3;
    }

    public static void question3() {
        Deque<String> line = new ArrayDeque<>();
        line.addLast("Nitin");
        line.addLast("Srini");
        line.addLast("Skibidi");

        line.addFirst("VIP Tarun Sigma");
        line.removeFirst();

        System.out.println(line.peekFirst());
        System.out.println(line.peekLast());
        System.out.println(line.size());
    }

    public static void bonusQuestion() {
        /*
         * A set would be the best data structure to use because sets basically just fit all the requirements perfectly: since we hash the elements, we don't store duplicates, and also checking if an element is in the set is O(1) so it's super fast. Sets unfortunately don't have an order, but we dont need one so this works out well.
         */
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
        System.out.println(question1(list));

        Set<Integer> set1 = new HashSet<>(Arrays.asList(1, 2, 3, 4));
        Set<Integer> set2 = new HashSet<>(Arrays.asList(3, 4, 5, 6));
        System.out.println(question2(set1, set2));

        question3();
    }
}

CollectablesHW.main(null);
[1, 3, 5]
[3, 4]
Nitin
Skibidi
3