我發現了這個Java代碼的一個奇怪的行爲。我需要存儲地圖的整數作爲鍵(用於識別實體)和第名單(基本上在Java Bean)的話,我這樣定義它:列表地圖:元素碰撞
HashMap<Integer, List<Article>> articles = new HashMap<Integer, List<Article>>();
要填寫的內容,我使用這些指令:
List<Article> topic_articles = new ArrayList<Article>();
topic_articles = emFactory.getRelatedArticle(ticket, list_1, true);
articles.put(15, new ArrayList<Article>(topic_articles));
for (Article article : topic_articles) {
logger.debug("DEBUG ### " + article.getTitle() + " - " + article.getWeight());
}
topic_articles = new ArrayList<Article>();
topic_articles = emFactory.getRelatedArticle(ticket, list_2, true);
articles.put(18, new ArrayList<Article>(topic_articles));
for (Article article : topic_articles) {
logger.debug("DEBUG ### " + article.getTitle() + " - " + article.getWeight());
}
日誌輸出是:
DEBUG ### How to start eating healthier - 1980
DEBUG ### Food label claims - 1980
DEBUG ### The glycemic index and carb confusion - 1980
DEBUG ### What is Health Check - 1980
DEBUG ### Why you should keep a food journal - 1980
DEBUG ### The sweet and lowdown on alternatives to white sugar - 1980
DEBUG ### An apple a day really can keep the doctor away - 1650
DEBUG ### Canada's new and improved food guide - 1650
DEBUG ### Choosing the right cooking oil - 1650
DEBUG ### How much fibre is in food? - 1650
DEBUG ### The glycemic index and carb confusion - 768
DEBUG ### The importance of weight management in diabetes - 768
DEBUG ### Why fibre is a friend to people with diabetes - 768
DEBUG ### Healthy recipes - 768
DEBUG ### Diabetes diet makeover - 768
DEBUG ### Exercise and your blood sugar - 768
DEBUG ### Make your favourite recipes diabetes-friendly - 768
DEBUG ### Why you should keep a food journal - 640
DEBUG ### Overweight or obese? - 512
DEBUG ### Diet + exercise = weight loss - 512
完全正確...但是,當我返回主調地圖,我嘗試讀取內部名單,我得到一個碰撞相同題目的文章(血糖指數和碳水化合物混亂)。
for (Map.Entry<Integer, List<Article>> entry : articles.entrySet()) {
logger.debug("####### Pipeline : " + entry.getKey());
for (Article article : entry.getValue()) {
logger.debug("DEBUG ### " + article.getTitle() + " - " + article.getWeight());
}
}
這是輸出:
####### List : 1
DEBUG ### The glycemic index and carb confusion - 768
DEBUG ### The importance of weight management in diabetes - 768
DEBUG ### Why fibre is a friend to people with diabetes - 768
DEBUG ### Healthy recipes - 768
DEBUG ### Diabetes diet makeover - 768
DEBUG ### Exercise and your blood sugar - 768
DEBUG ### Make your favourite recipes diabetes-friendly - 768
DEBUG ### Why you should keep a food journal - 640
DEBUG ### Overweight or obese? - 512
DEBUG ### Diet + exercise = weight loss - 512
####### List : 2
DEBUG ### How to start eating healthier - 1980
DEBUG ### Food label claims - 1980
DEBUG ### The glycemic index and carb confusion - 768
DEBUG ### What is Health Check - 1980
DEBUG ### Why you should keep a food journal - 1980
DEBUG ### The sweet and lowdown on alternatives to white sugar - 1980
DEBUG ### An apple a day really can keep the doctor away - 1650
DEBUG ### Canada's new and improved food guide - 1650
DEBUG ### Choosing the right cooking oil - 1650
DEBUG ### How much fibre is in food? - 1650
會發生什麼????看起來,我第二次把List放在地圖上時,第一個列表的標題相同的元素被第二個列表覆蓋。基本上位置是糾正,但重量不是。我正在使用Java 1.6
任何線索?它的駕駛我瘋了....
感謝
安德烈
現在還不清楚你的問題是什麼。在這種情況下什麼是「碰撞」? – Jokab
在這種情況下,碰撞發生在兩個列表上,元素「列表2上的血糖指數和碳水化合物混亂 - 768」覆蓋了列表1中的元素「血糖指數和碳水化合物困惑 - 1980」。不確定「碰撞」是否正確term .... –
'emFactory.getRelatedArticle'做了什麼?什麼是'ticket','list_1'和'list_2'? – andy256