Results 1 to 12 of 12
  1. #1
    guywalder is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default HashSet Contains problem

    Hi every body.
    Sorry in advance for any misstypes my english is not perfect.
    I'm having a problem using the HashSet contains method.
    For some reason the method is not working right.
    I have a NewsChannel class that has a HashSet of TVProgram objects.
    I've overwritten the equals and the hashcode methods.
    When I check in the MainClass if two TVProgram objects are equal the output is true. and the hash code is the same. But after a add a program to the hashset and check if the same object exsits with the contains method the output is false.
    This is the equal an the hashcode in the TVProgram Class:

    public boolean equals(Object obj) {
    if(obj==null||this.getClass()!=obj.getClass())
    return false;
    TVProgram other = (TVProgram) obj;
    return this.getProgName().equals(other.getProgName())&&
    this.getProgSerial().equals(other.getProgSerial()) ;
    }

    public int hashcode(){
    return this.getProgSerial().hashCode() + this.getProgName().hashCode();
    }

    this is the MainClass:

    NewsChannel news1 = new NewsChannel("Fox News", "PAL", "USA","14/04/1956", 30, 52);
    TVProgram p1 = new TVProgram(1111,"5 oclock","Normal",'y');
    news1.addProgram(new TVProgram(1111,"5 oclock","Normal",'y'));

    System.out.println(p1.hashcode());
    System.out.println((new TVProgram(1111,"5 oclock","Normal",'y').hashcode()));
    System.out.println(p1.equals(new TVProgram(1111,"5 oclock","Normal",'y')));
    System.out.println(news1.getPrograms().contains(p1 ));

    Output:

    -512393311
    -512393311
    true
    false
    Please tell me what am I doing wrong?????

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Send the code ...Some confusion with ur flow or else it would have worked.

    Send the code with code tag
    Ramya:cool:

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You will need to show how put the p1 in news1.getPrograms() (if at all).

    P.S Why not use instanceof test instead of class tests? That way subclasses of TVProgram can still use your equals.

  4. #4
    guywalder is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    I'm using the contains method in an addTVProgram method in the NewsChannel class. I need to check if a program is already in the HashSet each time I'm adding a new program so i wont have doubles or run over an existing program. So I cant use the instanceOf Method.
    Ramya what part of the code you need? I added the parts that I think are relevant.
    Still no solution...
    Thanks 4 your help.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You can still use the instanceof to replace the parts that are doing getClass() comparisons. The rest of your equals method would stay the same.
    Checking for contains in a HashSet is rather redundant. You can't have duplicates in a Set any way.
    The problem is probably that you never added the TVProgram into the set in the first place that's why the contains is always returning false. Post that code that adds the TVProgram that you are checking against.

  6. #6
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Any security issues to send the code?

    Please send the code so that it will help us to guide you.
    Ramya:cool:

  7. #7
    guywalder is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    I did add the program to the list and when I printed the news1 object the programs HashSet is printed as well including the program added.
    This is the addProgram method from the NewsChannel Class:

    public boolean addProgram(TVProgram program){
    if(program!=null && !this.programs.contains(program))
    return this.programs.add(program);
    return false;
    }

    Ramya I didnt know what part do you need so i added the entire projet in a zip file attached to the post.
    Attached Files Attached Files

  8. #8
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Java Code:
    	public int hashcode(){
    		return this.getProgSerial().hashCode() + this.getProgName().hashCode();
    	}
    should be
    Java Code:
    	@Override
    	public int hashCode() {
    		return this.getProgSerial().hashCode() + this.getProgName().hashCode();
    	}
    Notice the capitalisation (which you had right inside the method, but not in the method declaration).

    If you use the override annotation you won't make these types of mistakes.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    The code you posted above doesn't even compile.

    You also seem to have ignored the part of my post which said
    "Checking for contains in a HashSet is rather redundant. You can't have duplicates in a Set any way."

  10. #10
    guywalder is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    I can't belive it.... :)
    Thank you masijade you where right. I cant belive that i spent 4 hours just to find out that I wrote the Method hashCode with a small c and ther for did'nt over write it.
    Thanx to every one how helped.
    Guy.

  11. #11
    guywalder is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    r035198x you are right but you can over right an existing one.
    Thank you for your help due.

  12. #12
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by guywalder View Post
    r035198x you are right but you can over right an existing one.
    Thank you for your help due.
    If by this, you mean by calling add(Object) will overwrite (not over right) the existing "Object", if it exists. You're wrong. the add method returns a boolean, true if the Object is successfully added, false if the Object already exists and so cannot be added (which, of course, implies that it does not "overwrite" anything).

Similar Threads

  1. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 08:22 PM
  2. Replies: 0
    Last Post: 10-15-2008, 05:45 PM
  3. HashSet and AbstractSet both Implements Set
    By rkjsohi2 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 11:04 AM
  4. Working with HashSet and TreeSet
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-15-2008, 07:34 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •