Java Syntax for a list of comparable objects

2019-04-29 03:17发布

问题:

I'm writing a method that takes as its only parameter a list of comparable objects and doesn't return anything. I am uncertain as to the syntax that it should have:

public static void methodName(List<Comparable<Object>> list) {
    // Do some stuff
}

I think this is wrong because of the <Object> as a type for Comparable, which would mean the list can take an Integer and a Boolean as objects, but I don't want that. I want the list to take only one type, but that type has to implement the Comparable interface. How do I achieve that?

回答1:

Maybe make it generic?

public static <E extends Comparable<E>> void methodName(List<E> list) ...