Get keys from HashMap in Java

后端 未结 14 2634
野的像风
野的像风 2020-12-04 07:03

I have a Hashmap in Java like this:

private Map team1 = new HashMap();

Then I fill it like th

相关标签:
14条回答
  • 2020-12-04 07:37

    As you would like to get argument (United) for which value is given (5) you might also consider using bidirectional map (e.g. provided by Guava: http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/BiMap.html).

    0 讨论(0)
  • 2020-12-04 07:37

    A solution can be, if you know the key position, convert the keys into an String array and return the value in the position:

    public String getKey(int pos, Map map) {
        String[] keys = (String[]) map.keySet().toArray(new String[0]);
    
        return keys[pos];
    }
    
    0 讨论(0)
  • 2020-12-04 07:38

    What I'll do which is very simple but waste memory is to map the values with a key and do the oposite to map the keys with a value making this:

    private Map<Object, Object> team1 = new HashMap<Object, Object>();

    it's important that you use <Object, Object> so you can map keys:Value and Value:Keys like this

    team1.put("United", 5);

    team1.put(5, "United");

    So if you use team1.get("United") = 5 and team1.get(5) = "United"

    But if you use some specific method on one of the objects in the pairs I'll be better if you make another map:

    private Map<String, Integer> team1 = new HashMap<String, Integer>();

    private Map<Integer, String> team1Keys = new HashMap<Integer, String>();

    and then

    team1.put("United", 5);

    team1Keys.put(5, "United");

    and remember, keep it simple ;)

    0 讨论(0)
  • 2020-12-04 07:41

    You can retrieve all of the Map's keys using the method keySet(). Now, if what you need is to get a key given its value, that's an entirely different matter and Map won't help you there; you'd need a specialized data structure, like BidiMap (a map that allows bidirectional lookup between key and values) from Apache's Commons Collections - also be aware that several different keys could be mapped to the same value.

    0 讨论(0)
  • 2020-12-04 07:42

    To get keys in HashMap, We have keySet() method which is present in java.util.Hashmap package. ex :

    Map<String,String> map = new Hashmap<String,String>();
    map.put("key1","value1");
    map.put("key2","value2");
    
    // Now to get keys we can use keySet() on map object
    Set<String> keys = map.keySet();
    

    Now keys will have your all keys available in map. ex: [key1,key2]

    0 讨论(0)
  • 2020-12-04 07:46

    Try this simple program:

    public class HashMapGetKey {
    
    public static void main(String args[]) {
    
          // create hash map
    
           HashMap map = new HashMap();
    
          // populate hash map
    
          map.put(1, "one");
          map.put(2, "two");
          map.put(3, "three");
          map.put(4, "four");
    
          // get keyset value from map
    
    Set keyset=map.keySet();
    
          // check key set values
    
          System.out.println("Key set values are: " + keyset);
       }    
    }
    
    0 讨论(0)
提交回复
热议问题