find in a hashSet

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mickey0
    New Member
    • Jan 2008
    • 142

    find in a hashSet

    Hello,
    I'm new to java and my problem is this:
    Code:
    class Word {
        private String _value;
        public Word(String value) { _value = new String(value); }
    }
    
    
    //main
    HashSet< Word >  dictionary = new HashSet< Word >();
    dictionary.add( new Word( "goodbye" );
    dictionary.add( new Word( "hi" );
    dictionary.add( new Word( "hello" );
    
    if ( dictionary.contains( Word( "hello" ) )) ) { //how to to this?
    
    }
    I need to know if dictionary contains that word. I know it's simple but I can't do it

    Any hints, please?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Check out this website...looks like you are doing it just fine. HashSets have a .contains(Objec t) method that returns true if the HashSet has your word, false otherwise.

    If it's not working for you, check your Word's .hashCode() function - it might not be returning unique integers for each Word, or it might be generating different integers for the same Words.

    Comment

    • mickey0
      New Member
      • Jan 2008
      • 142

      #3
      sorry but that line isn't correct at all. I don't know how write that thing...

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        Try:

        Code:
        if (dictionary.contains(new Word("Hello"))) {
           System.out.println("Found Word: \"Hello\"");
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Read the API specs for any class that you don't understand. That contains method for example, will only find that value if there a value in the set such that the equals method returns true for that object.

          Comment

          • mickey0
            New Member
            • Jan 2008
            • 142

            #6
            nothing: it doesn't find the Word("hello") inside the dictionary.
            Besides,I notice that if I do:
            Code:
            dictionary.add( new Word( "hello" );
            dictionary.add( new Word( "hello" );
            it adds to word("hello") twice.
            I'd like no copies inside that. I chose HashSet because it doens't keep copies. it worked fine when I had HashSet<String> , but now it doesn't work anything....

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              The answer lies in my post above which you have ignored.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                To 3xpand on r0's example, you haven't overloaded the .equals() or the .hashCode() functions in your Word class, and so you can't expect the HashSet object to know how to use them.

                By default, an object equals another object if and only if they are the same object: that is, two instances of "Hello" are two separate Words and are not equal.

                In addition, two instances of "Hello" will produce two different hash codes because .hashCode() generates a code based on the object's location in memory by default.

                Overload these two methods to get the expected functionality in your program.

                Comment

                • Nepomuk
                  Recognized Expert Specialist
                  • Aug 2007
                  • 3111

                  #9
                  To find out more about that, this article by r035198x should help you understand what you have to do and how to do it.

                  Greetings,
                  Nepomuk

                  Comment

                  Working...