2013-07-24 23 views
0

我發現了這個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

任何線索?它的駕駛我瘋了....

感謝
安德烈

+0

現在還不清楚你的問題是什麼。在這種情況下什麼是「碰撞」? – Jokab

+0

在這種情況下,碰撞發生在兩個列表上,元素「列表2上的血糖指數和碳水化合物混亂 - 768」覆蓋了列表1中的元素「血糖指數和碳水化合物困惑 - 1980」。不確定「碰撞」是否正確term .... –

+0

'emFactory.getRelatedArticle'做了什麼?什麼是'ticket','list_1'和'list_2'? – andy256

回答

0

所以,問題似乎是與LIST_1變化標題「血糖指數和碳水化合物的困惑」一文的重量具有相同的價值在創建list_2之後,list_2中的等價物品。您確定當emFactory.getRelatedArticle(ticket, list_2, true);找到具有相同標題的文章時,會返回其他對象嗎?顯然你期待/假設這一點,但你有沒有證實這是實際發生的事情?

您可以通過重新排列代碼來做相同的事情,但以不同的順序來測試,首先構建兩個列表然後輸出它們。如果輸出只是通過重新排列線條而改變,那麼你就有了一把吸菸槍。

List<Article> topic_articles1 = emFactory.getRelatedArticle(ticket, list_1, true); 
articles.put(15, new ArrayList<Article>(topic_articles1)); 
List<Article> topic_articles2 = emFactory.getRelatedArticle(ticket, list_2, true); 
articles.put(18, new ArrayList<Article>(topic_articles2)); 

for (Article article : topic_articles1) { 
    logger.debug("DEBUG ### " + article.getTitle() + " - " + article.getWeight()); 
} 
for (Article article : topic_articles2) { 
    logger.debug("DEBUG ### " + article.getTitle() + " - " + article.getWeight()); 
}