Java 将两个哈希映射比较为相等的键并检查更高的值
我有两个哈希图Java 将两个哈希映射比较为相等的键并检查更高的值,java,hashmap,comparison,Java,Hashmap,Comparison,我有两个哈希图 Map<String, String> mapA = new HashMap<>(); mapA.put("A", "1"); mapA.put("B", "3"); mapA.put("C", "1"); mapA.put("D", "5"); Map<String, String> mapB = new HashMap<>(); mapB.put("A", "2"); mapB.put("B", "2"); mapB.put(
Map<String, String> mapA = new HashMap<>();
mapA.put("A", "1");
mapA.put("B", "3");
mapA.put("C", "1");
mapA.put("D", "5");
Map<String, String> mapB = new HashMap<>();
mapB.put("A", "2");
mapB.put("B", "2");
mapB.put("C", "4");
mapB.put("D", "2");
Map mapA=newhashmap();
mapA.put(“A”、“1”);
mapA.put(“B”、“3”);
mapA.put(“C”、“1”);
mapA.put(“D”、“5”);
Map mapB=新的HashMap();
mapB.put(“A”、“2”);
mapB.put(“B”、“2”);
mapB.put(“C”、“4”);
mapB.put(“D”、“2”);
如何获得下面的输出
Map<String, String> mapC = new HashMap<>();
mapC("A", "2");
mapC("B", "3");
mapC("C", "4");
mapC("D", "5");
Map-mapC=newhashmap();
mapC(“A”、“2”);
mapC(“B”、“3”);
mapC(“C”、“4”);
mapC(“D”、“5”);
具有唯一键和更大值的Hashmap C。请提前感谢。Map mapA=new Hashmap();
Map<String, String> mapA = new HashMap<String, String>();
mapA.put("A", "1");
mapA.put("B", "3");
mapA.put("C", "1");
mapA.put("D", "5");
Map<String, String> mapB = new HashMap<String, String>();
mapB.put("A", "2");
mapB.put("B", "2");
mapB.put("C", "1");
mapB.put("B", "1");
mapB.put("D", "2");
Map<String, String> mapC = Stream.of(mapA, mapB)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue(), (s1, s2) -> Integer.parseInt(s1) > Integer.parseInt(s2) ? s1 : s2));
System.out.println(mapC);
mapA.put(“A”、“1”);
mapA.put(“B”、“3”);
mapA.put(“C”、“1”);
mapA.put(“D”、“5”);
Map mapB=新的HashMap();
mapB.put(“A”、“2”);
mapB.put(“B”、“2”);
mapB.put(“C”、“1”);
mapB.put(“B”、“1”);
mapB.put(“D”、“2”);
Map mapC=Stream.of(mapA,mapB)
.flatMap(m->m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey,e->e.getValue(),(s1,s2)->Integer.parseInt(s1)>Integer.parseInt(s2)→s1:s2));
系统输出打印LN(mapC);
Map mapA=new HashMap();
mapA.put(“A”、“1”);
mapA.put(“B”、“3”);
mapA.put(“C”、“1”);
mapA.put(“D”、“5”);
Map mapB=新的HashMap();
mapB.put(“A”、“2”);
mapB.put(“B”、“2”);
mapB.put(“C”、“1”);
mapB.put(“B”、“1”);
mapB.put(“D”、“2”);
Map mapC=Stream.of(mapA,mapB)
.flatMap(m->m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey,e->e.getValue(),(s1,s2)->Integer.parseInt(s1)>Integer.parseInt(s2)→s1:s2));
系统输出打印LN(mapC);
你可以这样做
Map<String, Integer> keyToMaxValue = Stream.of(mapA, mapB)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> Integer.valueOf(e.getValue()), Integer::max));
你可以这样做
Map<String, Integer> keyToMaxValue = Stream.of(mapA, mapB)
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(Map.Entry::getKey,
e -> Integer.valueOf(e.getValue()), Integer::max));
您的问题可能需要编辑。在创建
mapB
之后,您仍然在向mapA
添加条目,并且您添加了两次元素……注意,您应该决定值是数字还是字符串,因为它们的比较方式不同。88>9但“88”<“9”您的问题可能需要编辑。在创建mapB
之后,您仍然在向mapA
添加条目,并且您添加了两次元素……注意,您应该决定值是数字还是字符串,因为它们的比较方式不同。88>9但“88”<“9”
Map<objA, objB> mapA = new HashMap<objA, objB>();
mapA.put("A", "1");
mapA.put("B", "3");
mapA.put("C", "1");
mapA.put("D", "5");
Map<objA, objB> mapB = new HashMap<objA, objB>();
mapA.put("A", "2");
mapA.put("B", "2");
mapA.put("C", "1");
mapA.put("B", "1");
mapA.put("D", "2");
mapA.put("C", "4");
Map<objA, objB> mapC = HashMap<objA, objB>) mapA.clone();
for(Map.Entry m:mapA.entrySet()){
if(m.getValue()>mapB.get(m.getKey())){
mapC.put(m.getKey(),m.getValue());
}
}
for(Map.Entry m:mapA.entrySet()){
if(m.getValue()>mapB.get(m.getKey())){
mapC.put(m.getKey(),m.getValue());
}
}