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

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

有什么线索吗?这让我疯狂....

谢谢安德里亚
_

4

1 回答 1

0

所以问题似乎是,在您制作 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());
}
于 2013-07-24T23:02:28.543 回答