Reputation: 6318
I have a Multimap and I need to display the output like this:
key (value count)
For example, I have these data inside the Multimap:
{Entertainment=[5],
Food=[2, 2, 2],
Products=[11],
Health & Beauty=[3]}
The number in the array is the ID of related keys. What I would want it to output is as follows:
Entertainment (1)
Food (3)
Products (1)
Health & Beauty (1)
Of course, I want the ID to be carried along as well.
This is what I did so far,
Multimap<String, String> map = ArrayListMultimap.create();
for (int i = 0; i < response.getMerchants().size(); i++) {
//hmm.setCategory_id(response.getMerchants().get(i).getCategory_id());
//hmm.setCategory_name(response.getMerchants().get(i).getCategory_name());
//hmm.setCategory_count(123); // FIXME
map.put(response.getMerchants().get(i).getCategory_name(),
response.getMerchants().get(i).getCategory_id());
//arrCategory.add(hmm);
}
for (String key : map.keySet()) {
int count = map.get(key).size();
Log.d(TAG, "count= "+count);
}
Just so you know, the value will be put in an array an passed to the BaseAdapter
to populate a ListView. Or is there any other way to do this besides using Multimap? I've used HashMap<String, String>
before, but it removes duplicate.
If anything unclear, please let me know. I've done thinking about it.
Upvotes: 0
Views: 882
Reputation: 6318
I managed to solve this problem by using HashMap instead.
In a for loop,
Map map = new HashMap();
...
map.put(response.getMerchants().get(i).getCategory_name(),
response.getMerchants().get(i).getCategory_id());
...
To put it in my adapter:
for (Object o : map.entrySet()) {
Map.Entry entry = (Map.Entry) o;
HomeMerchantsModel hms = new HomeMerchantsModel();
hms.setCategory_id(String.valueOf(entry.getValue()));
hms.setCategory_name(String.valueOf(entry.getKey()));
arrCategory.add(hms);
}
Since HashMap stores unique key-value, this is useful. Now moving on to calculate the counts.
Upvotes: 1