Java 如何使用Lamda表达式基于另一个贴图过滤贴图的贴图

Java 如何使用Lamda表达式基于另一个贴图过滤贴图的贴图,java,filter,java-stream,Java,Filter,Java Stream,我有一张地图,需要使用lambda表达式根据另一张地图进行过滤 我尝试在地图上进行过滤,并根据另一个地图查找所有匹配项,但似乎不起作用。这些值似乎没有正确过滤。 是否有一种方法可以执行流和映射,并将过滤逻辑放在那里? 有人能帮忙吗 public static void main(String []args){ System.out.println("Hello World"); Map<String,List<String>> items = new H

我有一张地图,需要使用lambda表达式根据另一张地图进行过滤

我尝试在地图上进行过滤,并根据另一个地图查找所有匹配项,但似乎不起作用。这些值似乎没有正确过滤。 是否有一种方法可以执行流和映射,并将过滤逻辑放在那里? 有人能帮忙吗

 public static void main(String []args){
    System.out.println("Hello World");
    Map<String,List<String>> items = new HashMap<>();
    List<String> ut1=new ArrayList<>();
    ut1.add("S");
    ut1.add("C");
    List<String> ut2=new ArrayList<>();
    ut2.add("M");
    List<String> ut3=new ArrayList<>();
    ut3.add("M");
    ut3.add("C");
    items .put("1010016",ut1);
    items .put("1010019",ut2);
    items .put("1010012",ut3);
    System.out.println("Map"+items);

    Map<String,Map<String,String>> sKey = new HashMap<>();
    Map<String,String> utKey1 = new HashMap<>();
    utKey1.put("S","1001");
    utKey1.put("M","1002");
    utKey1.put("C","1003");

    Map<String,String> utKey2 = new HashMap<>();
    utKey2.put("S","1004");

    Map<String,String> utKey3 = new HashMap<>();
    utKey3.put("S","1005");
    utKey3.put("M","1006");

    Map<String,String> utKey4 = new HashMap<>();
    utKey4.put("S","1007");
    utKey4.put("M","1008");
    utKey4.put("C","1009");

    sKey.put("1010016",utKey1);
    sKey.put("1010019",utKey2);
    sKey.put("1010012",utKey3);
    sKey.put("1010011",utKey4);

    System.out.println("Map2"+sKey);

    Map<String,Map<String,String>> map3 =
        sKey.entrySet().stream()
            .filter(x -> 
                items.containsKey(x.getKey())
                && x.getValue().entrySet().stream().allMatch(y -> 
                    items.entrySet().stream().anyMatch(list ->
                        list.getValue().contains(y.getKey()))))
            .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
    System.out.println("Map3"+map3);
 }
过滤后的映射返回为:

Map3{1010012={S=1005,M=1006},1010016={S=1001,C=1003,M=1002},1010019={S=1004}

但实际结果应该是:

Map3{1010012={M=1006},1010016={S=1001,C=1003}


我更愿意说,这是一种使用流实现预期输出的变通方法

Map<String, Map<String, String>> result = 
    sKey.entrySet().stream()
    .filter(detail -> items.keySet().contains(detail.getKey()) && 
            !Collections.disjoint(detail.getValue().keySet(), items.get(detail.getKey())))
    .collect(HashMap::new, 
            (m,v) -> m.put(v.getKey(), v.getValue().entrySet().stream()
                .filter(detail -> items.get(v.getKey()).contains(detail.getKey()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))), 
            HashMap::putAll);

乍一看,我看不出你是如何达到预期结果的。我正在重新格式化代码,以便在您的流管道获取一个输入映射、过滤掉它的一些条目并构建一个包含所有未过滤掉的条目的映射时读取它。因此,您不能期望新映射中的键与输入映射中的相同键具有不同的值。有更好的方法吗?不要使用allMatch和anyMatch?尝试重新阅读重新格式化的代码,我认为它可以更清楚地显示问题。你将需要新的列表,我会在你的收集器中这样做。使用MapkeySet和SetretainAll将大大缩短此代码有没有一种方法可以在那里执行流、映射和编写所有过滤逻辑?我试过这样做,但没有成功。
{1010012={M=1006}, 1010016={S=1001, C=1003}}