简体   繁体   English

仔细检查是否存在地图元素?

[英]Double checking for existence a map element?

In the code I'm working on right now I came across the following piece: 在我现在正在使用的代码中,我遇到了以下内容:

PartnerReportItem item = (map.get(partnerId) == null) ? null
    : map.get(partnerId).get(platform); //1, check for existence

if (item == null) {
    item = new PartnerReportItem();
    item.setPlatform(platform);
    item.setPartnerId(partnerId);
    item.setPartnerEmail(partner.getEmail());
    item.setPartnerPaymentDetails(paymentDetails);
    item.setPartnerCoef(partner.getCoef());
    item.setExchangeMap(exchangeMap);

    if (!map.containsKey(partnerId)) //2, double check for existence??
        map.put(partnerId, new HashMap<Platform, PartnerReportItem>());
    map.get(partnerId).put(platform, item);
}

I'm confused by //1 and //2 because I think //2 is unneccesary here. 我对//1//2感到困惑,因为我认为// 2在这里是不必要的。 We've already checked if the element with partnerId existed in the map. 我们已经检查了地图中是否存在带有partnerId的元素。 May I not get a hidden idea? 我可以不隐藏一个主意吗?

the item might be null because the map.get did not return anything (the outer map does not exist) or because the inner map does not contain the requested object. 该项目可能为null,因为map.get不返回任何内容(外部映射不存在)或内部映射不包含所请求的对象。 Therefor, a second check is performed to detect the actual reason item became null. 因此,执行第二次检查以检测实际原因item变为空。

It looks like you have a map within a map. 您似乎在地图中有一张地图。

PartnerReportItem item = (map.get(partnerId) == null) ? null : map.get(partnerId).get(platform);

If item is not null, this means that partnerId is found in the outer map and platform is found in the inner map. 如果item不为null,则表示在外部地图中找到partnerId ,在内部地图中找到platform

However, if item is null, it's still possible that partnerId key exists in the outer map, which means the containsKey check if necessary. 但是,如果item为null,则外部映射中仍可能存在partnerId键,这意味着在必要时进行containsKey检查。

As the other answers stated, you need to keep this check. 如其他答案所述,您需要保留此检查。 However this is a bad coding actually, because there are multiple reasons which are unrelated for the PartnerReportItem instance to be null (and hence the second check in the first if statement). 但是,实际上这是一个不好的编码,因为有多个原因与PartnerReportItem实例为null无关(因此,第一个if语句中的第二次检查)无关。

You better have to split it to make the intent more clear (and avoid confusions when others are reading your code). 您最好将其拆分以使意图更清晰(并避免在其他人阅读您的代码时造成混淆)。

Map<Platform, PartnerReportItem> innerMap = map.get(partnerId);
if(innerMap == null) {
    innerMap = new HashMap<Platform, PartnerReportItem>();
    innerMap.put(partnerId, createPartnerReportItem(...));
} else {
    PartnerReportItem item = innerMap.get(partnerId);
    if(item == null) {
        innerMap.put(partnerId, createPartnerReportItem(...));
    }
}

or another variant: 或其他变体:

Map<Platform, PartnerReportItem> innerMap = map.get(partnerId);
if(innerMap == null) {
    innerMap = new HashMap<Platform, PartnerReportItem>();
}
PartnerReportItem item = innerMap.get(partnerId);
if(item == null) {
    innerMap.put(partnerId, createPartnerReportItem(...));
}

The point is that the programmer who wrote this ternary nested statement though that it would be less code but at the end, you see that it's just confusing. 关键是,编写此三元嵌套语句的程序员虽然会减少代码的编写,但是最后您会发现它只是令人困惑。 Always write readable code! 始终编​​写可读代码!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM