列表地图:元素的碰撞

时间:2013-07-24 22:12:03

标签: java data-structures hashmap java-6

我发现了这个Java代码的奇怪行为。我需要存储一个带有整数作为键的映射(用于标识实体)和一个Article列表(基本上是一个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

有任何线索吗?这让我发疯了......

感谢
安德烈

1 个答案:

答案 0 :(得分:0)

所以问题似乎是list_1中标题为“血糖指数和碳水化合物混淆”的文章的权重变为与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());
}