java – 从ArrayList捕获重复项

我在从ArrayList中删除重复对象时遇到问题.我将
XML解析为我称之为IssueFeed对象的东西.这包括症状,问题和解决方案.

我的大多数对象都是独一无二的,并没有共享症状,问题,解决方案,但有些共享相同的症状,但有不同的问题.

我想完成几件事.

>捕获与重复的Arraylist共享相同症状的对象
>从主列表中删除重复项目,至少留下1个带有该症状的项目.
>当用户点击我们知道重复的项目时,在我的listview / adapter中设置重复数据Arraylist.

我采取的步骤.

>我已经尝试对对象进行排序,我能够捕获重复项,但不知道如何从主列表中删除除一个以外的所有对象.
> 2在列表之间循环并查找不是自身和症状=症状的对象,然后删除并更新我的重复数组和主数组.

一些代码

IssueFeed – 对象

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;
}

我的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

我可以通过以下排序代码获取重复项.

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());
            }
        });

现在我需要创建新的非欺骗列表,这段代码只添加了所有对象. :/

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);
                    }
                }
            }
        }

最佳答案 如果您可以修改IssueFeed对象,请考虑覆盖equals()和hashCode()方法,并使用set来查找重复项.例如

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

class IssueFeed {
    private String symptom;
    private String problem;
    private String solution;

    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;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((symptom == null) ? 0 : symptom.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        IssueFeed other = (IssueFeed) obj;
        if (symptom == null) {
            if (other.symptom != null)
                return false;
        } else if (!symptom.equals(other.symptom))
            return false;
        return true;
    }
    @Override
    public String toString() {
        return "IssueFeed [symptom=" + symptom + ", problem=" + problem
                + ", solution=" + solution + "]";
    }
}

public class Sample {

    public static void main(String[] args) {
        List<IssueFeed> mainList = new ArrayList<IssueFeed>(
                Arrays.asList(new IssueFeed[] {
                        new IssueFeed("sym1", "p1", "s1"),
                        new IssueFeed("sym2", "p2", "s2"),
                        new IssueFeed("sym3", "p3", "s3"),
                        new IssueFeed("sym1", "p1", "s1") }));
        System.out.println("Initial List : " + mainList);
        Set<IssueFeed> list_of_non_dupes = new LinkedHashSet<IssueFeed>();
        List<IssueFeed> duplicateDatalist = new ArrayList<IssueFeed>(); 
        for(IssueFeed feed : mainList){
            if(!list_of_non_dupes.add(feed)) {
                duplicateDatalist.add(feed);
            }
        }
        mainList = new ArrayList<IssueFeed>(list_of_non_dupes); // Remove the duplicate items from the main list, leaving at least 1 item with that symptom to be display
        list_of_non_dupes.removeAll(duplicateDatalist); // list of only objects with unique symptom
        System.out.println("Fina main list : " + mainList);
        System.out.println("Unique symptom" + list_of_non_dupes);
        System.out.println("Duplicate symptom" + duplicateDatalist);
    }
}
点赞