Results 1 to 4 of 4
  1. #1
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default Uisng primitive type values as keys for Hashtable

    I have the following code which works fine.

    Java Code:
    Hashtable hash = new Hashtable(10,10);
    int i= 1;
    hash.put(i, "StringV");
    System.out.println(hash.get(i));
    I read somewhere that Hashable takes objects as key and primitive types are not allowed as keys. I tried int as key and it worked. Can anyone tell me about this.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Autoboxing in j2se 1.5+ allows this. See this page in the guide in your j2se 1.5 javadocs distribution, or Autoboxing.

  3. #3
    ravian is offline Senior Member
    Join Date
    Nov 2007
    Posts
    115
    Rep Power
    0

    Default

    Thanks. That explains.

  4. #4
    kash_meu is offline Member
    Join Date
    Nov 2007
    Posts
    1
    Rep Power
    0

    Default Another Way

    Another way to accomplish this can be

    hash.put(new Integer(i), "StringV");
    System.out.println(hash.get(new Integer(i)));

Similar Threads

  1. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  2. Primitive data types of Java
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-28-2008, 07:29 PM
  3. Retreiving list of keys from Map with certan value
    By desmond5 in forum New To Java
    Replies: 1
    Last Post: 03-06-2008, 08:10 PM
  4. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Posting Permissions

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