Hello,
I'm new to java and my problem is this: -
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?
8 6301
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.
sorry but that line isn't correct at all. I don't know how write that thing...
Try: - if (dictionary.contains(new Word("Hello"))) {
-
System.out.println("Found Word: \"Hello\"");
-
}
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.
nothing: it doesn't find the Word("hello") inside the dictionary.
Besides,I notice that if I do: -
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....
The answer lies in my post above which you have ignored.
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.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:...
|
by: puzzlecracker |
last post by:
is there a comparable class to HashSet (java's) in csharp?
Thanks
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
| |