Results 1 to 15 of 15
  1. #1
    Toxicgas1 is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Exclamation Hashmapping: Have values, but returns key isn't there

    This is my last chance, my presentation is in 9 hours and I broke my program. I am on my knees.

    As the title states, I can get the values of all my object entries in my Hashmap, but when I use containsKey, it returns false.

    Here's my code, if it matters I'm running my methods from my main, and creating an object as to access the method with the map.

    Java Code:
    //map is the class containing the Hashmap
    //the hm is the name of the Hashmap
    System.out.print(map.hm.values()); //Gives me my values, and I selected -1414871478 (as an Integer)
    System.out.println(map.hm.containsKey(-1414871478)); //This line returns false
    Any help is appreciated... ANY!
    Last edited by Toxicgas1; 08-11-2012 at 10:45 AM.

  2. #2
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    As I know containsKey method returns boolean, so why will it return null?
    Last edited by mine0926; 08-11-2012 at 04:23 AM.

  3. #3
    Toxicgas1 is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashmapping: Have values, but returns key isn't there

    I noticed that the second I put it up, so I edited that. But obviously not fast enough for you!

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    Well you might overlooked this line.
    Quote Originally Posted by Toxicgas1 View Post
    Java Code:
    System.out.println(map.hm.containsKey(-1414871478)); //This line returns null

  5. #5
    Toxicgas1 is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashmapping: Have values, but returns key isn't there

    There, that one is fixed too. It doesn't matter much anyway, unless some godly developer can come along and say 'Here's the awnser' because I have about 2 hours.

  6. #6
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    What I mean on my first reply is containsKey does not return null and the PROBLEM might be on that method! You show us little of your code and we can only refer to that!
    show us the code of hm.containsKey(), if you have

  7. #7
    Toxicgas1 is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashmapping: Have values, but returns key isn't there

    That isn't a custom method, it's built into Java.util.Hashmap. The code given really is all there is to recreate the problem, the rest is irrelevant (works off of this, this must work correctly for the program to continue).

  8. #8
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    Is your key and values are same? because you print the values and then using that value you are looking for the KEY.

    -Hope you understand that

  9. #9
    Toxicgas1 is offline Member
    Join Date
    Aug 2012
    Posts
    5
    Rep Power
    0

    Default Re: Hashmapping: Have values, but returns key isn't there

    If key isn't an object other than a plain integer, the value method gives me all values which I presume are keys. Use one of those printed out I then look for the connected object.

  10. #10
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    Java Code:
            HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
            hm.put(1, 9);
            hm.put(2, 8);
            hm.put(3, 7);
            System.out.println("VALUES: " + hm.values());
            System.out.println("HAS KEY: " + hm.containsKey("9"));
    OUTPUT :
    VALUES: [9, 8, 7]
    HAS KEY: false

    As you see hm.values() does not return the keys (which are 1,2,3), it returns the values (which are 9,8,7).

  11. #11
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    change map.hm.values() to map.hm.keySet() to get all the keys from hm

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default Re: Hashmapping: Have values, but returns key isn't there

    You are looking for a String "9" while that map has Integers for its key type ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    yeah, my mistake. But the result still false if it is an Integer (9) :D

  14. #14
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: Hashmapping: Have values, but returns key isn't there

    Quote Originally Posted by mine0926 View Post
    yeah, my mistake. But the result still false if it is an Integer (9) :D
    The result false is ok, since when you store a new entry in a HashMap the key comes before the value. So in your example the keys are 1, 2, 3 and the values are 9, 8, 7 related to the keys. if you try hm.containsKey(3) you will get true. study the hashmap before you use it: Map Implementations

  15. #15
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default Re: Hashmapping: Have values, but returns key isn't there

    Quote Originally Posted by j2me64 View Post
    The result false is ok, since when you store a new entry in a HashMap the key comes before the value. So in your example the keys are 1, 2, 3 and the values are 9, 8, 7 related to the keys. if you try hm.containsKey(3) you will get true. study the hashmap before you use it: Map Implementations
    That is the OP's problem and so I am trying to produce the exact error, only more simple so that the OP can see it.

Similar Threads

  1. Replies: 4
    Last Post: 02-16-2011, 05:31 AM
  2. Replies: 14
    Last Post: 01-22-2011, 03:36 PM
  3. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-11-2009, 12:35 AM
  4. Replies: 0
    Last Post: 05-22-2008, 11:53 AM
  5. Replies: 4
    Last Post: 04-15-2008, 02:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •