dim*_*414 1 java casting primitive-types
可能重复:
确定对象是否是基本类型
这可能听起来很蠢,但请原谅我,我正在使用讽刺的代码.给定一组对象,最好的方法是识别哪些是基元,或者更准确地说,是基元周围的包装器.
假设我要打印所有基元:
HashMap<String,Object> context = GlobalStore.getContext(); // Some bizarre, strangely populated context
for(Entry<String,Object> e : context.entrySet()){
if(e.value() instanceof PRIMITIVE){ // What goes here?
System.out.println(e);
}
}
Run Code Online (Sandbox Code Playgroud)
除了通过逐个枚举所有原语之外,这是否可行?
优秀的Google Guava项目提供了一个Primitives.isWrapperType(Class),可用作:
Primitives.isWrapperType(e.value().getClass())
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1241 次 |
| 最近记录: |