-2

I have the following simple java program to compare two objects in list.

   public static void main( String[] args )
    {

         UserInfo user=new UserInfo();

         user.setDomainId(2);
         user.setId("sxpadmin");
         user.setStatus("active");



         UserInfo user1=new UserInfo();

         user1.setDomainId(2);
         user1.setId("sxpadmin");
         user1.setStatus("active");

         System.out.println(user.equals(user1));

        List<UserInfo> userinfo=new ArrayList<UserInfo>();

        userinfo.add(user);
        userinfo.add(user1);

        HashSet<UserInfo> set = new HashSet<UserInfo>();

        for (UserInfo temp : userinfo)
        {
            if(set.contains(temp)){
                System.out.println("same");
            }
            else{
                System.out.println("different");
                set.add(temp);
            }
        }
    }

Now I am comparing the two objects and it should take to if block as the content in both the objects is same.

I am iterating the userinfo object and comapring its elements and also I am adding it to set hoping to avoid the duplicates.But none of them worked. Help me in solving this.

Hashcode and equals methods in UserInfo are

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + customer_id;
    result = prime * result
            + ((domainId == null) ? 0 : domainId.hashCode());
    result = prime * result
            + ((last_name == null) ? 0 : last_name.hashCode());
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result
            + ((first_name == null) ? 0 : first_name.hashCode());
    // Added by Sandip on 04 Jan 2013 for 2 FA
    result = prime * result
            + ((seed_value == null) ? 0 : seed_value.hashCode());
    // End added by Sandip on 04 Jan 2013 for 2 FA
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    UserInfo other = (UserInfo) obj;
    if (customer_id != other.customer_id)
        return false;
    if (last_name == null) {
        if (other.last_name != null)
            return false;
    } else if (!last_name.equals(other.last_name))
        return false;
    if (id == null) {
        if (other.id != null)
            return false;
    } else if (!id.equals(other.id))
        return false;
    if (first_name == null) {
        if (other.first_name != null)
            return false;
    } else if (!first_name.equals(other.first_name))
        return false;
    // Added by Sandip on 04 Jan 2013 for 2 FA
    if (seed_value == null) {
        if (other.seed_value != null)
            return false;
    } else if (!seed_value.equals(other.seed_value))
        return false;
    // End added by Sandip on 04 Jan 2013 for 2 FA
    if (domainId == null) {
        if (other.domainId != null)
            return false;
    } else if (!domainId.equals(other.domainId))
        return false;

    return true;
} 
Saha
  • 307
  • 2
  • 5
  • 16

1 Answers1

0

If you want to create working solution here's what you have to override equals and hashCode method. Many IDE's have feature to autogenerate those methods for chosen class. Following code of UserInfo shows methods generated by IntelliJ:

public class UserInfo {
    private int domainId;
    private String id;
    private String status;

    public void setDomainId(int domainId) {
        this.domainId = domainId;
    }

    public void setId(String id) {
        this.id = id;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getStatus() {
        return status;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserInfo userInfo = (UserInfo) o;

        if (domainId != userInfo.domainId) return false;
        if (id != null ? !id.equals(userInfo.id) : userInfo.id != null) return false;
        if (status != null ? !status.equals(userInfo.status) : userInfo.status != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = domainId;
        result = 31 * result + (id != null ? id.hashCode() : 0);
        result = 31 * result + (status != null ? status.hashCode() : 0);
        return result;
    }

}

It's important to remember that if certain class doesn't implement those methods then hashCode is returning hash of objects address in memory and equals is using this default version of hashCode.

Code pasted above is working with code pasted by you. My output is:

true
different
same