gpt4 book ai didi

java - 数组列表比较

转载 作者:行者123 更新时间:2023-12-01 04:39:55 25 4
gpt4 key购买 nike

我有一个由类对象组成的ArrayList。该类有几个字符串字段。在某些类(class)必须从 ArrayList 中删除一些相同的字段。

我需要检查的类中的字段是sorted_pa​​rticipants,它在某些对象中设置为相同。

这是我的类(class):

public class Neo4jCompensation {
private String number_of_participants;
private String amount;
private String sorted_participants;
private String unsorted_participants;
private String href_pdf_compensation;
private String signed_by_all;

public Neo4jCompensation() {
this.number_of_participants = "";
this.amount = "";
this.sorted_participants = "";
this.unsorted_participants = "";
this.href_pdf_compensation = "";
this.signed_by_all = "";
}



public String getNumber_of_participants() {
return number_of_participants;
}

public void setNumber_of_participants(String number_of_participants) {
this.number_of_participants = number_of_participants;
}

public String getAmount() {
return amount;
}

public void setAmount(String amount) {
this.amount = amount;
}

public String getSorted_participants() {
return sorted_participants;
}

public void setSorted_participants(String sorted_participants) {
this.sorted_participants = sorted_participants;
}

public String getUnsorted_participants() {
return unsorted_participants;
}

public void setUnsorted_participants(String unsorted_participants) {
this.unsorted_participants = unsorted_participants;
}

public String getHref_pdf_compensation() {
return href_pdf_compensation;
}

public void setHref_pdf_compensation(String href_pdf_compensation) {
this.href_pdf_compensation = href_pdf_compensation;
}

public String getSigned_by_all() {
return signed_by_all;
}

public void setSigned_by_all(String signed_by_all) {
this.signed_by_all = signed_by_all;
}


}

所以我有一个包含类的第一个列表:

 ArrayList<Neo4jCompensation> results_list=new ArrayList<Neo4jCompensation>();

我认为查找重复项的好方法是复制一个列表,比较两个列表的相同类字段值并删除重复项。这就是我找到重复项的方法

 ArrayList<Neo4jCompensation> results_list1=new ArrayList<Neo4jCompensation>();

for(Neo4jCompensation pp:results_list)
{

Neo4jCompensation ss=new Neo4jCompensation();
ss.setAmount(pp.getAmount());
ss.setHref_pdf_compensation(pp.getHref_pdf_compensation());
ss.setNumber_of_participants(pp.getNumber_of_participants());
ss.setSigned_by_all(pp.getSigned_by_all());
ss.setSorted_participants(pp.getSorted_participants());
ss.setUnsorted_participants(pp.getUnsorted_participants());
results_list1.add(ss);
}

for (int i = 0; i < results_list.size(); i++) {
Neo4jCompensation kk=new Neo4jCompensation();
kk=results_list.get(i);

for (int j = 0; j < results_list1.size(); j++) {
Neo4jCompensation n2=new Neo4jCompensation();
n2=results_list1.get(j);
if(i!=j)
{
String prvi=kk.getSorted_participants().trim();
String drugi=n2.getSorted_participants().trim();
if(prvi.equals(drugi))
{

// results_list1.remove(j);
out.println("<p> Are equal su :"+i+" i "+j+"</p>");
}
}

}

}

因为我知道我不能同时循环并从 ArrayList 中删除元素我尝试使用这样的迭代器的时候......

int one=0;
int two=0;

Iterator<Neo4jCompensation> it = results_list.iterator();
while (it.hasNext()) {
Neo4jCompensation kk=it.next();

Iterator<Neo4jCompensation> it1 = results_list1.iterator();
while (it1.hasNext()) {

Neo4jCompensation kk1=it1.next();
String oo=kk.getSorted_participants().trim();
String pp=kk1.getSorted_participants().trim();
if(one<two && oo.equals(pp))
{
it1.remove();
}
two++;
}
one++;
}

但它失败了,并且在 ArrayList results_list1 中没有返回任何内容 - 在使用迭代器删除之前,它包含正确的元素。如何从数组列表中删除与 ArrayList 中其他对象具有相同字段值的对象。

最佳答案

为什么不使用.removeAll()

results_list1.removeAll(resultList);

这将要求您在 Neo4jCompensation 中实现 equalshashcode

public class Neo4jCompensation {

//Omitted Code

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime
* result
+ ((number_of_participants == null) ? 0
: number_of_participants.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;
Neo4jCompensation other = (Neo4jCompensation) obj;
if (number_of_participants == null) {
if (other.number_of_participants != null)
return false;
} else if (!number_of_participants.equals(other.number_of_participants))
return false;
return true;
}


}

关于java - 数组列表比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16726075/

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