首页 \ 问答 \ 如何检查Java中收到的泛型方法?(How to check the generic my method received in Java?)

如何检查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>>, and
  • Map<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 form Map<?,?> instead since further generic type information will be erased at runtime

Below 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
更新时间:2022-11-30 18:11

最满意答案

我已经安装并更新了修复问题的Visual Studio C ++模板


I have installed and updated the Visual Studio C++ template which got the issue fixed

相关问答

更多
  • 引自“ 用户贡献的备注 ”: 能够将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).
  • 你应该在客户端和服务的双方使用相同的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 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 ...
  • 我已经安装并更新了修复问题的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 ...
  • 该错误消息表明它正在尝试查找ContentSearch配置文件中引用的syncMaster更新策略 您的修补程序配置文件主动从配置中删除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.

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)