用lambda表达式遍历下边代码 Map<String,String> map =new HashMap<>(); map.put(“1”,“2”); map.put(“3”,“4”); map.put(“5”,“6”);
时间: 2023-11-17 12:04:14 浏览: 69
要使用lambda表达式遍历Map对象,你可以使用`forEach`方法。下面是使用lambda表达式遍历你提供的代码中的Map对象的示例代码:
```java
Map<String, String> map = new HashMap<>();
map.put("1", "2");
map.put("3", "4");
map.put("5", "6");
map.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
```
在这段代码中,我们通过调用`forEach`方法来遍历Map对象。lambda表达式`(key, value) -> System.out.println("Key: " + key + ", Value: " + value)`定义了每个键值对的操作,将键和值打印输出。你可以根据实际需求修改lambda表达式的操作。
相关问题
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) { LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X); ArrayList<String> writeBinList = new ArrayList<>(); Map<Integer, Frames> portMap = new HashMap<>(); List<Frames> frameList = new ArrayList<>(); for (int i = 1; i <= 78; i++) { frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } groupingFrames.put((long) groupingFrames.size(), frameList); long timeStamp = 0; for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) { List<Frames> framesList = entry.getValue(); int[] temp = new int[79]; //全部初始化为-1 Arrays.fill(temp, -1); for (int i = 0; i < framesList.size(); i++) { temp[framesList.get(i).getGroupIndex()] = 1; } // 补充FF 数据帧 for (int i = 1; i < temp.length; i++) { if (temp[i] == -1) { portMap.put(i, new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2)); } } framesList.addAll(portMap.values()); framesList = framesList.stream().sorted(Comparator.comparing(Frames::getGroupIndex)).collect(Collectors.toList()); portMap.clear(); if(timeStamp>7){ timeStamp = 0; } for (int j = 0; j < framesList.size(); j++) { BinaryFileUtils.bxFramesToHex(framesList.get(j),timeStamp,writeBinList); } timeStamp++; } return writeBinList; }优化这段代码
1. 在for循环中创建的Frames对象可以提取出来,避免重复创建对象,提高效率。
2. 可以将填充-1的数组temp改为使用HashSet或LinkedHashSet,避免重复遍历数组。
3. 可以使用Lambda表达式代替for循环中的操作,简化代码。
4. 可以使用Map的computeIfAbsent方法避免重复判断key是否存在。
重构后的代码如下:
```
public List<String> bxGetExcelList(List<LightGroupReq> lightGroupReqs) {
LinkedHashMap<Long, List<Frames>> groupingFrames = jsonToBinPub(lightGroupReqs,VehicleConstants.VEHICLE_X);
ArrayList<String> writeBinList = new ArrayList<>();
Map<Integer, Frames> portMap = new HashMap<>();
List<Frames> frameList = new ArrayList<>();
for (int i = 1; i <= 78; i++) {
frameList.add(new Frames(7, 63, 63, 2550, 15, 15, 0, i, 0, 2));
}
groupingFrames.put((long) groupingFrames.size(), frameList);
long timeStamp = 0;
for (Map.Entry<Long, List<Frames>> entry : groupingFrames.entrySet()) {
List<Frames> framesList = entry.getValue();
Set<Integer> groupIndexSet = new HashSet<>();
framesList.forEach(f -> groupIndexSet.add(f.getGroupIndex()));
// 补充FF 数据帧
groupIndexSet.stream()
.filter(i -> !framesList.stream().anyMatch(f -> f.getGroupIndex() == i))
.forEach(i -> portMap.computeIfAbsent(i, k -> new Frames(7, 63, 63, 2550, 15, 15, 0, k, 0, 2)));
framesList.addAll(portMap.values());
framesList.sort(Comparator.comparing(Frames::getGroupIndex));
portMap.clear();
if (timeStamp > 7) {
timeStamp = 0;
}
framesList.forEach(f -> BinaryFileUtils.bxFramesToHex(f, timeStamp, writeBinList));
timeStamp++;
}
return writeBinList;
}
```
优化这段代码Date occurredOn = new Date(); Map<String,Long> map = new HashMap(); jsonArray.stream().forEach(item->{ Map timeMap = (Map)item; if (map.isEmpty()||map.size()==0){ Long beginTime = (Long) timeMap.get("beginTime"); Long endTime = (Long) timeMap.get("endTime"); Long size = (Long) timeMap.get("size"); if (beginTime<occurredOn.getTime()&&occurredOn.getTime()<endTime){ map.put("time",occurredOn.getTime()+size); } } }); Long ooc = map.get("time"); System.out.println(ooc);
首先,应该避免在循环中使用isEmpty()和size()方法,这会导致性能下降。可以在循环外部先判断jsonArray是否为空,然后使用forEach方法遍历。
其次,可以使用Lambda表达式和Optional类来简化代码和避免空指针异常。
最后,如果jsonArray包含大量数据,可以考虑使用并行流来提高性能。
下面是优化后的代码:
```
Date occurredOn = new Date();
Map<String,Long> map = new HashMap<>();
if (!jsonArray.isEmpty()) {
jsonArray.parallelStream().map(item -> (Map<String, Long>) item)
.filter(timeMap -> {
Long beginTime = timeMap.get("beginTime");
Long endTime = timeMap.get("endTime");
return beginTime < occurredOn.getTime() && occurredOn.getTime() < endTime;
})
.findFirst()
.ifPresent(timeMap -> map.put("time", occurredOn.getTime() + timeMap.get("size")));
}
Long ooc = map.get("time");
System.out.println(ooc);
```
阅读全文