从Java中的HashMap获取密钥
我有一个像Java这样的Hashmap:
private Map<String, Integer> team1 = new HashMap<String, Integer>(); 然后我像这样填充它:
 team1.put("United", 5); 
 我怎样才能拿到钥匙? 例如: team1.getKey()返回“United”。 
 一个HashMap包含多个键。 您可以使用keySet()来获取所有密钥的集合。 
 team1.put("foo", 1); team1.put("bar", 2); 
 将存储1与关键"foo"和2与关键"bar" 。 遍历所有的键: 
 for ( String key : team1.keySet() ) { System.out.println( key ); } 
 将打印"foo"和"bar" 。 
这是可行的,至less在理论上, 如果你知道指数:
 System.out.println(team1.keySet().toArray()[0]); 
  keySet()返回一个列表,所以你把列表转换成一个数组。 
当然,问题是一套不承诺保持您的订单。 如果你的HashMap中只有一个项目,那么你很好,但是如果你有更多的东西,最好是在地图上循环,就像其他答案一样。
检查这个。
http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
假设你对每个键有不同的值,你可以这样做:
 private String getKey(Integer value){ for(String key : team1.keySet()){ if(team1.get(key).equals(value)){ return key; //return the first found } } return null; } 
或者如果你不能假定每个键都有不同的值:
 private List<String> getKeys(Integer value){ List<String> keys = new ArrayList<String>(); for(String key : team1.keySet()){ if(team1.get(key).equals(value)){ keys.add(key); } } return keys; } 
或者使用JDK8
 private Optional<String> getKey(Integer value){ return team1 .entrySet() .stream() .filter(e -> e.getValue().equals(value)) .map(e -> e.getKey()) .findFirst(); } private List<String> getKeys(Integer value){ return team1 .entrySet() .stream() .filter(e -> e.getValue().equals(value)) .map(e -> e.getKey()) .collect(Collectors.toList()); } 
 您可以使用keySet()方法检索所有Map的键。 现在,如果你需要的是获得一个关键的价值 ,这是一个完全不同的问题, Map不会帮助你在那里; 您需要一个专门的数据结构,比如来自Apache 公共集合的 BidiMap (一个允许在键和值之间进行双向查找的映射) – 也要注意几个不同的键可以映射到相同的值。 
 private Map<String, Integer> _map= new HashMap<String, Integer>(); Iterator<Map.Entry<String,Integer>> itr= _map.entrySet().iterator(); //please check while(itr.hasNext()) { System.out.println("key of : "+itr.next().getKey()+" value of Map"+itr.next().getValue()); } 
 正如你想得到参数( United )的价值( 5 ),你也可以考虑使用双向地图(例如提供的番石榴: http : //docs.guava-libraries.googlecode.com/git/javadoc/com /google/common/collect/BiMap.html )。 
如果你只是需要一些简单和更多的validation。
 public String getKey(String key) { if(map.containsKey(key) { return key; } return null; } 
那么你可以search任何键。
 System.out.println( "Does this key exist? : " + getKey("United") ); 
试试这个简单的程序:
 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); } } 
 public class MyHashMapKeys { public static void main(String a[]){ HashMap<String, String> hm = new HashMap<String, String>(); //add key-value pair to hashmap hm.put("first", "FIRST INSERTED"); hm.put("second", "SECOND INSERTED"); hm.put("third","THIRD INSERTED"); System.out.println(hm); Set<String> keys = hm.keySet(); for(String key: keys){ System.out.println(key); } } } 
获得钥匙及其价值
例如
 private Map<String, Integer> team1 = new HashMap<String, Integer>(); team1.put("United", 5); team1.put("Barcelona", 6); for (String key:team1.keySet()){ System.out.println("Key:" + key +" Value:" + team1.get(key)+" Count:"+Collections.frequency(team1, key));// Get Key and value and count } 
将打印:关键:联合值:5关键:巴塞罗那值:6
我会做什么,这是非常简单的,但浪费内存是映射与一个关键的价值观,并做相反的映射与这样的价值:
 private Map<Object, Object> team1 = new HashMap<Object, Object>(); 
 使用<Object, Object>是非常重要的,所以你可以映射keys:Value和Value:Keys 
 team1.put("United", 5); 
 team1.put(5, "United"); 
 所以如果你使用team1.get("United") = 5和team1.get(5) = "United" 
但是如果你在对中的一个对象上使用了一些特定的方法,如果你制作另一个地图,我会更好:
 private Map<String, Integer> team1 = new HashMap<String, Integer>(); 
 private Map<Integer, String> team1Keys = new HashMap<Integer, String>(); 
接着
 team1.put("United", 5); 
 team1Keys.put(5, "United"); 
记住,保持简单;)