gpt4 book ai didi

Java6、Guava、泛型、类型推断

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:10:40 26 4
gpt4 key购买 nike

我用 Java 编写了一个实用方法:

public static final ImmutableSortedSet<TimeUnit> REVERSED_TIMEUNITS = ImmutableSortedSet.copyOf(
Collections.<TimeUnit>reverseOrder(),
EnumSet.allOf(TimeUnit.class)
);


/**
* Get the number of ..., minutes, seconds and milliseconds
*
* You can specify a max unit so that you don't get days for exemple
* and can get more than 24 hours if you want to display the result in hours
*
* The lowest unit is milliseconds
* @param millies
* @param maxTimeUnit
* @return the result map with the higher unit first
*/
public static Map<TimeUnit,Long> getCascadingDateDiff(long millies,TimeUnit maxTimeUnit) {
if ( maxTimeUnit == null ) {
maxTimeUnit = TimeUnit.DAYS;
}
Map<TimeUnit,Long> map = new TreeMap<TimeUnit,Long>(Collections.<TimeUnit>reverseOrder());
long restInMillies = millies;
Iterable<TimeUnit> forUnits = REVERSED_TIMEUNITS.subSet(maxTimeUnit,TimeUnit.MICROSECONDS); // micros not included
// compute the number of days, then number of hours, then minutes...
for ( TimeUnit timeUnit : forUnits ) {
long numberForUnit = timeUnit.convert(restInMillies,TimeUnit.MILLISECONDS);
map.put(timeUnit,numberForUnit);
restInMillies = restInMillies - timeUnit.toMillis(numberForUnit);
}
return map;
}

适用于:

    Map<TimeUnit,Long> map = new TreeMap<TimeUnit,Long>(Collections.reverseOrder());

但我首先尝试了

    Map<TimeUnit,Long> map = Maps.newTreeMap(Collections.reverseOrder());

我的 IntelliJ 什么也没说,而我的编译器说:

DateUtils.java:[302,48] incompatible types; no instance(s) of type variable(s) K,V exist so that java.util.TreeMap conforms to java.util.Map [ERROR] found : java.util.TreeMap [ERROR] required: java.util.Map

没有比较器也能正常工作:

   Map<TimeUnit,Long> map = Maps.newTreeMap();

但我试过:

Map<TimeUnit,Long> map = Maps.newTreeMap(Collections.<TimeUnit>reverseOrder());

还有:

Map<TimeUnit,Long> map = Maps.newTreeMap(new Comparator<TimeUnit>() {
@Override
public int compare(TimeUnit timeUnit, TimeUnit timeUnit1) {
return 0;
}
});

我得到了同样的错误。所以似乎每次我在 TreeMap 中使用比较器时,类型推断都不再起作用。为什么?


Guava方法的签名是:

  public static <C, K extends C, V> TreeMap<K, V> newTreeMap(Comparator<C> comparator)

预期的返回类型是没有比较器的类型,Java 能够推断出 K = TimeUnit 和 V = Long。

通过 TimeUnit 类型的比较器,Java 知道 C 是 TimeUnit。它还知道预期的返回类型是 K = TimeUnit 和 V = Long 的类型。K extends C 受到尊重,因为 TimeUnit extends TimeUnit(无论如何,如果你认为它是错误的,我也尝试过使用 Object 比较器......)

所以我只是想知道为什么类型推断在这种情况下不起作用?

最佳答案

正如 Michael Laffargue 所建议的,这是一个 OpenJDK6 类型推断错误:

https://bugs.openjdk.java.net/show_bug.cgi?id=100167

http://code.google.com/p/guava-libraries/issues/detail?id=635

它在我的 IntelliJ 中运行良好,在版本 7 中使用 OpenJDK,在版本 6 中使用其他 JDK。


kutschkem 的以下建议有效:

    Map<TimeUnit,Long> map = Maps.<TimeUnit,TimeUnit,Long>newTreeMap(Collections.<TimeUnit>reverseOrder());

注意 <TimeUnit,TimeUnit,Long>,它允许显式强制输入参数。检查这个相关主题:What's this generics usage in Java? X.<Y>method()

谢谢大家

关于Java6、Guava、泛型、类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10945487/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com