Enhance your understanding of Java Generics by implementing and utilizing more complex generic structures, including multi-type generics, collections, wildcards, and practical caching.
This project demonstrates:
- Multi-type generic classes (
Pair<K, V>) - Generic data structures (
Stack<T>) - Generic methods and wildcards (
sort,printCollection,sumOfNumberList) - Complex generic implementations (
Cache<T>with bounded wildcards) - Custom class implementing
Comparable<T>(Student)
Generic2/
├─ src/
│ └─ main/
│ └─ java/
│ └─ genericsadvanced/
│ ├─ Pair.java
│ ├─ Stack.java
│ ├─ CollectionsUtil.java
│ ├─ Cache.java
│ ├─ Student.java
│ └─ Main.java
└─ README.md
Each file uses the package declaration:
package genericsadvanced;Open your terminal (PowerShell or CMD) in the project root:
javac -d out src\main\java\genericsadvanced\*.javajava -cp out genericsadvanced.MainIf using VS Code, you can press Run Main.java.
-
Stores a key-value pair of any types.
-
Methods:
getKey(),getValue(),setKey(),setValue(). -
Example:
Pair<String, Integer> personAge = new Pair<>("Age", 30); System.out.println(personAge); // Pair{Age = 30}
-
A generic stack that allows pushing and popping any type.
-
Methods:
push(),pop(),peek(),isEmpty(). -
Throws
NoSuchElementExceptionifpop()orpeek()is called on an empty stack. -
Example:
Stack<String> stack = new Stack<>(); stack.push("A"); stack.push("B"); System.out.println(stack.pop()); // B
-
Static generic method that sorts an
ArrayList<T>whereT extends Comparable<T>. -
Example:
ArrayList<Integer> list = new ArrayList<>(List.of(5, 3, 9, 1)); CollectionsUtil.sort(list); System.out.println(list); // [1, 3, 5, 9]
-
Accepts
Collection<?>and prints each item (demonstrates wildcard use). -
Example:
List<String> names = List.of("Alice", "Bob", "Carol"); CollectionsUtil.printCollection(names);
-
Accepts
List<? extends Number>and returns the sum as adouble. -
Example:
List<Number> nums = List.of(1, 2.5, 3L); double sum = CollectionsUtil.sumOfNumberList(nums); // 6.5
-
Stores instances of type
Twith retrieval by key. -
Uses a key extractor (
Function<T, String>) passed into the constructor. -
Methods:
add(T item)get(String key)clear()addAll(Cache<? extends T> other)— adds compatible cache entries using bounded wildcards.
-
Example:
Cache<Student> cache = new Cache<>(s -> s.lastName() + ":" + s.id()); cache.add(new Student("Alice", "Brown", 1)); System.out.println(cache.get("Brown:1"));
Implements Comparable<Student> for sorting by last name, first name, and ID.
| # | Feature | Expected Result |
|---|---|---|
| 1 | Pair<K, V> |
Prints key-value and allows updating value |
| 2 | Stack<T> push/pop |
Returns items in correct order |
| 3 | sort() utility |
Correctly sorts integers, strings, and custom objects |
| # | Feature | Expected Result |
|---|---|---|
| 1 | Pop empty stack | Throws and catches NoSuchElementException |
| 2 | sumOfNumberList() with mixed types |
Correctly sums integers, floats, doubles, longs |
| 3 | Cache<T>.clear() |
Removes all items; size() returns 0 |
=== Part 1: Pair<K,V> ===
Pair{age = 36}
Updated: Pair{age = 37}
=== Part 1: Stack<T> ===
Stack: [A, B, C]
peek: C
pop: C
after pop: [A, B]
Caught expected underflow: Stack underflow: cannot pop from empty stack
=== Part 2: Sort Utility ===
Sorted ints: [1, 2, 5, 9]
Sorted strings: [Alpha, bravo, charlie, delta]
Sorted students: [Student{Anderson, Bob #2}, Student{Anderson, Cara #5}, Student{Brown, Alice #3}]
=== Part 2: Wildcards ===
printCollection(students):
Student{Anderson, Bob #2}
Student{Anderson, Cara #5}
Student{Brown, Alice #3}
sumOfNumberList: 11.25
=== Part 3: Cache<T> ===
studentCache: [Student{Clark, Dana #10}, Student{Brown, Evan #11}]
after addAll: [Student{Clark, Dana #10}, Student{Brown, Evan #11}, Student{Anderson, Fiona #12}, Student{Brown, Gina #13}]
containsKey(Brown:Evan#11): true
get(Brown:Evan#11): Student{Brown, Evan #11}
after clear, size=0
You can earn up to 5% extra credit by:
- Implementing a text-based CLI menu to interact with Stack and Cache.
- Building a simple GUI (Swing/JavaFX) to manage the cache visually.
- Writing a unit test suite (JUnit) covering all classes.
Include a short PDF describing your enhancements and how they improve the assignment.
Elida Ribeiro North Seattle College — Application Development BAS 📅 Due Date: November 3, 2025