4 Replies Latest reply: Apr 9, 2008 8:39 AM by 807591 RSS

    HashMap - Non primitive types only?

    807591
      Am I right in saying that a HashMap can only map non primitive data types? So for example, if I want to map char to int I have to map Character to Integer?

      I seem to be wasting alot of time in an otherwise very efficient program, calling the .intValue() and .charValue() methods on the types Character and Integer. (As well as the time wasted declaring new Character(char) and new Integer(int) just to put in the HashMap). Is there any way that I could map, say char a-z on int 0-25? (Without having to use Character and Integer, and call the .*Value() methods.)

      HashMap<Character, Integer> charConverter = new HashMap<Character, Integer>();

      ^That's what my code looks like at the moment. I am using java 1.6, Eclipse wouldn't let me compile without explicitly stating the kind of contents to be held in the HashMap.

      Cheers

      Finbarr
        • 1. Re: HashMap - Non primitive types only?
          807591
          You don't need to convert using intValue and charValue. From java 1.5 there's been autoboxing to take care of this.
          Integer i = map.get(key);
          int j = i;//nothing more to it
          • 2. Re: HashMap - Non primitive types only?
            807591
            I'll try that, but it seems to involve way more lines of code.
            HashMap<Character, Integer> charConverter = new HashMap<Character, Integer>();
            
            private void populateMappings(char[] alphabet){
            
                 for(int i = 0; i < alphabet.length; i++){
                      Integer tempInt = i;
                      Character tempChar = alphabet;
            charConverter.put(tempChar, tempInt);
            }

            }
            vs
            HashMap<Character, Integer> charConverter = new HashMap<Character, Integer>();

            private void populateMappings(char[] alphabet){

            for(int i = 0; i < alphabet.length; i++){
            charConverter.put(new Character(alphabet[i]), new Integer(i));
            }

            }
            And your suggestion:
            char a = 'a';
            Character temp = a;
            Integer j = map.get(temp);
            int i = j;
            vs
            int i = map.get( ( (Integer)new Character(x) ) ).intValue();
            Finbarr
            
            Edited by: FTAYLOR on Apr 9, 2008 6:27 AM                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
            • 3. Re: HashMap - Non primitive types only?
              807591
              You misunderstand. It works the other way round as well. You can do directly:
              charConverter.put(alphabet, i);
              
              And
              int i = map.get(x);
                                                                                                                                                                                                                                                                                                                                                          
              • 4. Re: HashMap - Non primitive types only?
                807591
                Thankyou for your help.

                I need to go about optimizing the HashMap in contructor now.

                Finbarr