更新时间:2023-12-04 21:41:04
列表<名单,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);
}