JAVA 泛型强制转换若干疑问

这样强转编译器就报错:

Set<String> tables = (Set<String>) realMap.keySet()

改为对Set内具体元素强转则正常:

Set<Object> tables = realMap.keySet();
for(Object table: table){
	String t= (String)table;
	...

而把Object强转为带泛型的Map则不报错:

Map<String, OdpsPartitionInfoDTO> latestInfo = (Map<String, OdpsPartitionInfoDTO>) realMap.get(table);

附上源码,哪位大神解释下?

    @Override
    public CruiserItemResult.Rule doValidate(Map<String, String> ruleExpressionMap, Map<Object, Object> realMap) {
        CruiserItemResult.Rule ruleResult = new CruiserItemResult.Rule();
        ruleResult.setStatus(PASS);
        String errMsg;

        try {
            if (realMap != null && !realMap.isEmpty()) {
                for (Object table : realMap.keySet()) {
                    /* 上次巡检结果*/
                    String lastPartitionInfo = Diamond.getConfig((String) table, "ad_qa_base_platform", 10000L);
                    if (lastPartitionInfo != null && !lastPartitionInfo.isEmpty()) {
                        JSONObject lastJsonObject = JSON.parseObject(lastPartitionInfo);
                        if (realMap.containsKey(table)) {
                            Map<String, OdpsPartitionInfoDTO> latestInfo = (Map<String, OdpsPartitionInfoDTO>) realMap.get(table);
                            Map<String, OdpsPartitionInfoDTO> oldInfo = JSON.parseObject(lastJsonObject.getString((String) table), new TypeReference<Map<String, OdpsPartitionInfoDTO>>() {
                            });
                            if (oldInfo != null) {
                                for (Map.Entry<String, OdpsPartitionInfoDTO> old : oldInfo.entrySet()) {
                                    if (latestInfo.containsKey(old.getKey()) &&
                                            latestInfo.get(old.getKey()).getSize() != old.getValue().getSize()
                                            && old.getValue().getSize() > 0) {
                                        errMsg = String.format("[%s][%s] 数据变化. O[%s], N[%s]", table, old.getKey(), old.getValue(), latestInfo.get(old.getKey()));
                                        ruleResult.setStatus(FAIL);
                                        ruleResult.setLevel(CRITICAL);
                                        ruleResult.setMsg(errMsg);
                                        logger.info(errMsg);
                                    }
                                }
                            }
                        } else {
                            logger.info("本次巡检没有表: " + table);
                        }
                    }
                    Diamond.publishSingle((String) table, "ad_qa_base_platform", JSON.toJSONString(realMap.get(table)));
                }
            }
        } catch (Exception e) {
            errMsg = "校验失败. " + e.getMessage();
            ruleResult.setStatus(FAIL);
            ruleResult.setLevel(CRITICAL);
            ruleResult.setMsg(errMsg);
            e.printStackTrace();
        }

        return ruleResult;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值