根据第一张地图中的名字找到salay

时间:2017-03-19 09:13:36

标签: java collections

如何使用从value获得的Map并使用key作为Map来查找薪水,以便在另一个bryan中找到薪水 }。

示例从第一张地图获取名称 - salary并显示第二张地图中的20000 import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Set; public class twomaps { public static void main(String [] args) { LinkedHashMap <Integer, String> hmap = new LinkedHashMap <Integer, String>(); LinkedHashMap <String, String> hmap2 = new LinkedHashMap <String, String>(); hmap.put(22, "bryan"); hmap.put(11,"Savi"); hmap2.put("16000", "Savi"); hmap2.put("20000", "bryan"); // i know this part is not correct but was writing something is better than nothing boolean b = hmap.values().contains("bryan"); if(b==true){ if(hmap2.values().equals(hmap2)) } } }

require_once("sql.php");

2 个答案:

答案 0 :(得分:0)

您可以使用以下代码执行此操作,但最好的方法是,创建Employee类型并将employee个对象存储到map中,这将真正使事情更清晰,面向对象

//check if both the maps contain the "bryan"
if(hmap.containsValue("bryan") && hmap2.containsValue("bryan")) {

     //iterate over the hmap2 using the keys
     for(String key : hmap2.keySet()) {

          //if the value is bryan, then get the key, which is the salary
          if(hmap2.get(key) != null && hmap2.get(key).equals("bryan")) {
              System.out.println(key);
          }
     }
 }

答案 1 :(得分:0)

试试这个简单的:

String name1 = "bryan";
hmap2.forEach((salary, name2) -> {
    if (name2.equals(name1)) {
        System.out.println(String.format("Salary for %s is %s", name1, salary));
    }
});