我有一张地图,其值是一个整数列表。如果在读卡器中找到密钥,我必须向列表中添加一个整数。我有下面的代码,除了一个以外每次都有效。换句话说,如果密钥存在5次,列表将会说它只有4次。我错过了什么?谢谢!在地图中正确填充列表
Map<String, List<Integer>> map = new TreeMap<String, List<Integer>>();
String key; // the string from my reader
if (map.containsKey(key)) {
map.get(key).add(value); //value being an integer gotten earlier
}
我的代码已经被添加的钥匙地图在构造函数中,像这样:
while (reader.ready()) {
key = reader.readLine();
if(!key.isEmpty()) {
map.put(key, new ArrayList<Integer>());
}
}
我不需要做任何事情,如果关键是没有找到,只需添加值几时。