java根据Map的值(value)取键(key) 的实现方法有4种,分别为:
(1)使用for循环遍历
(2)使用Iterator迭代器
(3)使用KeySet迭代
(4)使用EnterySet迭代
下面为以上4种方法具体实现的代码:
1、使用for循环遍历
public static Object getKey(HashMap<Object,Object> map, String v) {
String key = "";
for (Map.Entry<String, Object> m :map.entrySet()) {
if (m.getValue().equals(v)) {
key = m.getKey();
}}
return key;
}
2、使用Iterator迭代器
public static Object getKey(HashMap<Object,Object> map, String v) {
Set set = map.entrySet();
Iterator iterator=set.iterator();
String key = "";
while (iterator.hasNext()) {
Map.Entry<String, Object> enter = (Entry<String, Object>)
iterator.next();
if (enter.getValue().equals(v)) {
key = enter.getKey();
}}
return key;
}
3、使用KeySet迭代
public static Object getKey(HashMap<Object,Object> map, String v) {
Iterator<Object> it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
if ((String) map.get(key).equals(v)) return key;
}
return null;
}
4、使用EnterySet迭代
public static Object getKey(HashMap<Object,Object> map, String v) {
String key = "";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
key = (String) entry.getKey();
}}
return key;
}
扩展资料:
java获取map的key和value的方法:
(1) 通过map.keySet()方法,先获取map的key,然后根据key获取对应的value。
for(String key : map.keySet()){
String value = map.get(key);
System.out.println(key+" "+value);
}
Iterator<String> iter = map.keySet().iterator();
while(iter.hasNext()){
String key=iter.next();
String value = map.get(key);
System.out.println(key+" "+value);
}
(2)通过map.entrySet()方法,循环map里面的每一对键值对,然后获取key和value。
for(Entry<String, String> vo : map.entrySet()) {
vo.getKey();
vo.getValue();
System.out.println(vo.getKey()+" "+vo.getValue());
}
Iterator<Entry<String,String>> iter = map.entrySet().iterator();
while(iter.hasNext()){
Entry<String,String> entry = iter.next();
String key = entry.getKey();
String value = entry.getValue();
System.out.println(key+" "+value);
}
参考资料来源:JAVA官方文档-Map
public static void main(String[] args) {
Map<String, Object> map = new LinkedHashMap<String,
Object>();
map.put("username","zhaokuo");
map.put("password", "123456");
map.put("email", "zhaokuo719@163.com");
map.put("sex", "男");
//第一种 用for循环的方式
for (Map.Entry<String, Object> m :map.entrySet()) {
System.out.println(m.getKey()+"\t"+m.getValue());
}
//利用迭代 (Iterator)
Set set=map.entrySet();
Iterator iterator=set.iterator();
while(iterator.hasNext()){
Map.Entry<String, Object> enter=(Entry<String, Object>)
iterator.next();
System.out.println(enter.getKey()+"\t"+enter.getValue());
}
//利用KeySet 迭代
Iterator it = map.keySet().iterator();
while(it.hasNext()){
String key;
String value;
key=it.next().toString();
value=(String) map.get(key);
System.out.println(key+"--"+value);
}
//利用EnterySet迭代
Iterator i=map.entrySet().iterator();
System.out.println( map.entrySet().size());
String key;
String value;
while(i.hasNext()){
Map.Entry entry = (Map.Entry)i.next();
key=entry.getKey().toString();
value=entry.getValue().toString();
System.out.println(key+"===="+value);
}
System.out.println(getKeyByValue(map, "zhaokuo"));
}
public static String getKeyByValue(Map map, Object value) {
String keys="";
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Entry) it.next();
Object obj = entry.getValue();
if (obj != null && obj.equals(value)) {
keys=(String) entry.getKey();
}
}
return keys;
}
Object>>,遍历得到entry。entry.getKey是键,entry.getValue是值。如果某一个entry.getValue和你的值相等,你return entry.getKey就是你要的键。
for(Map.Entry entry:map.entrySet()){
if(value.equals(entry.getValue())return entry.getKey();
}
return null;
如果value唯一可以设计一个函数取key值