Map<String, OrganizationLevelVo> data = organizationFeignService.getResourceLevelCity("1533835007229984").getData(); List<OrganizationVo> voList = organizationFeignService.getLargeScreenResourceCity().getData(); List<String> ids = voList.stream().map(OrganizationVo::getId).collect(Collectors.toList()); List<OrganizationLevelVo> vos = new ArrayList<>(); for (String s : data.keySet()) { for (String id : ids) { if (s.equals(id)){ vos.add(data.get(s)); } } }优化一下代码
时间: 2023-12-16 09:06:31 浏览: 23
可以使用 Java 8 的流式操作来简化代码,将两个循环合并为一个,并使用过滤器来筛选符合条件的元素,代码可优化为:
```
List<OrganizationLevelVo> vos = data.entrySet().stream()
.filter(entry -> ids.contains(entry.getKey()))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
```
其中,`entrySet()` 方法返回映射中包含的映射关系的 Set 视图,`filter()` 方法根据条件过滤元素,`map()` 方法将元素映射为另一个元素,`Collectors.toList()` 方法将结果收集为列表。
相关问题
优化这段代码:List<CompletableFuture<CallIntersectionVo>> futureList = Lists.newArrayList(); for (Map.Entry<String, List<String>> entry : intersectionResult.entrySet()) { CompletableFuture<CallIntersectionVo> future = CompletableFuture.supplyAsync(() -> { String account = entry.getKey(); List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList()); CallIntersectionVo vo = new CallIntersectionVo(); if (personNoList.size() >= 2) { List<PersonBasicVo> personVoList = Lists.newArrayList(); int count = 0; for (String personNo : personNoList) { Map<String, Object> callMap = callMapList.stream().filter(map -> personNo.equals(map.get("personNo"))).findAny().get(); List<CallRecord> callList = (List<CallRecord>) callMap.get("callList"); // 统计通话频率 count += callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count(); // 获取涉案人 personVoList.add(personList.stream().filter(person -> personNo.equals(person.getPersonNo())).findAny().get()); } // 共同号码是否属于涉案人 String commonPersonName = getCommonPersonName(personList, account); if (frequency != null && frequency > 0) { if (count >= frequency) { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); } return vo; } else { vo.setPersons(personVoList); vo.setCommonAccount(account); vo.setFrequency(count); vo.setCommonPersonName(commonPersonName); return vo; } } else { return vo; } }, executor); futureList.add(future); } voList.addAll(futureList.stream().map(CompletableFuture::join) .distinct().sorted(Comparator.comparing(vo -> vo.getPersons().size())) .collect(Collectors.toList()));
优化后的代码如下:
```java
List<CompletableFuture<CallIntersectionVo>> futureList = intersectionResult.entrySet().stream()
.map(entry -> CompletableFuture.supplyAsync(() -> {
String account = entry.getKey();
List<String> personNoList = entry.getValue().stream().distinct().collect(Collectors.toList());
CallIntersectionVo vo = new CallIntersectionVo();
if (personNoList.size() >= 2) {
List<PersonBasicVo> personVoList = personNoList.stream()
.map(personNo -> {
Map<String, Object> callMap = callMapList.stream()
.filter(map -> personNo.equals(map.get("personNo")))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
List<CallRecord> callList = (List<CallRecord>) callMap.get("callList");
int count = (int) callList.stream().filter(x -> account.equals(x.getRelationshipAccount())).count();
return personList.stream()
.filter(person -> personNo.equals(person.getPersonNo()))
.findAny()
.orElseThrow(() -> new NoSuchElementException("Person not found"));
})
.collect(Collectors.toList());
String commonPersonName = getCommonPersonName(personList, account);
vo.setPersons(personVoList);
vo.setCommonAccount(account);
vo.setFrequency(personVoList.size());
vo.setCommonPersonName(commonPersonName);
return vo;
} else {
return vo;
}
}, executor))
.collect(Collectors.toList());
List<CallIntersectionVo> voList = futureList.stream()
.map(CompletableFuture::join)
.distinct()
.sorted(Comparator.comparingInt(vo -> vo.getPersons().size()))
.collect(Collectors.toList());
```
主要优化:
1. 使用流式编程,替换原来的for循环和entrySet遍历。
2. 使用`stream()`和`collect(Collectors.toList())`方法来收集结果,代替手动添加到列表中。
3. 使用`orElseThrow`方法来处理可能找不到元素的情况,避免空指针异常。
4. 将lambda表达式内联,使代码更简洁。
5. 使用`Comparator.comparingInt`方法来指定排序规则,避免编译器警告。
list<VO> 转为 Map<String, List<String>>
可以使用Java 8的流(Stream)来将List<VO>转换为Map<String, List<String>>。下面是一个示例代码:
```java
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
List<VO> voList = getListOfVO(); // 假设这是一个List<VO>的实例
Map<String, List<String>> resultMap = voList.stream()
.collect(Collectors.groupingBy(VO::getKey, Collectors.mapping(VO::getValue, Collectors.toList())));
System.out.println(resultMap);
}
// 假设VO类的定义如下
static class VO {
private String key;
private String value;
public VO(String key, String value) {
this.key = key;
this.value = value;
}
public String getKey() {
return key;
}
public String getValue() {
return value;
}
}
// 获取一个List<VO>的示例数据
private static List<VO> getListOfVO() {
List<VO> voList = List.of(
new VO("key1", "value1"),
new VO("key2", "value2"),
new VO("key1", "value3"),
new VO("key2", "value4")
);
return voList;
}
}
```
运行以上代码,将会输出以下结果:
```
{key1=[value1, value3], key2=[value2, value4]}
```
这里使用了`Collectors.groupingBy`方法来根据VO对象的key属性进行分组,然后使用`Collectors.mapping`方法将VO对象的value属性提取出来并转换为List<String>。最终得到的结果是一个Map,其中key是VO对象的key属性,value是对应的value属性的列表。