Set<Integer,Integer> set = map.entrySet();优化
时间: 2024-05-30 13:13:40 浏览: 11
将代码修改为:
Set<Map.Entry<Integer,Integer>> set = map.entrySet();
这是因为 Map.entrySet() 方法返回的是一个 Set 集合,其中每个元素都是一个 Map.Entry 对象,而 Map.Entry 包含键和值两部分,因此应该将集合元素类型设置为 Map.Entry<Integer, Integer>。这样可以提高代码的可读性,避免在后续代码中需要强制转换类型的情况。
相关问题
Set<Integer> keySet = map1.keySet(); Iterator<Integer> it=keySet.iterator(); int res=0; while(it.hasNext()){ int key=it.next(); res+=map1.get(key)*map2.get(key); }优化这个代码
对于上述代码,可以通过使用增强的 for 循环来遍历 Map 中的键值对,从而简化代码,优化性能。重写的代码如下:
```java
int res = 0;
for (Map.Entry<Integer, Integer> entry : map1.entrySet()) {
int key = entry.getKey();
int value = entry.getValue();
if (map2.containsKey(key)) {
res += value * map2.get(key);
}
}
```
上述代码中,使用 `Map.entrySet()` 方法获取 Map 集合中所有键值对的集合,然后使用增强的 for 循环遍历每个键值对,从而不再需要使用 `Set<Integer> keySet = map1.keySet();` 和 `Iterator<Integer> it=keySet.iterator();`。在计算乘积之和时,使用 `if (map2.containsKey(key))` 来判断 map2 是否包含 map1 中的键,以避免空指针异常。
优化这段代码:Map<String, List<DeepAnalysisSmsRecordResultVo>> smsListMap = smsList.stream().collect(Collectors.groupingBy(DeepAnalysisSmsRecordResultVo::getCollectTargetId)); for (Map.Entry<String, List<DeepAnalysisSmsRecordResultVo>> entry : smsListMap.entrySet()) { String key = entry.getKey(); List<DeepAnalysisSmsRecordResultVo> value = entry.getValue(); if (key.contains(iNode.getCollectTargetId())) { Integer z = null; for (DeepAnalysisSmsRecordResultVo sms : value) { String phone = sms.getOtherNumber(); List<MobilePhonePersonIdentityNumberVo> persons = getCasePerson("3", phone); Long personId = persons.get(0).getPersonId(); for (int j = 0; j < nodeList.size(); j++) { MobilePhoneModelPersonDiagramNodeVo jNode = nodeList.get(j); if (jNode.getPersonId().equals(personId)) { z = j; break; } } } MobilePhoneModelPersonDiagramLinkVo linkVo = new MobilePhoneModelPersonDiagramLinkVo(); linkVo.setSource(i); linkVo.setTarget(z); linkVo.setName("短信记录"); linkVo.setNumber(value.size()); linkList.add(linkVo); } }
优化后的代码如下:
Map<String, List<DeepAnalysisSmsRecordResultVo>> smsListMap = smsList.stream()
.filter(sms -> sms.getCollectTargetId().contains(iNode.getCollectTargetId()))
.collect(Collectors.groupingBy(DeepAnalysisSmsRecordResultVo::getCollectTargetId));
for (Map.Entry<String, List<DeepAnalysisSmsRecordResultVo>> entry : smsListMap.entrySet()) {
String key = entry.getKey();
List<DeepAnalysisSmsRecordResultVo> value = entry.getValue();
Integer z = null;
for (DeepAnalysisSmsRecordResultVo sms : value) {
String phone = sms.getOtherNumber();
List<MobilePhonePersonIdentityNumberVo> persons = getCasePerson("3", phone);
Long personId = persons.get(0).getPersonId();
for (int j = 0; j < nodeList.size(); j++) {
MobilePhoneModelPersonDiagramNodeVo jNode = nodeList.get(j);
if (jNode.getPersonId().equals(personId)) {
z = j;
break;
}
}
}
MobilePhoneModelPersonDiagramLinkVo linkVo = new MobilePhoneModelPersonDiagramLinkVo();
linkVo.setSource(i);
linkVo.setTarget(z);
linkVo.setName("短信记录");
linkVo.setNumber(value.size());
linkList.add(linkVo);
}
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)