【问题标题】:Map of lists: collision of elements列表映射:元素的碰撞
【发布时间】:2013-07-24 22:12:03
【问题描述】:

我发现这段 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

会发生什么???似乎我第二次将列表放在地图上时,与第一个列表具有相同标题的元素被第二个列表覆盖。基本上位置是正确的,但重量不是。我正在使用 Java 1.6

有什么线索吗?快把我逼疯了……

谢谢
安德烈亚

【问题讨论】:

  • 目前还不清楚您的问题到底是什么。在这种情况下,什么是“碰撞”?
  • 在这种情况下,冲突发生在两个列表上,列表 2 上的元素“血糖生成指数和碳水化合物混淆 - 768”覆盖列表 1 上的元素“血糖生成指数和碳水化合物混淆 - 1980”。不确定“碰撞”是否正确......
  • emFactory.getRelatedArticle 是做什么的? ticketlist_1list_2 是什么?
  • 文章列表提取参数

标签: java data-structures hashmap java-6


【解决方案1】:

所以问题似乎是,在您创建 list_2 后,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());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多