gpt4 book ai didi

java - 从 ArrayList 中捕获重复项

转载 作者:太空宇宙 更新时间:2023-11-03 10:20:01 28 4
gpt4 key购买 nike

我在从 ArrayList 中删除重复对象时遇到问题。我将 XML 解析为我所说的 IssueFeed 对象。这包括症状、问题、解决方案。

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

我试图完成几件事。

  1. 捕获与重复 Arraylist 具有相同症状的对象
  2. 从主列表中删除重复项,至少保留 1 个具有该症状的项进行显示。
  3. 当用户点击我们知道有重复的项目时,在我的 ListView /适配器中设置重复数据 Arraylist。

我采取的步骤。

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

一些代码

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>

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

我可以通过以下方式获得副本 sort下面的代码。

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() 方法并使用集合查找重复项.例如

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

关于java - 从 ArrayList 中捕获重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25044726/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com