I found a strange behavior of this Java code. I need to store a map with an integer as key (used to identify the entity) and a list of Article (basically a Java bean) so, I defined it in this way:
HashMap<Integer, List<Article>> articles = new HashMap<Integer, List<Article>>();
To fill the contents, I use these instructions:
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());
}
The log output is:
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
Absolutely correct... but, when I return to caller the map, and I try to read inner lists, I get a collision on the articles with same title (The glycemic index and carb confusion).
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());
}
}
This is the output:
####### 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
What happens???? It seems that the second time I put the List on the map, the element with same title of the first list is override by the second one. Basically the position is corrects but the weight not. I'm using Java 1.6
Any clue? It's driving me crazy....
thanks
Andrea