簡體   English   中英

在ConcurrentHashMap內部將條目添加到LinkedList無效

[英]Adding entries to LinkedList inside ConcurrentHashMap not working

我有一個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());
    }
}

謝謝!

對於hashMap,此處的鍵必須唯一。 從這段代碼看來,密鑰始終是相同的,因此始終會覆蓋數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM