This discussion is archived
9 Replies Latest reply: Nov 10, 2009 2:00 PM by 3004 RSS

setting a hashmap key with a combination of Integer values

761308 Newbie
Currently Being Moderated
hi ,

I am newbie to java and need some help with setting a hashmap key with a combination of integer values.

My hashmap is
private Map<String, Boolean> map = new HashMap<String, Boolean>();
Now In the map i want to set the key value something like this:
int x = 123;
         int y = 456;
        
        map.put("123-456" , true);
I know i say map.put("x-y" ,true) it will get the corresponding x and y values. Please advise how to do this.

Thank You So Much

Edited by: twisai on Nov 10, 2009 1:35 PM
  • 1. Re: setting a hashmap key with a combination of Integer values
    796447 Newbie
    Currently Being Moderated
    int x = 123;
    int y = 456;
    String key = String.valueOf(x) + "-" + String.valueOf(y); // "123-456"
    for example.
  • 2. Re: setting a hashmap key with a combination of Integer values
    761308 Newbie
    Currently Being Moderated
    Thanks very much warneja for your instant reply. It worked
  • 3. Re: setting a hashmap key with a combination of Integer values
    3004 Newbie
    Currently Being Moderated
    If you want the two ints 123 and 456, rather than the String "123-456", then you should create a class with those two ints as members, and use instances of that class as your key.
  • 4. Re: setting a hashmap key with a combination of Integer values
    YoungWinston Expert
    Currently Being Moderated
    twisai wrote:
    Thanks very much warneja for your instant reply. It worked
    It worked, but is it good? Listen to jverd, he is a member of the 50,000 club and wise in the ways of the JUniverse.

    What you got is the answer to an implementation you've already decided on; and I suspect warnerja would also agree that it's not the best. Avoid converting things to strings when you don't have to (Bloch II, chapter 50, verses 1-5).

    Winston
  • 5. Re: setting a hashmap key with a combination of Integer values
    800308 Newbie
    Currently Being Moderated
    So x,y is logically a point, right? So it would probably be better to "model" a logical point in a Point class, right? (Just nod and smile if you don't understand... and read on)... Then from outside the Point class we could just refer to each logical point as a point, as apposed to referring to it's attributes (x & y) individually.

    This might seem a bit silly for just two attributes... but let's add a couple more attributes just for the sake of argument: Say we now need to store a z (altitude) and m (measure) attribute to our point, such that points A and b are equal if (and only if) they have the same x, y, z, and m attribute values.

    So, if we have four independant attributes then we have to test all four attributes, and (here's the kicker) we have to do so in each and every place where we need to determine if one point equals another, and we have to be really anal about doing this consistently in all those places, which is a bit yucky (if you don't mind me saying so)... So what to do instead? The object oriented approach is to create a Point class, and "encapsulate" that "complex" definition of equality into a convenient method in that class... and we can also add other "behaviours" which allow us to abstract away from detailed knowledge of the anatomy of a point.

    Ergo: The Point class would override [Object's hashCode method|http://java.sun.com/javase/6/docs/api/java/lang/Object.html#hashCode%28%29] (and the equals method, of course , because thats specified in "the equals-hashCode contract") and then we can use that class as the key of our HashMap. Easy.
    package forums;
    
    public class Point
    {
      private final int x;
      private final int y;
    
      public Point(int x, int y) {
        this.x = x;
        this.y = y;
      }
    
      public int getX() { return this.x; }
      public int getY() { return this.y; }
    
      public boolean equals(Object o) {
        return (o instanceof Point)
            && ((Point)o).x == this.x
            && ((Point)o).y == this.y;
      }
    
      public int hashCode() {
        return x*1000 + y;
      }
    }
    ... So now I have a logical point. It "knows" it's position, and it can tell me if it is equal to any other point, and it even has a "quick find tag" which will allow it to be used a key in a HashMap.

    Cheers. Keith.
  • 6. Re: setting a hashmap key with a combination of Integer values
    3004 Newbie
    Currently Being Moderated
    corlettk wrote:
    This might seem a bit silly for just two attributes.
    Not in the least. Easier handling of more attributes is of secondary importance in my opinion. The real value is in clear modeling, so that a developer dealing with this data sees an instance of Point and knows that it represents a Point, as opposed to having to remember or figure out what various String formats represent.
  • 8. Re: setting a hashmap key with a combination of Integer values
    843789 Newbie
    Currently Being Moderated
    Looking at Point2D.hashCode()
        public int hashCode() {
         long bits = java.lang.Double.doubleToLongBits(getX());
         bits ^= java.lang.Double.doubleToLongBits(getY()) * 31;
         return (((int) bits) ^ ((int) (bits >> 32)));
        }
    I guess 64-bit scalar multiplication is a relatively cheap operation on most processors
    or they might have gone for
        public int hashCode() {
         long bits = java.lang.Double.doubleToLongBits(getY());
         bits = ((bits<<5)-bits) ^ java.lang.Double.doubleToLongBits(getX());
         return (((int) bits) ^ ((int) (bits >> 32)));
        }
  • 9. Re: setting a hashmap key with a combination of Integer values
    843789 Newbie
    Currently Being Moderated
    My guess the OP is gone!!! may be without a to the Point answer.