题解 | #合并表记录#
合并表记录
http://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
import java.util.*;
import java.util.stream.*;
import java.io.*;
public class Main{
public static void main(String[]args){
Map<Integer,Integer> m = new HashMap<>();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a =0 ,b = 0;
for(int i=0;i<n;i++){
//一边输入一边计算,hashMap
a=sc.nextInt();
b=sc.nextInt();
if(m.containsKey(a)){
m.put(a,m.get(a) + b);
}else{
m.put(a,b);
}
}
List<Integer> sortedList = m.keySet().stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
for (Integer k : sortedList) {
System.out.println(k + " " + m.get(k));
}
}
}