This discussion is archived
4 Replies Latest reply: May 24, 2011 1:00 PM by 796440 RSS

Problem with implementation of hashcode with Set collection

Prem Newbie
Currently Being Moderated
Hi everyone ,

As we know that set is used to avoid duplicates. But what if you are adding some bean into set having same values. How can avoid those duplicates . I tried following but failed , Please help me out.


class Student
{

     private int id;
     private String fname;
     public int getId() {
          return id;
     }
     public void setId(int id) {
          this.id = id;
     }
     public String getFname() {
          return fname;
     }
     public void setFname(String fname) {
          this.fname = fname;
     }
     
     
     
     @Override
     public int hashCode() {
          
          System.out.println(this.id);
          return this.id;
     }
     
     @Override
     public String toString() {
          String s=""+this.id+"";
          return s;
     }
}

public class TestSet
{
     
     public static void main(String[] args) {
          
          HashSet tSet=new HashSet();
          Student s=new Student();
          s.setId(1);
          Student s1=new Student();
          s1.setId(2);
          Student s2=new Student();
          s2.setId(1);
          
          tSet.add(s);
          tSet.add(s1);
          tSet.add(s2);          
                    
          
          
          for(Object o:tSet)
          {
               System.out.println("-->"+o);
          }
          
          
          
          
     }


I mean to say that, I am not getting where should I keep check whether there is duplicate element or not.
  • 1. Re: Problem with implementation of hashcode with Set collection
    DarrylBurke Guru Moderator
    Currently Being Moderated
    Moderator advice: Please read the announcement(s) at the top of the forum listings and the FAQ linked from every page. They are there for a purpose.

    Then edit your post and format the code correctly.

    db
  • 2. Re: Problem with implementation of hashcode with Set collection
    jtahlborn Expert
    Currently Being Moderated
    equality for a HashSet is controlled via the hashCode() and equals() methods on the objects you add to it. make sure yours are implemented appropriately.
  • 3. Re: Problem with implementation of hashcode with Set collection
    Prem Newbie
    Currently Being Moderated
    jtahlborn wrote:
    equality for a HashSet is controlled via the hashCode() and equals() methods on the objects you add to it. make sure yours are implemented appropriately.
    Could you please elaborate more by editing my code.... It will be very helpful.
  • 4. Re: Problem with implementation of hashcode with Set collection
    796440 Guru
    Currently Being Moderated
    Prem wrote:
    jtahlborn wrote:
    equality for a HashSet is controlled via the hashCode() and equals() methods on the objects you add to it. make sure yours are implemented appropriately.
    Could you please elaborate more by editing my code.... It will be very helpful.
    No, it doesn't work that way, and it won't be helpful. Just doing your work for you doesn't help you. The idea is to give you the tools to solve your own problem--to nudge you just enough to get you over the hump. You still have to do the hard work, and the boring, tedious work as well.

    You need to take a step back and think for a bit.

    You know that a Set will not allow two elements that are equal by the equals() method. You say that you want to add two elements that have "the same values". So according to your business logic, are those two objects "equal"? If so, then why are you using a Set in the first place? If they're not "equal" according to your business logic, then your equals() method should not just blindly compare all their fields. That's a common equals() approach, but there's no law that says equals() has to be written that way.

    So:
    1) What does it mean for two of your objects to be equal? Write equals() to match that.

    2) If you want to insert two objects that are "equal" by the above into a collection, then, by definition, a Set is not appropriate here.

    If you want more info on implementing equals and hashcode, see here: http://java.sun.com/developer/Books/effectivejava/Chapter3.pdf or google for examples.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points