Reputation: 20916
public void removeDuplicates (ArrayList<ArrayList<String>> strings) {
Set<ArrayList<String>> s = new LinkedHashSet<ArrayList<String>>(strings);
strings = new ArrayList<ArrayList<String>>(s);
}
i want to remove duplicated lines in ArrayList<ArrayList<String>>
, I want to use LinkedHashSet
and compare ArrayList
between themselves and if is the same not insert it. i know i need Comparator
but i do not know how to implement it for comparing ArrayList
inside ArrayList
.
Thx
Upvotes: 2
Views: 899
Reputation: 2075
Agree with @Leni above, just override ArrayLists's equals method and the LinkedHashSet implementation should automatically filter duplicates.
From Java doc: A collection that contains no duplicate elements. More formally, sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element. As implied by its name, this interface models the mathematical set abstraction
Upvotes: 0
Reputation: 40176
You didn't mention if the order is important.
If the item order in the inner list is not important, this should work:-
List<List<String>> list = new ArrayList<List<String>>();
list.add(Arrays.asList(new String[] {
"a",
"b",
"c"
}));
list.add(Arrays.asList(new String[] {
"b",
"a",
"c"
}));
list.add(Arrays.asList(new String[] {
"a",
"b",
"c",
"d"
}));
// use set to remove duplicates
Set<Set<String>> set = new HashSet<Set<String>>();
for (List<String> innerList : list) {
set.add(new HashSet<String>(innerList));
}
// convert back to list
List<List<String>> noDupList = new ArrayList<List<String>>();
for (Set<String> innerSet : set) {
noDupList.add(new ArrayList<String>(innerSet));
}
// print out for debugging
for (List<String> l : noDupList) {
System.out.println(l);
}
Upvotes: 1
Reputation: 18354
Say the list contains
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
What do you expect the set to contain? Two elements? One element?
Can you tell how you want to compare the ArrayLists?
If that is the one-to-one compare - "they are equal if they contain the same elements in the same order" then
sets = new HashSet<ArrayList<String>>(lists);
Should be sufficient.
If you want to have more complex comparison rules then you need to override the equals method as Leni Kirilov said above. But the larger the list is, you will have a high performance overhead.
Upvotes: 0
Reputation: 1317
You could try using a Set< List > - and if you implement your own List or extend ArrayList os that the "equals()" acts the way you expect.
So when you add an arrayList it will automatically be compared with the rest.
Not sure what will happen when you add the arrayLists first and then populate them with elements.
What is your case?
You said you want to use LinkedHashSet - ok, that is the same as my initial suggestion. (you don't need comparator) You can extend ArrayList and make its "equals()" method conform your wish:
Upvotes: 4
Reputation: 55770
You are going to have to do a brute force compare and compare each and every arraylist against every other array list.
start with index 0 and compare it's elements against index 1-n once you find an
Assumeing ArrayList myDatastructure following psuedo code:
for (int i =0; i < myDatastructure.count() ; i++){
for (int j = i+1 ; i< mydatastructure.count() ; j++){
compare(myDatastructure.get(i),myDataStructure.get(j));
}
}
for the compare method you will want to write a for loop that iterates through the elements one by one comparing the item at each index in both arrayLists, you will want to short circuit this by not bothering to compare them if they are not the same length.
You will probably want to mark the indexes that you want to remove in an Separate arraylist and remove them in a separate loop otherwise you will screw up your indices.
Implementation should be fairly simple, so that's left as an exercise.
Upvotes: 0