如何检查Java中收到的泛型方法?(How to check the generic my method received in Java?)
我有一个set方法需要接受2种泛型:
Map<Long,Map<String,Object>>
和Map<Long,Map<String,String>>
但是我需要将其设置为
Map<Long,Map<String,String>>
我知道String是一个Object,所以在参数中我可以使用
Map<Long,Map<String,Object>>
,但是如果不需要,我不想进行转换。 使用instanceof似乎不起作用,给我一个错误:无法对参数化类型
Map<Long,Map<String,String>>
执行instanceof检查。 请使用表单Map<?,?>
因为在运行时将删除其他泛型类型信息以下是我现在的现行代码。
public void setRates(Map<Long,Map<String,Object>> inputRates){ if(inputRates!=null){ Map<Long,Map<String,String>> result = new HashMap<>(); // if <Map<Long,Map<String,String>> do below : if(inputRates instanceof Map<Long,Map<String,String>>){ //instanceof not working.... this.rates= inputRates; }else{ // if really Map<Long,Map<String,Object>> do below - converting Object to String for (Map.Entry<Long, Map<String,Object>> entry1 : inputRates.entrySet()) { Map<String,String> resultMap = new HashMap<>(); Long key = entry1.getKey(); Map<String,Object> inputMap = entry1.getValue(); for(Map.Entry<String,Object> entry2 : inputMap.entrySet()) { String key2 = entry2.getKey(); Object value2 = entry2.getValue(); if(value2!=null){ resultMap.put(key2, value2.toString()); }else{ resultMap.put(key2, null); } } result.put(key, resultMap); } this.rates = result; } } }
关于我如何实现这个的任何想法?
I have a set method that need to accept 2 kind of generics :
Map<Long,Map<String,Object>>
, andMap<Long,Map<String,String>>
However I need to set it as
Map<Long,Map<String,String>>
I know that String is an Object, so in the parameter I can use
Map<Long,Map<String,Object>>
, however I don't want to undergo a conversion if there no need to. Using instanceof seems not working, throwing me with error:Cannot perform instanceof check against parameterized type
Map<Long,Map<String,String>>
. Use the formMap<?,?>
instead since further generic type information will be erased at runtimeBelow is current codes I have now.
public void setRates(Map<Long,Map<String,Object>> inputRates){ if(inputRates!=null){ Map<Long,Map<String,String>> result = new HashMap<>(); // if <Map<Long,Map<String,String>> do below : if(inputRates instanceof Map<Long,Map<String,String>>){ //instanceof not working.... this.rates= inputRates; }else{ // if really Map<Long,Map<String,Object>> do below - converting Object to String for (Map.Entry<Long, Map<String,Object>> entry1 : inputRates.entrySet()) { Map<String,String> resultMap = new HashMap<>(); Long key = entry1.getKey(); Map<String,Object> inputMap = entry1.getValue(); for(Map.Entry<String,Object> entry2 : inputMap.entrySet()) { String key2 = entry2.getKey(); Object value2 = entry2.getValue(); if(value2!=null){ resultMap.put(key2, value2.toString()); }else{ resultMap.put(key2, null); } } result.put(key, resultMap); } this.rates = result; } } }
Any idea on how I implement this?
原文:https://stackoverflow.com/questions/49535198
最满意答案
我已经安装并更新了修复问题的Visual Studio C ++模板
I have installed and updated the Visual Studio C++ template which got the issue fixed
相关问答
更多-
通过Windows注册表进行的PHP配置是否适用于CLI?(Does PHP configuration via the Windows registry work for CLI?)[2023-11-18]
引自“ 用户贡献的备注 ”: 能够将php指令放在httpd.conf中并让它们在每个目录或每个主机的基础上工作是非常好的。 现在还有另一个方面可能值得注意: 放入apache conf文件的php.ini指令适用于php作为apache模块运行时(即在网页中),但不适用于以CLI(命令行界面)运行的情况。 这样的功能可能是不愉快的少数人不想要的,但我想大多数人会发现它很有用。 就我而言,我很高兴我可以在我的httpd.conf文件中使用open_basedir,它限制了我的域的网络用户和子管理员的访问权限 ... -
你需要在facebook和google中创建应用程序,获取api密钥并将其放在它所说的位置: apiKey: 'api-key-here'你环境中的apiKey: 'api-key-here' 。 Turns out my problem was a simple one, the ember serve process needed to be restarted (Ctrl + c, and then re run ember serve).
-
Wcf服务配置问题(Wcf service configuration issue)[2023-02-09]
你应该在客户端和服务的双方使用相同的bindingConfiguration you should use same bindingConfiguration on both sides clients and service -
您可以选择直接修改原始webpack配置( 示例 ) 它通过config参数传入 这让我们做这样的事情: if (config.performance) { config.performance.hints = false; } 多个webpack配置通过此函数传递 所以我们首先检查performance属性,因为它仅在某些情况下出现( docs ) you have the option of modifying the original webpack config directly (exa ...
-
复制由Angular2 CLI插件创建的Eclipse运行配置(Duplicating Eclipse run configurations created by the Angular2 CLI plugin)[2024-01-24]
我建议你安装Angular2 Eclipse 1.1.0修复了几个bug: 修复重复启动的错误 添加测试 为ng启动提供UI 你可以使用angular-cli.json编辑器执行现在的ng命令: I suggest you that you install Angular2 Eclipse 1.1.0 which fixes several bugs: fix bug with duplicated launch add ng test provide an UI for ng launch you can ... -
Mean-cli配置问题(Mean-cli configuration issue)[2021-12-22]
我已经安装并更新了修复问题的Visual Studio C ++模板 I have installed and updated the Visual Studio C++ template which got the issue fixed -
安装包nodejs-legacy ,它只是nodejs符号链接。 Install package nodejs-legacy, it is just symlink to nodejs.
-
因此,您所遵循的说明似乎有点误导。 您不需要第二个文件,您应该删除app.config文件。 您可以将所有配置值放在web.config中。 只需确保配置项位于
节点中。 但是您应该继续在代码中使用ConfigurationManager类来访问值。 So it appears the instructions you followed were a bit misleading. You do not need a second file, you should delete the ... -
Sitecore配置问题(Sitecore Configuration issue)[2022-12-12]
该错误消息表明它正在尝试查找ContentSearch配置文件中引用的syncMaster更新策略您的修补程序配置文件主动从配置中删除syncMaster更新策略。 -
对于AWS Glacier您需要帐户ID List Vaults(GET保险库) 您可以从Support页面获取您的帐户ID(12位数) - 在AWS仪表板的右上角。 Looks like for AWS Glacier you need the account ID List Vaults (GET vaults) You can get your account id (12 digits) from Support page - Top right on your AWS dashboard.