Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 阵列列表比较_Java - Fatal编程技术网

Java 阵列列表比较

Java 阵列列表比较,java,Java,我有一个由类和对象组成的ArrayList。这个类有几个字符串字段。在某些班级 必须从ArrayList中删除一些相同的字段 我需要检查的类中的字段是按参与者排序的,在某些对象中设置为相同 这是我的班级: public class Neo4jCompensation { private String number_of_participants; private String amount; private String sorted_participants; priv

我有一个由类和对象组成的ArrayList。这个类有几个字符串字段。在某些班级 必须从ArrayList中删除一些相同的字段

我需要检查的类中的字段是按参与者排序的,在某些对象中设置为相同

这是我的班级:

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 results\u list=new ArrayList();
我认为找到重复项的很好的方法是复制一个列表,比较两个相同的类字段值,然后删除重复项。 这就是我找到复制品的方法

 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 results_list1=new ArrayList();
对于(NEO4J补偿pp:结果列表)
{
Neo4jCompensation ss=新的Neo4jCompensation();
ss.setAmount(pp.getAmount());
ss.setHref_pdf_compensation(pp.getHref_pdf_compensation());
ss.setNumber of_参与者(pp.getNumber of_参与者());
ss.setSigned_by_all(pp.getSigned_by_all());
ss.setSorted_参与者(第getSorted_参与者()页);
ss.setUnsorted_参与者(pp.getUnsorted_参与者());
结果1.添加(ss);
}
对于(int i=0;i”);
}
}
}
}
因为我知道我不能同时循环和删除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++;
 }
intone=0;
int 2=0;
迭代器it=results_list.Iterator();
while(it.hasNext()){
Neo4jCompensation kk=it.next();
迭代器it1=results_list1.Iterator();
while(it1.hasNext()){
Neo4jCompensation kk1=it1.next();
字符串oo=kk.getSorted_participants().trim();
字符串pp=kk1.getSorted_participants().trim();

如果(一个为什么不使用
.removeAll()

这将要求您在
Neo4jCompensation

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


}

另一种办法可以是:

在Neo4J补偿中重写equals方法,检查已排序的参与者并相应返回。然后使用Set作为对象的集合。Set不允许重复,它使用equals确定相等性。

for(int i=0;ifor (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(results_list1.contains(kk) { results_list1.remove(j); }
}
Neo4jCompensation kk=新的Neo4jCompensation(); kk=结果列表。获取(i); 对于(int j=0;j

但是,您可能希望使用a而不是a,或者使用一个数据结构来维护顺序,例如使用sentinel值的a。

不是整个类是重复的,而是一个类字段成员,其他字段可能不同。
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(results_list1.contains(kk) { results_list1.remove(j); }
}