package com.other;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
//获取一个Key
public static Object getKey(HashMap hm,String value){
Object key=null;
for(Object getKey:hm.keySet()){
if (hm.get(getKey).equals(value)) {
key=getKey;
}
}
return key;
}
//获取所有Key
public static ArrayList getAllKey(HashMap hm,String value){
ArrayList list=new ArrayList();
for(Object getKey:hm.keySet()){
if (hm.get(getKey).equals(value)) {
list.add(getKey);
}
}
return list;
}
//测试类
public static void main(String[] args) {
HashMap hm=new HashMap();
hm.put("CHINA", "中国");
hm.put("CN", "中国");
//获取一个Key
System.out.println("通过Value获取Key:"+getKey(hm,"中国"));
//获取所有Keys
ArrayList list=getAllKey(hm,"中国");
for(int i=0;i<list.size();i++){
System.out.println(list.get(i));
}
}
}
引用 http://www.zhijie.net