Which collection is used in Java?
                                        
                    •
                    Java                                    
                I want to map integers to strings. They are one-to-one, for example:
60 : c 61 : c# 62 : d 63 : d# 64 : e 65 : f 66 : f#
But I need to be able to do the following:
>Get the value from the key: "C" = getValue (60) [give it a key 60 and return a string value] > get the key from the value: 65 = getKey ("F") [give it a string value "F" and return a key]
Which collection model is best suited to this situation? I asked, because I read a few, no one can do the < 2 > part Or do I have to write code to traverse each pair to find which key has the value "F"?
Edit: jdk1 Didn't you do that in 6?
Solution
Found this in another forum (link text)
public class ReversibleMap {
       private final Map keyToValue = new HashMap(8);
       private final Map valueToKey = new HashMap(8);
       public void put(Integer key,String value) {
           keyToValue.put(key,value);
           valueToKey.put(value,key);
       }
       public String get(Integer key) {
           return (String) keyToValue.get(key);
       }
       public Integer getKey(String value) {
           return (Integer) valueToKey.get(value);
      }
   }
This should be done
                            The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
                        
                                            
                        THE END
                    
                    
                    
                                                        二维码
                        
                        
                                                
                        