简体   繁体   English

在java中使用key和vlue从两个不同的Map中获取Map键和值

[英]Get Map key and value from two different Map with key and vlue in java

There are two Map, first one contains student id and name.有两个 Map,第一个包含学生 ID 和姓名。 second one contains student id and status of 'Pass' or 'Fail'.第二个包含学生 ID 和“通过”或“失败”状态。 There is a method which takes two parameters of Map type and returns Map.有一个方法接受两个 Map 类型的参数并返回 Map。

    //1st Map
    Map<Integer, String> map1 = new HashMap<>();
    map1.put(1, "x");
    map1.put(2, "y");
    map1.put(3, "z");
    map1.put(4, "a");
    map1.put(5, "b");`

and

    //2nd Map
    Map<Integer, String> map2 = new HashMap<>();
    map2.put(1, "fail");
    map2.put(2, "fail");
    map2.put(3, "fail");
    map2.put(4, "pass");
    map2.put(5, "pass");`

   //the method
public Map<Integer, String>findFaildStudent(Map<Integer, String>map1,Map<Integer, String>map2){
returns Map<Integer, String>;
 }

So, my question is how to find the record of failed students.所以,我的问题是如何找到失败学生的记录。 I did try it myself but didn't succeed.我自己也试过,但没有成功。 Any help is appreciated.任何帮助表示赞赏。

What I tried so far到目前为止我尝试过的

public Map<Integer, String>findFaildStudent(Map<Integer, String>map1,Map<Integer, String>map2){
    Integer key = null;
    String value = null;

    Iterator<Entry<Integer, String>> iterator = map1.entrySet().iterator();
    while(iterator.hasNext()){
        Entry<Integer, String> next = iterator.next();
        key = next.getKey();
    }

    Iterator<Entry<Integer, String>> iterator2 = map2.entrySet().iterator();
    while(iterator2.hasNext()){
        Entry<Integer, String> next = iterator2.next();
        value=next.getValue();
    }

    Map<Integer,String> hashMap = new HashMap<>();
    hashMap.put(key, value);

    return hashMap;

}
public Map<String, String> findFaildStudent(Map<Integer, String>map1,Map<Integer, String>map2){
    Map<String, String> failed = new HashMap<>();

    for (Map.Entry<Integer, String> k : map2.entrySet()) {
        if(k.getValue().equals("fail")){
            failed.put(map1.get(k.getKey()), map2.get(k.getValue()));
        }
    }
    return failed;
 }
//returns... ("x","failed"), ("y", "failed"), ("z", "failed")

So basically you just need to iterate through the 2nd map since it contain failed student, then you get the key and use that key to get the value of map1.所以基本上你只需要遍历第二张地图,因为它包含失败的学生,然后你得到键并使用该键来获取 map1 的值。 You cannot concate the keys because the key is unique so if you want combine both map together it is better to create another map using map1(value) as key and map2(value) as value.您无法连接键,因为键是唯一的,因此如果您想将两个映射组合在一起,最好使用 map1(value) 作为键和 map2(value) 作为值创建另一个映射。 Hope I understand your question but if you only want map2 of failed student then try below.希望我理解您的问题,但如果您只想要失败学生的 map2,请尝试以下操作。

public Map<Integer, String> findFaildStudent(Map<Integer, String>map1,Map<Integer, String>map2){
Map<Integer, String> failed = new HashMap<>();

for (Map.Entry<Integer, String> k : map2.entrySet()) {
    if(k.getValue().equals("fail")){
        failed.put(k.getKey(), map1.get(k.getKey()));
    }
}
return failed;
//returns... (1,"x"), (2, "y"), (3, "z")
map1.entrySet().stream()
    .filter(entry -> "fail".equals(map2.get(entry.getKey())))
    .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM