我正在使用以下代码遍历哈希映射列表,以查找所需的哈希映射对象。
public static Map<String, String> extractMap(List<Map<String, String>> mapList, String currentIp) {
for (Map<String, String> asd : mapList) {
if (asd.get("ip").equals(currentIp)) {
return asd;
}
}
return null;
}
我在考虑使用Java 8流。这是我用来显示所需对象的代码。
public static void displayRequiredMapFromList(List<Map<String, String>> mapList, String currentIp) {
mapList.stream().filter(e -> e.get("ip").equals(currentIp)).forEach(System.out::println);
}
我无法使用以下代码从流中获取所需的Map。
public static Map<String, String> extractMapByStream(List<Map<String, String>> mapList, String currentIp) {
return mapList.stream().filter(e -> e.get("ip").equals(currentIp))
.collect(Collectors.toMap(p -> p.getKey(), p -> p.getValue()));
}
这会导致语法错误类型不匹配:无法从Map转换为Map。我需要在这里放什么才能得到Map?
List<Map<Set<String>, Map<Integer, List<HttpClient>>>>
代表什么意思。 - Michael