gpt4 book ai didi

java - 删除 Multimap 中的重复值

转载 作者:行者123 更新时间:2023-12-01 12:57:51 25 4
gpt4 key购买 nike

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

key (value count)

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

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

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

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

当然,我也希望携带ID。

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

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>之前,但它删除了重复的内容。

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

最佳答案

我设法通过使用 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 存储唯一的键值,因此这很有用。现在继续计算计数。

关于java - 删除 Multimap 中的重复值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23750637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com