如何通过其值比较两个映射并打印每个键的最大值(键在两个映射中都很常见)


void displayFeedback(){
Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
//same staffs taking two different subjects maths and english 
//values taken as feedback for each subject
// i need to compare both subject feedback and print only max as o/p
System.out.println(maths);
System.out.println(maths.entrySet());
//maths.
//maths.entrySet();
//Collections.max(coll, comp)
Map<String, Integer> top = new HashMap<String, Integer>();
System.out.println(maths.size());
for (Map.Entry<String, Integer> math: maths.entrySet()){
for (Map.Entry<String, Integer> eng: english.entrySet()){   
//for(int i = 0;i<maths.size();i++){
//math.comparingByValue()
System.out.println(math.getValue()+" "+eng.getValue());
//Collections.max(math.getValue(), eng.getValue());
if(math.getValue() <= eng.getValue()){
//System.out.println(" math <= eng");
}else
{
//System.out.println("math > eng");
}

教师在两张地图中都很常见,他们同时处理数学和工程科目 以及它们在每个子中的反馈作为两个主题的值

我需要比较并找到最大反馈值,并且仅打印最大值 每个老师....t 教师是两个地图中的通用键

您可以使用math entrySet((迭代器键获取英语反馈映射的值并选择最大值

Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("Nirmala", 70);
maths.put("Subaksha", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("Nirmala", 75);
english.put("Subaksha", 60);
System.out.println(english.entrySet());
System.out.println(maths.entrySet());
Map<String, Integer> top = new HashMap<String, Integer>();
for (Map.Entry<String, Integer> math: maths.entrySet()){    
System.out.println( "Teacher : " +math.getKey() + " Max Feedback :" + Math.max(math.getValue(), english.get(math.getKey())));
}

这是我的;)

private void printMaxValues(Map<String, Integer> first, Map<String, Integer> second) {
Set<String> keys = first.keySet();
for (String s : keys) {
Integer val1 = first.get(s);
Integer val2 = second.get(s);
if (val1 == null && val2 == null) {
System.out.println("No values for key: " + s);
} else if (val1 == null) {
System.out.println(s + "=" + val2);
} else if (val2 == null) {
System.out.println(s + "=" + val1);
} else {
System.out.println(s + "=" + Math.max(val1, val2));
}
}
}

您可以执行以下操作:

1.从学科中建立map,因为关键是老师,价值是它的全部回报

Map<String, List<Integer>> feedbacks = 
Stream.of(maths, english)
.flatMap(map -> map.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(), 
e -> new ArrayList<Integer>(Arrays.asList(e.getValue())), 
(l1, l2)-> {l1.addAll(l2); return l1;}));
System.out.println(feedbacks); //{Subaksha=[80, 60], Nirmala=[70, 75]}

2.从反馈中建立map,因为关键是老师,价值是其反馈的最大

Map<String, Integer> maxs = feedbacks.entrySet().stream()
.collect(Collectors.toMap(e->e.getKey(),
e-> e.getValue().stream().max(Integer::compare).get()));
System.out.println(maxs);     //{Nirmala=75, Subaksha=80}

你可以尝试这样的事情:

static <V extends Comparable<V>>
boolean valuesEquals(Map<?,V> map1, Map<?,V> map2) {
List<V> values1 = new ArrayList<V>(map1.values());
List<V> values2 = new ArrayList<V>(map2.values());
Collections.sort(values1);
Collections.sort(values2);
return values1.equals(values2);
}

map1.keySet().equals(map2.keySet())

以比较值。如果等于为真,那么您会找到两个值中更大的键。

请参考 : http://thelogofthewook.blogspot.com/2011/12/hashmap-comparison-how-to-compare-two.html

您可以为每个教师键(Map<String, Collection<Integer>>(创建一个辅助集合,通过迭代两个现有地图,然后将乐谱放入其中来填充。

或者,您可以在两个映射的 entrySet 上创建一个 Java 8 Stream,然后通过调用 Collectors utils 将它们合并到一个 Map 中:

Map<String, Optional<Integer>> data = Arrays.asList( map1.entrySet(), map2.entrySet() )
.stream()
.flatMap( Set::stream )
.collect( Collectors.groupingBy( Entry::getKey, HashMap::new, Collectors.mapping( Entry::getValue, Collectors.maxBy( Integer::compare ) ) ) );
for( Entry<String, Optional<Integer>> entry : data.entrySet() )
{
System.out.println( entry.getKey() + ": " + entry.getValue().orElse( 0 ) );
}

如果您确定会得到每个科目的每位教师的反馈,则可以使用以下方法。

Map<String, Integer> maths = new HashMap<String, Integer>();
maths.put("T1", 75);
maths.put("T2", 68);
maths.put("T3", 80);
Map<String, Integer> english = new HashMap<String, Integer>();
english.put("T1",60);
english.put("T2",70);
english.put("T3",79);
for(String s:maths.keySet())
System.out.println("Teacher "+s+" with max feedback:"+Math.max(maths.get(s),english.get(s)));

最新更新