Collectables
HW
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)
import java.util.ArrayList;
import java.util.List;
List<Integer> nums = new ArrayList<Integer>(); // make the arraylist
// add values with .add()
nums.add(1);
nums.add(406);
nums.add(23);
nums.add(-1);
// print list
System.out.println("Current list: " + nums);
// use .get()
System.out.println("First element: " + nums.get(0));
// use .size();
System.out.println("Arraylist size: " + nums.size());
nums.add(100000);
// loop:
List<Integer> evensOnly = new ArrayList<Integer>();
for (int i = 0; i < nums.size(); i++) {
// is even?
if (nums.get(i) % 2 == 0) {
evensOnly.add(nums.get(i));
}
}
System.out.println("Evens only: " + evensOnly);
Current list: [1, 406, 23, -1]
First element: 1
Arraylist size: 4
Evens only: [406, 100000]
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
import java.util.HashSet;
import java.util.Set;
public class SetExample {
public static void main(String[] args) {
// make two sets to compare
Set<String> myClasses = new HashSet<>();
Set<String> friendClasses = new HashSet<>();
// adding stuff:
myClasses.add("Calc");
myClasses.add("APEL");
myClasses.add("CSA");
myClasses.add("USH");
myClasses.add("Physics");
System.out.println("My classes: " + myClasses);
friendClasses.add("Stats");
friendClasses.add("APEL");
friendClasses.add("CSA");
friendClasses.add("APUSH");
friendClasses.add("Physics");
System.out.println("Friend classes: " + friendClasses);
// check for intersections, make new set
Set<String> commonClasses = new HashSet<>();
for (String myClass : myClasses) {
// loop through my own classes and check if my friend's contains that
if (friendClasses.contains(myClass)) {
commonClasses.add(myClass);
}
}
System.out.println("Common classes: " + commonClasses);
}
}
SetExample.main(null);
My classes: [CSA, APEL, Calc, USH, Physics]
Friend classes: [CSA, APUSH, Stats, APEL, Physics]
Common classes: [CSA, APEL, Physics]
Part 3: Working with Deques
Task: Simulate a line of customers using a Deque. Implement the following steps in order:
- Add 3 customers to the end
- Add 1 customer to the front (VIP)
- Remove the customer at the front
- Show the current front and back of the line
- Print the size of the line
Requirements:
- Use ArrayDeque
- Use addFirst, addLast, removeFirst, peekFirst, peekLast, and size
import java.util.ArrayDeque;
import java.util.Deque;
public class DequeExample {
public static void main(String[] args) {
Deque<String> line = new ArrayDeque<>(); // make customer line
// add 3 elements to tail
line.addLast("Eric");
line.addLast("Eric's uncle");
line.addLast("Eric's other uncle");
// add to head
line.addFirst("Eric's dog");
// remove customer at front
line.removeFirst();
// show the front and back of line
System.out.println("Front: " + line.peekFirst());
System.out.println("Back: " + line.peekLast());
// print line size
System.out.println("Size: " + line.size());
}
}
DequeExample.main(null);
Front: Eric
Back: Eric's other uncle
Size: 3
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?
The most efficient collection type for storing student IDs in this scenario is a set. Sets are designed to store unique elements, automatically preventing duplicates without additional logic. They are also optimized for fast membership checks, with hash-based implementations like HashSet offering an average time complexity of O(1) for operations like checking if an ID exists. Since the problem specifies that order doesn’t matter, sets are a perfect fit as they do not maintain any specific order.
Using other data structures like lists or arrays would require extra processing to enforce uniqueness and check for membership, making them less efficient. A set, on the other hand, directly fulfills all the requirements: it prevents duplicates, allows quick lookups, and avoids unnecessary overhead related to maintaining order. This makes it the ideal choice for this use case.