删除Multimap中的值重复项

时间:2014-05-20 03:37:40

标签: java android multimap

我有一个Multimap,我需要像这样显示输出:

  

键(值计数)

例如,我在Multimap中有这些数据:

{Entertainment=[5],
Food=[2, 2, 2],
Products=[11],
Health & Beauty=[3]}

数组中的数字是相关键的ID。 我希望它输出的内容如下:

Entertainment (1)
Food (3)
Products (1)
Health & Beauty (1)

当然,我也想要携带身份证。

这是我到目前为止所做的,

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);
}

您知道,该值将放入一个传递给BaseAdapter的数组中以填充ListView。或者除了使用Multimap之外还有其他方法吗?我之前使用过HashMap<String, String>,但它删除了重复内容。

如果有任何不清楚的地方,请告诉我。我已经考虑过了。

1 个答案:

答案 0 :(得分:1)

我设法通过使用HashMap解决了这个问题。

在for循环中,

Map map = new HashMap();

...

map.put(response.getMerchants().get(i).getCategory_name(),
   response.getMerchants().get(i).getCategory_id());

...

把它放在我的适配器中:

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);
}

由于HashMap存储唯一键值,因此这很有用。现在继续计算计数。

相关问题