循环遍历,集合应用

import java.util.*;
public class Main{
    public static void main(String [] args){
        getMerges();
    }

    public static void getMerges(){
        Scanner scan = new Scanner(System.in);
        Map<Integer,Integer> map = new TreeMap<>();
        int num = Integer.valueOf(scan.nextLine());
        while(num > 0){
            String str = scan.nextLine();
            String [] temp = str.split(" ");
            Integer key = Integer.valueOf(temp[0]);
            Integer value = Integer.valueOf(temp[1]);
            if(null != map && null != map.get(key)){
                map.put(key, map.get(key) + value);
            }else{
                map.put(key, value);
            }
            num--;
        }
        for(Map.Entry<Integer,Integer> entry: map.entrySet()){
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + " " + value);
        }
    }
}