4 Replies Latest reply: Jan 6, 2005 10:18 AM by 807596 RSS

    Hashtable with int values

      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
          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
            The colt library has some hash maps and collections that work with different kind of primitives:

            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
              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
                IBM has an article on a collections class called Trove that can take primitives as keys and values: