Worked example
public static <T extends Comparable<T>> int countGreater(T[] arr, T threshold) {
int count = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i].compareTo(threshold) > 0) { // strictly greater
count++;
}
}
return count;
}
Integer[] nums = {3, 9, 2, 7};
System.out.println(countGreater(nums, 5)); // 2 — the 9 and the 7
String[] words = {"pear", "apple", "fig"};
System.out.println(countGreater(words, "banana")); // 2 — pear and fig
The very same method counts numbers above 5 and words after "banana" alphabetically.