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));
}
}
}
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));
}
}
}