简体   繁体   English

可比是原始类型。 对通用类型Comparable的引用 <T> 应该参数化

[英]Comparable is a raw type. References to generic type Comparable<T> should be parameterized

I am setting my my variable like 我将我的变量设置为

Map<String, Function<CLASS_NAME, Comparable>> map = new HashMap<>();

where Comparable is giving the warning message as 其中Comparable发出警告消息为

Comparable is a raw type. References to generic type Comparable<T> should be parameterized

I am using it like 我正在使用它

map.put(VARIABLE_NAME1, s -> s.getStringProperty());
map.put(VARIABLE_NAME2, s -> s.getIntProperty());
..

I am using for compare like 我用于比较喜欢

Comparator<CLASS_TYPE> v = Comparator.comparing(map.get(VARIABLE_NAME), Comparator.nullsFirst(Comparator.naturalOrder()));

What type of Generic should be used to avoid the warning? 应该使用哪种类型的泛型来避免警告?

Comparable is obviously a generic type. 可比显然是通用类型。

So all you need is just: 因此,您只需要:

Map<String, Function<CLASS_NAME, Comparable<CLASSNAME>>> map = new HashMap<>();

instead of 代替

Map<String, Function<CLASS_NAME, Comparable>> map = new HashMap<>();

or you want to compare another type..? 或者您想比较另一种类型..?

Map<String, Function<CLASS_NAME, Comparable<SomeOtherClass>>> map = new HashMap<>();

There are several things wrong with your current scheme. 您当前的方案有几处错误。

  1. Comparator and Comparable are two different approaches to comparing objects. ComparatorComparable是比较对象的两种不同方法。 You are confusing the two. 您将两者混淆了。
  2. You are trying to store a function that does comparisons into the map. 您正在尝试将一个比较功能存储到地图中。 Later, you fetch the value from the map and try to compare it (the function) to a Comparator . 稍后,您从映射中获取值,然后尝试将其(函数)与Comparator This won't work, as you can't compare a function to anything except another function. 这是行不通的,因为您无法将一个功能与另一个功能进行比较。
  3. You don't actually store a value anywhere; 实际上,您不会在任何地方存储值。 you are only storing a function. 您只存储一个函数。
  4. In your example, you store two different values to the same VARIABLE_NAME . 在您的示例中,您将两个不同的值存储到同一VARIABLE_NAME Is that intentional? 那是故意的吗?

If you want to create a property map, then you need to create a storable object that can be stored into the map, and can compare its value to a provided value. 如果要创建属性映射,则需要创建一个可存储的对象,该对象可以存储到该映射中,并且可以将其值与提供的值进行比较。 For instance, 例如,

class Storable<T extends Comparable<T>> {
  private final T value;
  Storable(T value) {
    this.value = value;
  }
  int compareTo(Object other) {
    if ( value.getClass().equals(other.getClass()) ) {
      return value.compareTo( (T) other );
    }
    return -1;
  }
}

Now create appropriate sub-classes: 现在创建适当的子类:

class StorableInt extends Storable<Integer> {
    StorableInt(Integer t) {
        super(t);
    }
}
class StorableString extends Storable<String> {
    StorableString(String s) {
        super(s);
    }
}

Your property map now looks like: 您的财产地图现在看起来像:

Map<String, Storable<?>>map = new HashMap<>();

map.put(variableName1, new StorableInt(13));
map.put(variableName2, new StorableString("string2"));

<T extends Comparable<T>> int compare( String key, T val ) {
  return map.get( key ).compareTo( val );
}

You can now store properties into your map and compare values against those properties. 现在,您可以将属性存储到地图中,然后将这些属性与值进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Type Class is a raw type. References to generic type Class<T> should be parameterized 原始类型。 对泛型类型的引用应该被参数化 - Raw type. References to generic types should be parameterized 警告-类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Warning - Class is a raw type. References to generic type Class<T> should be parameterized 类是原始类型。 对泛型类型Class的引用 <T> 应该参数化 - Class is a raw type. References to generic type Class<T> should be parameterized 为什么我收到警告:Class是原始类型。对泛型类型<T>的引用应该参数化“? - Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"? 如何在Eclipse中禁用警告 - 'Class是原始类型。对泛型类型<T>的引用应该参数化' - How to disable warning in Eclipse - 'Class is a raw type. References to generic type Class<T> should be parameterized' Eclipse警告 - 类是原始类型。 对泛型类的引用 <T> 应该参数化 - Eclipse warning - Class is a raw type. References to generic type Class<T> should be parameterized AdapterView是原始类型。 泛型类型AdapterView的引用 <T> 应该参数化 - AdapterView is a raw type. References to generic type AdapterView<T> should be parameterized AddLinkEntry类是原始类型。 对泛型类型AddLinkEntry类的引用 <T> 应该参数化 - AddLinkEntry Class is a raw type. References to generic type AddLinkEntry Class<T> should be parameterized JComboBox是原始类型。应参数化对泛型类型JComboBox <E>的引用 - JComboBox is a raw type. References to generic type JComboBox<E> should be parameterized
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM