从列表和保留列表列表中删除重复项 [英] removing duplicates from list of lists and preserving lists

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

问题描述

我有一个由arrayLists 组成的arrayList.每个内部数组列表都包含一些格式为 (name.version) 的对象.

I have an arrayList of arrayLists. Each inner arraylist contains some objects with the format (name.version) .

{  {a.1,b.2,c.3} , {a.2,d.1,e.1} , {b.3,f.1,z.1}....} 

例如 a.1 意味着 name = a 并且 version 是 1.

For example a.1 implies name = a and version is 1.

所以我想消除这个列表数组列表中的重复项.对我来说,两个对象同名时是重复的

So i want to eliminate duplicates in this arraylist of lists. For me , two objects are duplicate when they have the same name

所以基本上我的输出应该是

So essentially my output should be

{ { a.1,b.2,c.3},{d.1,e.1} ,{f.1 ,z.1} }

请注意,我希望以完全相同的形式输出(也就是说,我不想要一个没有重复的列表)

Note that i want the output in the exact same form (That is , i dont want a single list with no duplicates)

有人可以为此提供最佳解决方案吗?

Can someone provide me with an optimal solution for this?

我可以遍历每个内部列表并将内容放入哈希集中.但是有两个问题,我无法得到答案列表列表的形式.另一个问题是,当我需要覆盖该对象的 equals 时,但我不确定这是否会破解其他代码.如果它们的名称相同,这些对象就有意义地相等(仅在这种情况下.我不确定是否会覆盖整个频谱)

I can loop through each inner list and place the contents in the hashset. But two issues there, i cant get back the answer in form of list of lists.Another issue is that when i need to override equals for that object , but i am not sure if that would break other code. These objects are meaningfully equal if their names are same (only in this case. I am not sure that would cover the entire spectrum)

谢谢

推荐答案

List<List<Pair>> inputs; // in whatever format you have them
List<List<Pair>> uniqued = new ArrayList<>(); // output to here
Set<String> seen = new HashSet<String>();
for (List<Pair> list : inputs) {
  List<Pair> output = new ArrayList<>();
  for (Pair p : list)
    if (seen.add(p.getName()))
      output.add(p);
  uniqued.add(output);
}

这篇关于从列表和保留列表列表中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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