Java 8 从HashMap筛选值<;整数,列表<;对象>&燃气轮机;使用Java8概念

Java 8 从HashMap筛选值<;整数,列表<;对象>&燃气轮机;使用Java8概念,java-8,stream,Java 8,Stream,我的输入: 结构为hashmap的hashmap,其中List可以包含子对象的实例 我的期望: 使用Java8 streams概念提取HashMap的子集,其中列表中的对象是instaceOf Child类,并且子类的finder属性具有特定值(例如test) 示例: 输入 { 1=[Child[finder=test],Parent[],Child[finder=test], 2=[Child[finder=text],Parent[],Parent[],Parent[], 3=[Child[

我的输入:
结构为
hashmap
的hashmap,其中
List
可以包含子对象的实例

我的期望:
使用Java8 streams概念提取
HashMap
的子集,其中列表中的对象是
instaceOf Child
类,并且子类的
finder
属性具有特定值(例如test)

示例:
输入

{
1=[Child[finder=test],Parent[],Child[finder=test],
2=[Child[finder=text],Parent[],Parent[],Parent[],
3=[Child[finder=test],Child[finder=test],Parent[]]
}

输出

{
1=[Child[finder=test],Child[finder=test],
3=[Child[finder=test],Child[finder=test]
}

代码

下面是我的类结构,其中有
父类
子类
。还有
Hashmap
对象,其中key是整数,值为
List

测试数据

Map<Integer, List<Parent>> map = new HashMap<>();
map.put(1, List.of(new Child("test"),new Parent(),
        new Child("test"), new Child("Foo"), new Parent()));
map.put(2, List.of(new Parent(), new Child("text")));
map.put(3, List.of(new Parent(), new Child("Bar"), 
        new Child("test"), new Parent()));


我向子类添加了一个getter来检索finder值。

谢谢您的帮助:)
Map<Integer, List<Parent>> map = new HashMap<>();
map.put(1, List.of(new Child("test"),new Parent(),
        new Child("test"), new Child("Foo"), new Parent()));
map.put(2, List.of(new Parent(), new Child("text")));
map.put(3, List.of(new Parent(), new Child("Bar"), 
        new Child("test"), new Parent()));

Map<Integer, List<Parent>> map2 = map.entrySet().stream()
        .flatMap((Entry<Integer, List<Parent>> e) -> e
                .getValue().stream()
                .map(v -> new AbstractMap.SimpleEntry<>(
                        e.getKey(), v)))
        .filter(obj -> obj.getValue() instanceof Child && 
                ((Child)obj.getValue()).getFinder().equals("test"))
        .collect(Collectors.groupingBy(Entry::getKey,
                Collectors.mapping(Entry::getValue,
                        Collectors.toList())));

map2.entrySet().forEach(System.out::println);
1=[Child [finder=test], Child [finder=test]]
3=[Child [finder=test]]