我用 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/
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht
这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/