Map 根据value 获取key

来源:互联网 发布:手机电话录音软件 编辑:程序博客网 时间:2024/05/01 00:57
package com.basic.object;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


public class GetKeyByValue {

public static ArrayList getAllKey(Map<Object, Object> sourceMap, Map<String, Object> temp){
ArrayList list = new ArrayList();

for (Object getKey : sourceMap.keySet()) {
if (temp.equals(sourceMap.get(getKey))){
list.add(getKey);
System.out.println("Adding the key...");
}
//System.out.println("Can not add the key...");
}

return list;
}


public static void main(String[] args) {

Map<Object, Object> sourceMap = new HashMap<Object, Object>();
Map<String, Object> temp = new HashMap<String, Object>();


temp.put("shanghai", "CN");
temp.put("shenzhen", "CN");

sourceMap.put("P", temp);


ArrayList list = getAllKey(sourceMap, temp);

System.out.println("Run here...");

for (int i=0; i<list.size(); i++){
System.out.println("The key is ...." + list.get(i));
}


}
}
0 0