HashMap
package TestExample;
import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.HashMap; import java.util.Set;
class TestHashMap { static Map map = new HashMap(); public static void main(String args) {
map.put("中国", "北京");
map.put("ss中国", "北京");
map.put("中国", "北京");
map.put("日本", "东京");
map.put("法国", "巴黎");
//根据KEY获得VALUE
System.out.println(map.get("中国"));
//根据Value获得Key
System.out.println(getKey("北京"));
}
/*
*
* 这里关键是那个entrySet()方法,它会返回一个Map.Entry集的Set对象,Map.Entry对象中有getValue()和getKey()方法
*/
public static Object getKey(Object value)
{
ArrayList all = new ArrayList();
Object o = null;
Set set = map.entrySet();
Iterator it = set.iterator();
while(it.hasNext())
{
Map.Entry enter =(Map.Entry)it.next();
if(enter.getValue().equals(value))
{
o = enter.getKey();
all.add(o);
}
}
return all;
}
}