1

クラス オブジェクトから作成された ArrayList があります。クラスにはいくつかの String フィールドがあります。一部のクラスでは、同じフィールドを ArrayList から削除する必要があります。

チェックする必要があるクラスのフィールドは、一部のオブジェクトで同じになるように設定されている sorted_pa​​rticipants です。

これは私のクラスです:

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

重複を見つける非常に良い方法は、リストのコピーを作成し、同じクラス フィールドの値について 2 つを比較し、重複を削除することだと思いました。これは私が重複を見つける方法です

 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 内の他のオブジェクトと同じフィールド値を持つオブジェクトを配列リストから削除する方法。

4

3 に答える 3

0

使わない理由.removeAll()

results_list1.removeAll(resultList);

これには、実装する必要がありequalsますhashcodeNeo4jCompensation

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


}
于 2013-05-24T00:39:54.407 に答える
0

別のアプローチは次のとおりです。

Neo4jCompensation で equals メソッドをオーバーライドし、sorted_pa​​rticipants を確認して、それに応じて返します。次に、Set をオブジェクトのコレクションとして使用します。Set は重複を許可せず、等しいかどうかを判断するために equals を使用します。

于 2013-05-24T00:43:35.573 に答える