473,287 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

find in a hashSet

mickey0
142 100+
Hello,
I'm new to java and my problem is this:
Expand|Select|Wrap|Line Numbers
  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. }
I need to know if dictionary contains that word. I know it's simple but I can't do it

Any hints, please?
Nov 17 '08 #1
8 6301
Ganon11
3,652 Expert 2GB
Check out this website...looks like you are doing it just fine. HashSets have a .contains(Object) 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.
Nov 17 '08 #2
mickey0
142 100+
sorry but that line isn't correct at all. I don't know how write that thing...
Nov 17 '08 #3
Ganon11
3,652 Expert 2GB
Try:

Expand|Select|Wrap|Line Numbers
  1. if (dictionary.contains(new Word("Hello"))) {
  2.    System.out.println("Found Word: \"Hello\"");
  3. }
Nov 17 '08 #4
r035198x
13,262 8TB
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.
Nov 17 '08 #5
mickey0
142 100+
nothing: it doesn't find the Word("hello") inside the dictionary.
Besides,I notice that if I do:
Expand|Select|Wrap|Line Numbers
  1. dictionary.add( new Word( "hello" );
  2. dictionary.add( new Word( "hello" );
  3.  
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....
Nov 17 '08 #6
r035198x
13,262 8TB
The answer lies in my post above which you have ignored.
Nov 17 '08 #7
Ganon11
3,652 Expert 2GB
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.
Nov 17 '08 #8
Nepomuk
3,112 Expert 2GB
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
Nov 17 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: amit | last post by:
I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE to do it, the find proerty does it, the results are getting...
0
by: AMIT PUROHIT | last post by:
hi, this is a qry which I m stuck up with I want to find out that if there is a mechanism to find a text inside a C# file and replace it with another string. I am using DTE(EnvDTE) to do it,...
0
by: amit | last post by:
hi I have created a tool which does a find and replace thru DTE, now after it is done, it opens up a window, "FIND REACHED THE STARTING POINT OF SEARCH" I want to disbale this window...
5
by: Mike Labosh | last post by:
In VB 6, the Form_QueryUnload event had an UnloadMode parameter that let me find out *why* a form is unloading, and then conditionally cancel the event. In VB.NET, the Closing event passes a...
3
by: David T. Ashley | last post by:
Hi, Red Hat Enterprise Linux 4.X. I'm writing command-line PHP scripts for the first time. I get the messages below. What do they mean? Are these operating system library modules, or...
0
by: Derek | last post by:
I am creating an intranet using Visual Web Developer Express Edition. Everything has been working OK until yesterday when I started getting 62 messages all beginning "Could not find schema...
5
by: BaronB | last post by:
Hi all, I'm writing a program to solve sliding block puzzles like these and while I think I've more or less figured it out, I have one problem before I start coding it. During execution, the...
2
by: Jazz Kyat | last post by:
Hello, I have a series of arrays that I inserted into a HashSet. Now I want to find out whether a particular array is contained in the set; how do I do that? Here is what the code looks like:...
2
by: puzzlecracker | last post by:
is there a comparable class to HashSet (java's) in csharp? Thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.