我发现这段 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
有什么线索吗?这让我疯狂....
谢谢安德里亚
_