从排序的ArrayList中删除重复项,同时保持某些元素从重复 [英] Remove duplicates from a sorted ArrayList while keeping some elements from the duplicates

查看:156
本文介绍了从排序的ArrayList中删除重复项,同时保持某些元素从重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧起初我以为这将是pretty简单。但我想不出一个有效的方式来解决这个问题。我想通蛮力的方式来解决这个问题,但是,这不是很优雅。我有一个ArrayList。联系人是一个具有多个成员的VO类 - 名字,地区ID。有ArrayList中,因为不同的地区出现多​​次重复。该名单是由ID进行排序。下面是一个例子:

Okay at first I thought this would be pretty straightforward. But I can't think of an efficient way to solve this. I figured a brute force way to solve this but that's not very elegant. I have an ArrayList. Contacts is a VO class that has multiple members - name, regions, id. There are duplicates in ArrayList because different regions appear multiple times. The list is sorted by ID. Here is an example:

输入0 - 名称:约翰·史密斯;地区:N; ID:1结果
进入1 - 产品名称:约翰·史密斯;地区:MW; ID:1结果
进入2 - 产品名称:约翰·史密斯;地区:S; ID:1结果
进入3 - 产品名称:李四;地区:NULL; ID:2结果
第4项 - 姓名:杰克·布莱克;地区:N; ID:3结果
进入6 - 产品名称:杰克·布莱克;地区:MW; ID:3结果
进入7 - 产品名称:乔唐;地区:东北; ID:4

Entry 0 - Name: John Smith; Region: N; ID: 1
Entry 1 - Name: John Smith; Region: MW; ID: 1
Entry 2 - Name: John Smith; Region: S; ID: 1
Entry 3 - Name: Jane Doe; Region: NULL; ID: 2
Entry 4 - Name: Jack Black; Region: N; ID: 3
Entry 6 - Name: Jack Black; Region: MW; ID: 3
Entry 7 - Name: Joe Don; Region: NE; ID: 4

我想重复的地区,共同为同一ID合并到列表中转化为下方。因此,最终的列表应该只有4结合的区域不同的元素。

I want to transform the list to below by combining duplicate regions together for the same ID. Therefore, the final list should have only 4 distinct elements with the regions combined.

所以输出应该是这样的: -

So the output should look like this:-

输入0 - 名称:约翰·史密斯;地区:N,MW,S; ID:1结果
进入1 - 产品名称:李四;地区:NULL; ID:2结果
进入2 - 姓名:杰克·布莱克;地区:N,MW; ID:3结果
进入3 - 产品名称:乔唐;地区:东北; ID:4

Entry 0 - Name: John Smith; Region: N,MW,S; ID: 1
Entry 1 - Name: Jane Doe; Region: NULL; ID: 2
Entry 2 - Name: Jack Black; Region: N,MW; ID: 3
Entry 3 - Name: Joe Don; Region: NE; ID: 4

你对解决这个最佳的方式的想法?我不是在寻找实际的code,但想法或提示,去最好的方式来完成它。

What are your thoughts on the optimal way to solve this? I am not looking for actual code but ideas or tips to go about the best way to get it done.

感谢您的时间!

推荐答案

您可以遍历他们,而他们倾倒(与合并复制)到一个TreeMap的。然后,从树形图的价值观的排序视图创建列表。

You can iterate them while dumping them (and merging duplicates) into a TreeMap. Then create a list from the sorted view of the TreeMap's values.

在样品code我假设你已经有ID,名称和地区条目类字段这最后一个是区域实例的列表。这很容易被更改为设置和区域为字符串或任何你正在使用。样本复制项之前,将它们插入到地图上,因为当合并其他条目,他们将被修改。

In the sample code I'm assuming you have an Entry class with id, name and regions fields this last one being a List of Region instances. This could easily be changed to a Set, and Region to Strings or whatever you're using. The sample copies the entries before inserting them into the map since they will be modified when merged to other entries.

SortedMap<Integer, Entry> mergedEntriesMap = new TreeMap<Integer, Entry>();
for (Entry e : entries) {
  if (mergedEntriesMap.contains(e.id)) {
    Entry m = mergedEntriesMap.get(e);
    m.regions.addAll(e.regions);
  } else {
    Entry m = new Entry();
    // copy the entry to keep the original array clean
    m.id = e.id;
    m.name = e.name;
    m.regions = new ArrayList<Region>(e.regions);
    mergedEntriesMap.put(m.id, m);
  }
}

List<Entry> mergedEntries = new ArrayList<Entry>(mergedEntriesMap.values());

这篇关于从排序的ArrayList中删除重复项,同时保持某些元素从重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆