Дженерики Java: compareTo и “capture#1-of?”

Если это - стиль, Вы хотите использовать в нескольких местах на странице, использовать класс. Если Вы хотите большую настройку для отдельного объекта, говорите военно-морскую панель относительно стороны страницы, то идентификатор является лучшим, потому что Вам вряд ли будет нужна та комбинация стилей где-либо еще.

13
задан Nick Heiner 20 November 2009 в 14:57
поделиться

1 ответ

List means "List of anything", so two objects with this type are not the same: One could be a list of String, the other a list of BigDecimal. Obviously, those are not the same.

List means "List of anything but when you see T again, it's the same T".

You must tell the compiler when you mean the same type in different places. Try:

public static <T extends Comparable<? super T>> List<T> merge(Set<List<T>> lists) {
    List<T> result = new LinkedList<T>();
    HashBiMap<List<T>, Integer> location = HashBiMap.create();

[EDIT] So what does > List mean? The first part defines a type T with the following properties: It must implement the interface Comparable (or Comparable where X is also defined in terms of T).

? super T means that the type which the Comparable supports must T or one of its super types.

Imagine for a moment this inheritance: Double extends Integer extends Number. This is not correct in Java but imagine that Double is just an Integer plus a fraction part. In this scenario, a Comparable which works for Number also works for Integer and Double since both derive from Number. So Comparable would satisfy the super part for T being Number, Integer or Double.

As long as each of these types support the Comparable interface, they also satisfy the first part of the declaration. This means, you can pass in Number for T and the resulting code will also work when there are Integer and Double instances in the lists. If you Integer for T, you can still use Double but Number is not possible because it doesn't satisfy T extends Comparable anymore (the super part would still work, though).

The next step is to understand that the expression between static and List just declares the properties of the type T which is used later in the code. This way, you don't have to repeat this long declaration over and over again. It's part of the behavior of the method (like public) and not part of the actual code.

21
ответ дан 1 December 2019 в 22:39
поделиться
Другие вопросы по тегам:

Похожие вопросы: