Results 1 to 4 of 4

Thread: [java] finding in a HashSet

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default [java] finding in a HashSet

    Hello,
    I'm new to java and my problem is this:
    Qt Code:
    1. class Word {
    2. private String _value;
    3. public Word(String value) { _value = new String(value); }
    4. }
    5.  
    6.  
    7. //main
    8. HashSet< Word > dictionary = new HashSet< Word >();
    9. dictionary.add( new Word( "goodbye" );
    10. dictionary.add( new Word( "hi" );
    11. dictionary.add( new Word( "hello" );
    12.  
    13. if ( dictionary.contains( Word( "hello" ) )) ) { //how to to this?
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    I need to know if dictionary contains that word. I know it's simple but I can't do it

    Any hints, please?
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [java] finding in a HashSet

    What exactly is the problem? As I see the class has a "contains" method.

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: [java] finding in a HashSet

    apart that maybye I forget a "new Word( "hello" )" (because without new it doens't compile), it's never true: it don't find "hello"...

    EDIT:
    furtheremore:
    I notice that if I do:
    Qt Code:
    1. dictionary.add( new Word( "hello" );
    2. dictionary.add( new Word( "hello" );
    To copy to clipboard, switch view to plain text mode 
    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....
    Last edited by mickey; 17th November 2008 at 13:15.
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [java] finding in a HashSet

    Did you implement the comparison operator for your class?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.