题解 | #记票统计#
记票统计
https://www.nowcoder.com/practice/3350d379a5d44054b219de7af6708894
import java.util.LinkedHashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n1 = sc.nextInt(); Map<String, Integer> map = new LinkedHashMap<>(); for (int i = 0; i < n1; i++) { map.put(sc.next(), 0); } map.put("Invalid", 0); int n2 = sc.nextInt(); for (int i = 0; i < n2; i++) { String key = sc.next(); if (map.containsKey(key)) { map.merge(key, 1, (oldVal, newVal) -> oldVal + 1); } else { map.merge("Invalid", 1, (oldVal, newVal) -> oldVal + 1); } } for (Entry<String, Integer> entry : map.entrySet()) { System.out.println(entry.getKey() + " : " + entry.getValue()); } } }