Reputation: 4815
Is it good to store multiple information like this:
List<Map<Integer, Object>> l = new ArrayList<Map<Integer, Object>>();
Map<Integer, Object> m = new HashMap<Integer, Object>();
m.put(123, null);
l.add(m);
I guess there's a better way.
In my case I need to store a List of Categories and the information how many Articles are using this Category for each.
Upvotes: 0
Views: 199
Reputation: 1028
I think you are on the right way. But also you can encapsulate the other information in class and put a list of objects
Like this
Class CategorieModel{
private Integer articlies;
private Categories categories;
//Constructors
//getters and setters
}
List<CategorieModel> m = new ArrayList<CategorieModel>();
m.add(new CategorieModel(111,null));
Upvotes: 1
Reputation: 801
I'd use a multimap from guava.
http://code.google.com/p/guava-libraries/
Multimap<Category, Article> categorysToArticles = HashMultimap.create();
Put all category to article relations;
categoryToArticles.put(category, article);
Get all articles in a category; (as a collection)
categoryToArticles.get(category);
Get count of articles in a category;
categoryToArticles.get(category).size();
Upvotes: 0
Reputation: 10161
Do you want to have a many-to-many relationship between categories and articles? If so then I would suggest you have:
Map<Category, Set<Article>>
this way for each category if you want to know the count of articles for that category you just need to check the size of the set mapped to the category. However, if you want to find out how many categories a particular article belongs to, then you will have to work it out by going through every category and checking if the set contains the article you are interested in.
UPDATE
you mention Hibernate in one of your comments, maybe what you are looking for is a bi-directional many-to-many relationship? i.e. your Category object will hold a collection of articles that are in that category and your Article object will hold a collection of categories that it belongs to.
If this is the case then check out this question:
Hibernate Bidirectional Many to Many implementation
for a discussion on Bi-Directional Many-to-Many relationships.
Upvotes: 0
Reputation: 4421
You need to be clearer of the terms you are using. What's "Category", what's "Articles"?
In your example:
List<Map<Integer, Object>> l = new ArrayList<Map<Integer, Object>>();
Map<Integer, Object> m = new HashMap<Integer, Object>();
m.put(123, null);
what's the point of the Map's value if you are just passing in NULL ?
1 easiest way if you really just want to have a count of reference to each category:
Map<Category, Integer> categoryCount = ...
or you can use a primary key from Category to keep count as well:
Map<Integer, Integer> categoryCount = ... // the Map key is Category.getId()
or if you want to know which Article reference that category:
Map<Category, List<Article>> categoryArticleRef = ...
would work too, then to know how many articles you can always just do:
categoryArticleRef.get(category).size(); // need NPE check, etc
Upvotes: 0
Reputation: 533880
If you want to store a count of the articles for each Category I would use
Map<Category, Integer> categoryCountMap = ...
Upvotes: 2
Reputation: 692231
Then why not use a List<CategoryUsage>
, where CategoryUsage
is a class containing a Category
, and the number of articles using it? Much more readable, type-safe, maintainable, and object-oriented IMHO.
Upvotes: 4