To compare generic types in Java, use the
compareTo
method.Make sure your generic type is defined as T extends Comparable
, then you can use compareTo
to compare objects of type T
:
public class ComparingClass<T extends Comparable<T>> {
public boolean compareTwoValues(T value1, T value2) {
return value1.compareTo(value2) < 0;
}
}