This discussion is archived
4 Replies Latest reply: Jan 6, 2005 8:18 AM by 807596 RSS

Hashtable with int values

807596 Newbie
Currently Being Moderated
Hi,
I am using JDK 1.4. I need a hashtable like datastructure where i can store int values (primitive) against string keys. Is there any datastructure like that in java and if not how can it be implemented?
  • 1. Re: Hashtable with int values
    gimbal2 Guru
    Currently Being Moderated
    A primitive is not an object, you can't have a reference to it. You need to wrap them in an Integer object and store that. Use a Map as your hashtable (HashMap for example). Then you can simply do
    Map mymap = new HashMap();
    mymap.put("myint1", new Integer(5));
  • 2. Re: Hashtable with int values
    807596 Newbie
    Currently Being Moderated
    The colt library has some hash maps and collections that work with different kind of primitives:
    http://hoschek.home.cern.ch/hoschek/colt/

    Also, I think the jakarta commons project at apache may have similar things, but I've never used it...
  • 3. Re: Hashtable with int values
    807596 Newbie
    Currently Being Moderated
    If your really want to store primitive values and
    want to access them by hash, you would have
    to write your own Hashtable that uses a int[]
    as underlying storage. This is definitly not part
    of JRE and I am not aware of any 3rd party library
    that does that. It's even questionable if you could
    get any performance win out of it. However if you do,
    let me know.
  • 4. Re: Hashtable with int values
    807596 Newbie
    Currently Being Moderated
    IBM has an article on a collections class called Trove that can take primitives as keys and values:
    http://www-106.ibm.com/developerworks/java/library/j-perf09284.html