从Java中的Object进行转换,而不会获取未选中的警告 [英] Casting from Object in Java without getting an unchecked warning

查看:452
本文介绍了从Java中的Object进行转换,而不会获取未选中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个类,它有一个< String,Object> 的映射。我需要它来持有任意对象,但在同一时间有时我需要投射一些对象,所以我会做一些像

I wrote a class that has a map of <String, Object>. I need it to hold arbitrary objects, but at the same time sometimes I need to cast some of those objects, so I'll do something like

HashMap<String, Object> map = new HashMap<String, Object>();                                                                                 
Object foo = map.get("bar");                                                                                                                                                                                                         
if (foo instanceof HashMap) {                                                                                                                                                                                                        
    ((HashMap<String, Integer>) foo).put("a", 5);                                                                                                                                                                                    
}            

这会产生警告

Stuff.java:10: warning: [unchecked] unchecked cast
found   : java.lang.Object
required: java.util.HashMap<java.lang.String,java.lang.Integer>
        ((HashMap<String, Integer>) foo).put("a", 5);

我怀疑它与泛型的使用有关。我可以摆脱的错误使用@SupressWarnings(未选中),但我想知道是否有一个更好的方法来做到这一点。或者也许事实,我得到警告意味着我应该重新考虑我在做什么。

I suspect it has to do with the use of generics. I can get rid of the error using @SupressWarnings("unchecked"), but I was wondering if there was a better way to do it. Or maybe the fact that I'm getting the warning means I should reconsider what I'm doing. Is there anything I could do, or should I just use @SupressWarnings?

推荐答案

已编辑(基于

投射到 HashMap< String,Integer> (btw,using 映射而不是 HashMap 可以说是一个更好的选择)是一个不同的故事。很遗憾,没有办法避免在这种情况下由于类型擦除的未经检查的警告。你可以使用它作为非通用地图:

Casting to HashMap<String, Integer> (btw, using Map instead of HashMap is arguably a better choice) is a different story. There's sadly no way to avoid an unchecked warning in that case due to type erasure. You can, however, use it as non-generic map:

if (foo instanceof Map) {                                                                                                                                                                                                        
  ((Map) foo).put("a", 5);                                                                                                                                                                                    
}

你显然必须投射在gets )类型安全,但不会有未经检查的警告。

You'll obviously have to cast on "gets" and you lose (perceived) type safety but there'll be no unchecked warning.

必须有更多的故事。以下代码:

There must be more to this story. The following code:

Map<String, Object> map = Maps.newHashMap(); // or new HashMap<String, Object>();
Object foo = map.get("bar");
if (foo instanceof Widget) {
  ((Widget) foo).spin();
}

为我生成未选中的警告。我也不能想象为什么会。如果你事先知道bar总是返回一个窗口小部件,这样做:

does NOT generate an unchecked warning for me. Nor can I imagine why would it. If you know beforehand that "bar" would always return a widget, doing this:

Widget widget = (Widget) map.get("bar");
widget.spin();

我在这里缺少什么吗?

这篇关于从Java中的Object进行转换,而不会获取未选中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆