所以我想输入一个数字,然后输入一个人或其他人的名字.这没有问题,但为什么我不能在1个块中放置2个错误异常?
while (true) {
try {
int id = Integer.parseInt( reader.readLine() );
String name = reader.readLine();
if (name.equals("")) {
break;
}
map.put(name, id);
} catch (NumberFormatException | IOException e) {
break;
}
}
Run Code Online (Sandbox Code Playgroud)
当我试图打印我的价值时,我明白了 NumberFormatException
for (Map.Entry<Integer, String> pair: map.entrySet()) {
int id = pair.getKey();
String name = pair.getValue();
System.out.println(id + " " + name);
}
Run Code Online (Sandbox Code Playgroud) 我不知道如何最好地描述我的问题,但在这里,我正在尝试从中删除相同的名称(值) HashMap<String, String> map = new HashMap<String, String>();
例如,如果这张地图包含这样的名字
map.put("Vivaldi","Antonio");
map.put("Belucci", "Monica");
map.put("Gudini", "Harry");
map.put("Verdo", "Dhuzeppe");
map.put("Maracci", "Bruno");
map.put("Carleone", "Vito");
map.put("Bracco", "Luka");
map.put("Stradivari", "Antonio");
Run Code Online (Sandbox Code Playgroud)
我想使用 method 从中删除所有值为“Antonio”的条目removeTheFirstNameDuplicates,我在谷歌上看了几天,所有的例子都接近我想要的,但并不是我真正需要的。
我的想法是,我需要一些东西来检查地图,如果其中包含相同的值,则删除重复项。但是我该怎么做呢?