0

I have a set of this structure. How to remove duplicates of equal object of that class? Equals means field File plik is the same.

EDIT: But the problem gut bigger I don't have duplicates but I would like to replace old SET member by new.

Withoud 3rd party libraries.

import java.io.*;

public class WordInfo implements Serializable {
    File plik;
    Integer wystapienia;

    public WordInfo(File plik, Integer wystapienia) {
        this.plik = plik;
        this.wystapienia = wystapienia;
    }

    public String toString() {
    //  if (plik.getAbsolutePath().contains("src") && wystapienia != 0)
            return plik.getAbsolutePath() + "\tWYSTAPIEN " + wystapienia;
    //  return "";
    }

}

EDIT

I don't have this HashCodeBuilder I want to use Java standard libraries

 public int hashCode() {
            return new HashCodeBuilder(17, 31).append(plik).append(wystapienia).toHashCode();
        }

    public boolean equals(Object obj) {
        File f = (File) obj;
        return(plik.getAbsoluteFile().equals(f.getAbsolutePath()));
    }
Yoda
  • 15,011
  • 59
  • 173
  • 291
  • 3
    override `equals` and `hashcode`...a set automatically disallows duplicates. – mre Nov 19 '12 at 03:46
  • You don't have to use Apache's HashCodeBuilder to implement hashCode. You can probably use the hashCode method implemented by `File`, assuming there is one. It should be very easy to check... And two seconds after I typed this, @BheshGurung has provided an answer that demonstrates exactly how! – jahroy Nov 19 '12 at 03:57
  • @mre please look at edit – Yoda Nov 19 '12 at 03:57

2 Answers2

3

As discussed here, override equals.

public class Person {
    private String name;
    private int age;
    // ...

    public int hashCode() {
        return new HashCodeBuilder(17, 31). // two randomly chosen prime numbers
            // if deriving: appendSuper(super.hashCode()).
            append(name).
            append(age).
            toHashCode();
    }

    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (obj == this)
            return true;
        if (obj.getClass() != getClass())
            return false;

        Person rhs = (Person) obj;
        return new EqualsBuilder().
            // if deriving: appendSuper(super.equals(obj)).
            append(name, rhs.name).
            append(age, rhs.age).
            isEquals();
    }
}
Community
  • 1
  • 1
ian.shaun.thomas
  • 3,349
  • 23
  • 39
  • what are these methods appedn they are not implemented or even declared – Yoda Nov 19 '12 at 03:50
  • The class HashCodeBuilder does not exist – Yoda Nov 19 '12 at 03:51
  • @RobertKilar - They are from the _Apache Commons Lang_ library (see the link in the answer). The point tencent is trying to make is that a Set (by definition) does not contain duplicates. In order for a Set to work with a custom object, you must implement _equals_ and _hashCode_ for your class. – jahroy Nov 19 '12 at 03:52
  • The OP has defined a very simple requirement for hashCode... there's really no need to use _HashCodeBuilder_ when you can just use _File.hashCode._. – jahroy Nov 19 '12 at 04:03
  • You are missing the point. The point was this question has been asked and the OP can use the the thread I linked and the source I lifted from the original thread to answer the question. I did not see value in writing the code for the op as it would take away the learning experience. – ian.shaun.thomas Nov 19 '12 at 13:04
2
public class WordInfo implements Serializable {
    File plik;

Considering that following is how you can override the equals and hashCode method as per your requirement:

@Override
public boolean equals(Object obj) {
    if(this == obj) return true;
    if(!(obj instanceof WordInfo)) return false;
    return this.plik.equals(((WordInfo) obj).plik);
}

@Override
public int hashCode() {        
    return this.plik.hashCode();
}
Bhesh Gurung
  • 48,464
  • 20
  • 87
  • 139