# Dictionary in Java

#### Eric007

Joined Aug 5, 2011
1,154
Hi All,

Merry Christams & Happy New Year 2017.

I have used the 'dictionary' collection in C# successfully. It's my first time using a diction in Java and I am having issues. The issue is that the Hashmap does not recognize the key entered and I do not know why. Not sure if I am missing anything. I tried debugging and yes I can see in the logcat the key and value (the value is a customed object)

Here's my code sample:

Map<String, SomeClass> dictionary = new Hashmap<>();

I been adding stuff as follows:

dictionary.put(key, object);

BUT when I try to retrieve an object using a key it always returns null. And when I check if the key exist as follows:
if(dictionary.containsKey("keyname"))
{
}
it says null.

Any help will be appreciated. I hate wasting time on these kind of things

Eric

#### Eric007

Joined Aug 5, 2011
1,154
let me put it this way:

Does anyone knows how to create a simple dictionary (K, V)in Java? without this Hashmap and collision bah bah bah

#### Eric007

Joined Aug 5, 2011
1,154
The fun/weird part is that the current code (dictionary) recognizes all the keys BUT the first one!

Home come!?

#### spinnaker

Joined Oct 29, 2009
7,815
Did you remember to new the object that you are storing?

I would first test things out with something like a storing a simple integer.

#### Eric007

Joined Aug 5, 2011
1,154
As temporary solution (might be final) I just created a dummy entry as first entry. I can now retrieve all the objects using second key,...,nth key.

#### WBahn

Joined Mar 31, 2012
24,572
It's hard to guess why your code is misbehaving given so little code to work with. You don't show how you are putting an entry with "keyname" into the map, so it's pretty much impossible to tell why it isn't in there.

It would really help if you stripped your code down to the minimal code that exhibits the undesired behavior and then post that. Then we have something real to look at and play with.

You asked for an example of a dictionary that didn't use HashMap. I don't know what you were thinking of using instead, so here's one that does use it and that works. Hopefully that will be of some help to you.

Code:
import java.util.Map;
import java.util.HashMap;

public class TestMap
{
public static void main(String[] args)
{
Map<String, Integer> map = new HashMap<>();

map.put("Fred", 1);
map.put("Tom", 2);
map.put("Dick", 3);
map.put("Harry", 1);

System.out.println(map);

String[] names = {"Tom", "Sue", "Dick", "Trent", "Fred", "Harry"};
for (String name: names)
{
if (map.containsKey(name))
System.out.println("Map contains " + name);
else
System.out.println("Map does not contain " + name);
}
}
}

#### Eric007

Joined Aug 5, 2011
1,154
It's hard to guess why your code is misbehaving given so little code to work with. You don't show how you are putting an entry with "keyname" into the map, so it's pretty much impossible to tell why it isn't in there.

It would really help if you stripped your code down to the minimal code that exhibits the undesired behavior and then post that. Then we have something real to look at and play with.

You asked for an example of a dictionary that didn't use HashMap. I don't know what you were thinking of using instead, so here's one that does use it and that works. Hopefully that will be of some help to you.
The structure of code is exactly as you posted. That's really weird! The only difference is that my values are custom objects. Lets work with your code then. if I try to retrieve the object with the 'Fred' key, the app crashes.
Actually I have a .csv file that I open, parse and put it in an object. I then add the object in a dictionary. What I did to make it work is, in my csv file I added a line '-,-,-,-'. Basically what I am trying to say is when I read the first object by passing the first key it crashes.

Based on your code my csv looks like this:

Fred, is, a, teacher
Tom, is, a, liar
Dick, has, a, car
Harry, is, a, mechanic

I use the first column as key. To make it work I changed the above as follows:
-,-,-,-
Fred, is, a, teacher
Tom, is, a, liar
Dick, has, a, car
Harry, is, a, mechanic

With this change I can retrieve the first object (is, a, teacher) using the 'Fred' key.

Thanks anyway
Eric

#### WBahn

Joined Mar 31, 2012
24,572
Well, I'm glad you found a kludge that you seem happy with. Hopefully the kludge isn't just masking a more fundamental issue that is going to come back and bite you (which kludge solutions have a strong tendency to do). Without seeing the actual code that is actually misbehaving, I can't even do much in the way of guessing what might be the problem.

#### Eric007

Joined Aug 5, 2011
1,154
Well, I'm glad you found a kludge that you seem happy with. Hopefully the kludge isn't just masking a more fundamental issue that is going to come back and bite you (which kludge solutions have a strong tendency to do). Without seeing the actual code that is actually misbehaving, I can't even do much in the way of guessing what might be the problem.
Yep! That's why I said this is a temporary solution that might be final. Coz I'm pretty sure something is wrong here! Will do a C# version and check if the behavior persists. If so will post code here.

Thanks
Eric

#### WBahn

Joined Mar 31, 2012
24,572
Yep! That's why I said this is a temporary solution that might be final. Coz I'm pretty sure something is wrong here! Will do a C# version and check if the behavior persists. If so will post code here.

Thanks
Eric
I'm more than willing to take a shot at spotting what is wrong. But I simply can't do that without seeing the actual code that is exhibiting the behavior. Not a description of part of it, but the actual, complete, code (minimized as much as possible) that actually exhibits the problem. The issue might be in how you set up a Scanner object, for all I know, and have nothing at all to do with how you are using your Map object.

#### Eric007

Joined Aug 5, 2011
1,154
Now things getting interesting! I wrote a quick dictionary in C# and same behavior! I had to add '-,-,-,-' to be able to retrieve the first object.
Will definitely post the code here but as I have some other code to write I will use the '-,-,-,-' as temporary solution. Dictionary in C# is even simpler and I was expecting better results.

@WBahn in your above code, can you please try to retrieve only the object with key 'Fred'. Do not loop as you did. I am interested in retrieving individual object. I am suspecting the issue is in the .csv file.
if you don't mind you may create a csv file containing the following data:

Fred, is, a, teacher
Tom, is, a, liar
Dick, has, a, car
Harry, is, a, mechanic

#### spinnaker

Joined Oct 29, 2009
7,815
Now things getting interesting! I wrote a quick dictionary in C# and same behavior! I had to add '-,-,-,-' to be able to retrieve the first object.
Will definitely post the code here but as I have some other code to write I will use the '-,-,-,-' as temporary solution. Dictionary in C# is even simpler and I was expecting better results.

@WBahn in your above code, can you please try to retrieve only the object with key 'Fred'. Do not loop as you did. I am interested in retrieving individual object. I am suspecting the issue is in the .csv file.
if you don't mind you may create a csv file containing the following data:

Fred, is, a, teacher
Tom, is, a, liar
Dick, has, a, car
Harry, is, a, mechanic

1. You never answered my question. Not sure if your post #5 is an answer to that or not, it is confusing If it is then, if you can store and retrieve objects then where is the problem?

2. No one is going to be able to help you if we can't see your code.

3. If you can't take the time to post code or try the sample you were given then why should anyone do your work for you?

#### Eric007

Joined Aug 5, 2011
1,154
I have attached the .csv file with the above data. I tested it but having the same issue. we can work with this little file. Try to retrieve the object containing (is, a, teacher) using the 'Fred' key.

Thanks!
Eric

#### Attachments

• 212 bytes Views: 0

#### Eric007

Joined Aug 5, 2011
1,154
1. You never answered my question. Not sure if your post #5 is an answer to that or not, it is confusing If it is then, if you can store and retrieve objects then where is the problem?

2. No one is going to be able to help you if we can't see your code.

3. If you can't take the time to post code or try the sample you were given then why should anyone do your work for you?
Sorry for not replying to you.

1. #5 is a temporary solution. Storing objects has never been a problem BUT retrieving! And the problem was/is retrieving the object with the first key. @WBahn code is ok but my code is slightly different as I mentioned in one of the above code the data entered into the dictionary comes from a csv file that has been parsed. That is why in post #13 I posted a sample csv file to work with as the real csv file is private! So if it works with this little file it will work with the real file as well.

2 & 3. Yes I don't really have time to post code for now as I am busy writing other code. I don't want to try that code coz it's slightly different from what I doing... the above one loop through the whole dictionary and print them and I am retrieving individual object. Also I did not try that code coz I don't see anything that I missed from the code posted by @WBahn.
Actually I do not want anyone to do the work for me! I can write my own code I am just stuck with this little thing. BTW, @WBahn please do not try to help by trying to solve it as I requested in post #11 and #13. I will figured it out by myself and post my solution or tell you what was wrong as soon as I get the time to do so.

Thanks All
Eric

#### spinnaker

Joined Oct 29, 2009
7,815
Then answer my question. Are you creating the object before storing it?

You need to store a pointer to an object not the actual object.

This won't work. Note pseudo code. It has been a while since I worked in Java.

class Person

name : string;
age : integer;

End class

Person person;

void main()
{

person.name = "John";
person.age = 50;

map.put(person, 1);

}

At least this won't work in C++.