Dictionary in Java

Discussion in 'Programmer's Corner' started by Eric007, Dec 26, 2016.

  1. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
  2. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
  3. Eric007

    Thread Starter Senior Member

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

    Home come!?:rolleyes::mad:
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,039
    3,132
    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.
     
  5. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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.
     
  6. WBahn

    Moderator

    Mar 31, 2012
    23,160
    6,973
    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 (Text):
    1.  
    2. import java.util.Map;
    3. import java.util.HashMap;
    4.  
    5. public class TestMap
    6. {
    7.    public static void main(String[] args)
    8.    {
    9.       Map<String, Integer> map = new HashMap<>();
    10.  
    11.       map.put("Fred", 1);
    12.       map.put("Tom", 2);
    13.       map.put("Dick", 3);
    14.       map.put("Harry", 1);
    15.  
    16.       System.out.println(map);
    17.  
    18.       String[] names = {"Tom", "Sue", "Dick", "Trent", "Fred", "Harry"};
    19.       for (String name: names)
    20.       {
    21.          if (map.containsKey(name))
    22.             System.out.println("Map contains " + name);
    23.          else
    24.             System.out.println("Map does not contain " + name);
    25.       }
    26.    }
    27. }
    28.  
    29.  
     
  7. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
  8. WBahn

    Moderator

    Mar 31, 2012
    23,160
    6,973
    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.
     
  9. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
  10. WBahn

    Moderator

    Mar 31, 2012
    23,160
    6,973
    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.
     
  11. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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

    Thanks in advance!
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,039
    3,132
    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?
     
  13. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
    • Eric.zip
      File size:
      212 bytes
      Views:
      0
  14. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    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
     
  15. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,039
    3,132
    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++.
     
  16. Eric007

    Thread Starter Senior Member

    Aug 5, 2011
    1,145
    39
    Yes I am creating an object before storing. Let's leave it for now until I post my code then you will take it from there.
     
Loading...