如何通过两个地图的值比较两个地图并为每个键打印最大值(两个地图中的键都相同)

时间:2018-06-26 08:35:17

标签: java collections

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 老师是这两个图中的共同键

6 个答案:

答案 0 :(得分:1)

您可以使用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())));
          }

答案 1 :(得分:1)

这是我的建议;)

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));
        }
    }
}

答案 2 :(得分:1)

您可以执行以下操作:

1。。因为关键是老师,而价值就是它的全部回报

map

2。从反馈中获取一个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]} ,因为关键是老师,而值是其反馈的最大值

map

答案 3 :(得分:0)

您可以尝试以下操作:

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())

比较值。如果equals为true,则可以找到两个值中的更大Key。

引用:http://thelogofthewook.blogspot.com/2011/12/hashmap-comparison-how-to-compare-two.html

答案 4 :(得分:0)

您可以通过迭代两个现有地图,然后将分数放入其中,为您填写的每个教师钥匙(Map<String, Collection<Integer>>)创建一个助手集合。

或者您可以在两个地图的entrySets上创建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 ) );
    }

答案 5 :(得分: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)));