By Alvin Alexander. Last updated: March 1, 2020
As a quick note, if you need some examples of the syntax of how to write a Java method that returns a generic type, I hope these are helpful:
public static <T> T getRandomValue(List<T> listOfPossibleOutcomes, int numPossibilities) { int r = RandomNumberGenerator.getRandIntBetween(0, numPossibilities); return listOfPossibleOutcomes.get(r); } public static <T> T getRandomValueFromGenericList(List<T> list) { Collections.shuffle(list); return list.get(0); }
I hadn’t written a Java generic method in a while, and I forgot you need to declare the generic type (<T>
) early in the method declaration.