【发布时间】:2015-03-30 11:18:06
【问题描述】:
我有一个 ConcurrentHashMap,它包含一个字符串作为键和 LinkedList 作为值。列表的大小不应超过 5。我正在尝试向列表中添加一些元素,但是当我打印出地图时,我只看到最后添加的元素。这是我的代码:
private ConcurrentHashMap<String, LinkedList<Date>> userDisconnectLogs = new ConcurrentHashMap<String, LinkedList<Date>>();
public void addNewDateEntry(String userId, LinkedList<Date> timeStamps) {
if (timeStamps.size() >= 5) {
timeStamps.poll();
timeStamps.add(new Date());
userDisconnectLogs.put(userId, timeStamps);
} else {
timeStamps.add(new Date());
userDisconnectLogs.put(userId, timeStamps);
}
for (Entry<String, LinkedList<Date>> entry : userDisconnectLogs
.entrySet()) {
String key = entry.getKey().toString();
;
LinkedList<Date> value = entry.getValue();
System.out.println("key: " + key + " value: " + value.size());
}
}
谢谢!
【问题讨论】:
-
你的钥匙一直都是一样的。
-
显示你正在调用的代码
addNewDateEntry()? -
类似
userService.addNewDateEntry(userService.getUserIdByPrincipal(disconnectedUser), (LinkedList<Date>) timeStamps); -
timeStamps的值是多少?作为旁注,你为什么需要投射它?
标签: java dictionary linked-list