Insert '<'Generic'>' Title Here
Using Generics in Java
Generics allow types (classes and interfaces) to be parameters when creating classes, interfaces and methods. Generics can accept multiple types until we specify a type.
private final List<Item> items = new ArrayList();
Here, Item
is a stand in for anything that uses the item interface. You can’t specify a primative type in a generic: you must must us a reference type (ex Object, Integer, Interface, etc.)
List<E>; //E is a placeholder for inputs
Generics Demo
Predicate is a functional interface that takes a string and returns a boolean. In the code below, it’s checking to see if the variable ismyName
matches Rachel
.
Predicate<String> isMyName = s ->s.equals("Rachel");
Here is a diagram of the same code, broken down into smaller parts:
Functional Interface | Generic Type | Variable Name | ƛ parameter & expression | The string we are checking against |
---|---|---|---|---|
Predicate | <String> | isMyName= | s -> s.equals | ("Rachel"); |
In class, we created a generic functional interface Predicate
that took an integer and returned a boolean. In the line of code below, we can see Predicate
used to check if the variable isMyFavoriteNumber
is equal to “33”.
Predicate<Integer> isMyFavoriteNumber = s -> s == 33;
Here is a diagram of the same code, broken down into smaller parts:
Functional Interface | Generic Type | Variable Name | ƛ parameter | ƛ expression & the int we are checking against |
---|---|---|---|---|
Predicate | <Integer> | isMyFavoriteNumber = | s -> | s == 33; |
We implemented Predicate
in a class called IsMeaningOfLife
. Alex pointed out that we write the class many different ways and accomplish the same comparison (checking to see if an integer is equal to Alex’s favorite number, 33).
import java.util.function.Predicate; public class IsMeaningOfLife implements Predicate<Integer> {private int meaningOfLife = 42; @Overridepublic boolean test(Integer i) { return i == meaningOfLife; } |
import java.util.function.Predicate; public class IsMeaningOfLife implements Predicate<Integer> { public boolean test(Integer i) { return i == 42; } |
More Examples using Predicate
import java.util.function.Predicate;
public class GenericsDemo {
public static void main(String[] args) {
Predicate<Integer> isMyFavoriteNumber = s -> s == 33;
IsMeaningOfLife isLife = new IsMeaningOfLife();
System.out.println("Number 33 is fave: " + isMyFavoriteNumber.test(33)
+ " life: " + isLife.test(33));
System.out.println("Number 42 is fave: " + isMyFavoriteNumber.test(42)
+ " life: " + isLife.test(42));
System.out.println("Number 177 is fave: " + isMyFavoriteNumber.test(177)
+ " life: " + isLife.test(177));
}
}
The output from the above code was:
- Number 33 is fave: true life: false
- Number 42 is fave: false life: true
- Number 177 is fave: false life: false
Functional Interfaces are a simple type of Generic, but we can also create custom generics. In class, Alex created a generic class named Box
.
Box Class | Code using Box Class | Output |
public class Box <TYPE> { private TYPE item; public void add(TYPE item){ //this is a setter this.item = item; } public TYPE remove(){ TYPE myItem = item; item = null; return myItem; } public TYPE seeItem(){ //this is a getter return item; } } |
Box<String> stringBox = new Box<>(); stringBox.add("a present"); System.out.println("I open my box and see " + stringBox.seeItem() + "."); System.out.println("I take out " + stringBox.remove() + "."); System.out.println("So now my box contains " + stringBox.remove() + "."); |
I open my box and see a present. I take out a present. So now my box contains null. |
We also experiemented with using arrays to interact with Box
:
Box Class | Code using Box Class | Output |
public class Box <TYPE> { private TYPE item; public void add(TYPE item){ //this is a setter this.item = item; } public TYPE remove(){ TYPE myItem = item; item = null; return myItem; } public TYPE seeItem(){ //this is a getter return item; } }
|
List<String> strings = new ArrayList<>(Arrays.asList("My", "Strings", "Are", "Great")); Box <List<String>> stringyBox = new Box<>(); stringyBox.add(strings); System.out.println("My Stringy Box: " + stringyBox.seeItem()); stringyBox.seeItem().add("New String"); System.out.println("My Stringy Box again: " + stringyBox.seeItem()); |
My Stringy Box: [My, Strings, Are, Great] My Stringy Box again: [My, Strings, Are, Great, New String]
|
In our game, we could create a Box-type generic class that could hold items (like a treasure chest), adversaries (like a group of enemies), or a list of our commands. One of my Bootcamp goals for this weekend is to try out one of these suggestions in TAG.