Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Problem with equality check with HashSet

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Problem with equality check with HashSet

    below is my java program. Please tell me why i am getting the size of hashset as 2 instead of 1 as i have overridden the equals and hashcode method.


    import java.util.HashSet;
    import java.util.Set;


    public class EqualityCheck {
    String first = new String();
    String second = new String();
    /**
    * @param args
    */
    public static void main(String[] args) {
    // TODO Auto-generated method stub

    EqualityCheck t1 = new EqualityCheck();
    t1.first = "megha";
    t1.second = "jain";

    EqualityCheck t2 = new EqualityCheck();
    t2.first = "j megha";
    t2.second = "jain";

    if(t1.equals(t2)){
    System.out.println("t1 and t2 are equal");
    System.out.println("t1 hashcode"+t1.hashCode());
    System.out.println("t2 hashcode"+t2.hashCode());
    }
    Set<EqualityCheck> set1 = new HashSet<EqualityCheck>();
    set1.add(t1);
    set1.add(t2);
    set1.add(t1);
    System.out.println("set "+set1.toString()+"size = "+set1.size());

    }

    public boolean equals(EqualityCheck t){
    System.out.println("inside equals method");
    return true;
    }

    public int hashCode(){
    System.out.println("inside hashcode method");
    return 1111;
    }

    }

    this is the output i am getting while running this in eclipse.

    inside equals method
    t1 and t2 are equal
    inside hashcode method
    t1 hashcode1111
    inside hashcode method
    t2 hashcode1111
    inside hashcode method
    inside hashcode method
    inside hashcode method
    inside hashcode method
    inside hashcode method
    set [EqualityCheck@457, EqualityCheck@457]size = 2


  2. #2
    Junior Member
    Join Date
    Dec 2010
    Posts
    7
    My Mood
    Stressed
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Problem with equality check with HashSet

    The exact signature to override the equals method is as below:

    public boolean equals(Object obj){}

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem with equality check with HashSet

    Use the @Override statement to verify correct coding:
    Your code as is with the @Override added gives this compile error:

    EqualityCheck.java:35: method does not override or implement a method from a supertype

Similar Threads

  1. error in HashSet
    By harsh23 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2011, 09:18 AM
  2. Removing objects from a hashSet
    By JohnTor in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 03:03 PM
  3. Check this code out
    By jwill22 in forum Java Theory & Questions
    Replies: 4
    Last Post: October 11th, 2010, 08:34 PM
  4. how to check the value
    By javaking in forum Java Servlet
    Replies: 2
    Last Post: July 22nd, 2010, 06:56 AM
  5. Problem in AWT and IFrame implementaion
    By AZBOY2000 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: April 24th, 2009, 03:41 AM