从列表和preserving名单列表中删除重复项 [英] removing duplicates from list of lists and preserving lists
问题描述
我的ArrayLists的ArrayList。每个内ArrayList包含与格式(name.version)某些对象。
{{A.1,B.2,C.3},{A.2,D.1,E.1},{B.3,F.1, Z.1} ....}
例如A.1暗示的名称= A和版本为1。
所以我想消除这个ArrayList中列出的副本。对我来说,两个对象是重复的,当他们具有相同的名称
所以基本上我的输出应
{{A.1,B.2,C.3},{D.1,E.1},{F.1,Z.1}}
请注意,我想在完全相同的输出形式(也就是,我不想没有重复一个列表)
有人能为我提供这个最佳的解决方案?
我可以通过每一个内部列表循环,并将内容的HashSet的。但有两个问题,我不能取回答案
lists.Another问题的列表形式是,当我需要覆盖等于该对象,但如果那会我不知道
其他突破code。这些对象是有意义的,如果相等他们的名字相同(只有在这种情况下,我不知道那会
覆盖整个光谱)
感谢
列表<名单,LT;对GT&;>输入; //的任何格式,你有他们
清单<名单,LT;对GT&;> uniqued =新的ArrayList<>(); //输出到这里
SET<串GT;可见=新的HashSet<串GT;();
对于(列表<对GT&;清单:输入){
清单<对GT&;输出=新的ArrayList<>();
对(对P:名单)
如果(seen.add(p.getName()))
output.add(P);
uniqued.add(输出);
}
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}....}
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?
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)
Thanks
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);
}
这篇关于从列表和preserving名单列表中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!