做一个方法 方法有两个参数 参数1 Map<String,String> 参数2:String value
方法完成的功能是 根据value获取key
public static 返回值类型 getKeyByValue(Map<String,String>,String value){
}
返回值类型自身思考用什么类型???
getKeyByValue(map,“111”)
例如:传入的map中的数据为:
map.put(“aaa”,“111”);
map.put(“bbb”,“111”);
map.put(“ccc”,“111”);
map.put(“ddd”,“222”);
方法就应该获得 “aaa” “bbb” “ccc”
package zuoye11;
import java.util.HashMap;
import java.util.Map;
public class Demo03 {
public static void main(String[] args) {
Map<String, String> map = new HashMap<String, String>();
map.put("aaa", "111");
map.put("bbb", "111");
map.put("ccc", "111");
map.put("ddd", "222");
String ab1= getKeyValue(map,"111");
System.out.println(ab1);
}
private static String getKeyValue(Map<String, String> map, String str) {
Map<String, String> map1 = new HashMap<>();
for(Map.Entry<String, String> e :map.entrySet()) {//遍历传入的map
if (map1.containsKey(e.getValue())) {//判断 map1中的key是否存下map的value
map1.put(e.getValue(),map1.get(e.getValue())+" "+e.getKey());
} else {
map1.put(e.getValue(),e.getKey());
}
}
String a = map1.get(str);
return a;
}
}