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

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

问题描述

好吧,起初我认为这会很简单.但我想不出一个有效的方法来解决这个问题.我想出了一个蛮力的方法来解决这个问题,但这不是很优雅.我有一个 ArrayList.Contacts 是一个 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 - 姓名:John Smith;区域:N;编号:1
条目 1 - 姓名:John Smith;地区:兆瓦;编号:1
条目 2 - 姓名:John Smith;区域:S;编号:1
条目 3 - 姓名:Jane Doe;区域:NULL;编号:2
条目 4 - 姓名:杰克·布莱克;区域:N;编号:3
条目 6 - 姓名:杰克·布莱克;地区:兆瓦;编号:3
条目 7 - 姓名:Joe Don;地区:东北;编号: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 - 姓名:John Smith;区域:N、MW、S;编号:1
条目 1 - 姓名:Jane Doe;区域:NULL;编号:2
条目 2 - 姓名:杰克·布莱克;区域:N,MW;编号:3
条目 3 - 姓名:Joe Don;地区:东北;编号: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

您对解决此问题的最佳方法有何看法?我不是在寻找实际的代码,而是在寻找实现它的最佳方式的想法或技巧.

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 时迭代它们.然后从 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.

在示例代码中,我假设您有一个包含 ID、名称和区域字段的 Entry 类,最后一个是区域实例列表.这可以很容易地更改为 Set,将 Region 更改为 Strings 或您正在使用的任何内容.该示例在将条目插入地图之前复制条目,因为它们在合并到其他条目时会被修改.

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天全站免登陆