I'm having issues with removing duplicate objects from an ArrayList. Im parsing XML into what i call an IssueFeed object. This consists of a symptom, problem, solution.
Most of my objects are unique and don't share a symptom, problem, solution but some share the same symptom but have a different problem.
Im trying to accomplish several things.
- Capture objects that share the same symptom as a duplicate Arraylist
- Remove the duplicate items from the main list, leaving at least 1 item with that symptom to be displayed.
- When the user clicks on the item that we know has duplicates, set the duplicate data Arraylist in my listview/adapter.
Steps i've taken.
- I've tried sorting the objects and i am able to capture the duplicates, however not sure how to remove all but one from the main list.
- 2 Loops between the list and looking for objects that aren't themselves and symptom = symptom and then remove and update my duplicate array and main array.
Some code
IssueFeed - object
public IssueFeed(String symptom, String problem, String solution) {
this.symptom = symptom;
this.problem = problem;
this.solution = solution;
}
public String getSymptom() {
return symptom;
}
public String getProblem() {
return problem;
}
public String getSolution() {
return solution;
}
My ArrayList<IssueFeed>'s
duplicateDatalist = new ArrayList<IssueFeed>(); // list of objects thats share a symptom
list_of_non_dupes = new ArrayList<IssueFeed>(); // list of only objects with unique symptom
mIssueList = mIssueParser.parseLocally(params[0]); // returns ArrayList<IssueFeed> of all objects
I can obtain the duplicates by the following sort code below.
Collections.sort(mIssueList, new Comparator<IssueFeed>(){
public int compare(IssueFeed s1, IssueFeed s2) {
if (s1.getSymptom().matches(s2.getSymptom())) {
if (!duplicateDatalist.contains(s1)) {
duplicateDatalist.add(s1);
System.out.print("Dupe s1 added" + " " + s1.getSymptom() + ", " + s1.getProblem() + "\n");
}
if (!duplicateDatalist.contains(s2)) {
duplicateDatalist.add(s2);
System.out.print("Dupe s2 added" + " " + s2.getSymptom() + ", " + s2.getProblem() + "\n");
}
}
return s1.getSymptom().compareToIgnoreCase(s2.getSymptom());
}
});
Now i need to create the new list of non dupes, This code only added all of the objects. :/
for (int j = 0; j < mIssueList.size(); j++) {
IssueFeed obj = mIssueList.get(j);
for (int i = 0; i < mIssueList.size(); i++) {
IssueFeed obj_two = mIssueList.get(j);
if (obj.getSymptom().matches(obj_two.getSymptom())) {
if (!list_non_dupes.contains(obj_two)) {
list_non_dupes.add(obj_two);
}
break;
} else {
if (!list_non_dupes.contains(obj_two)) {
list_non_dupes.add(obj_two);
}
}
}
}