Java 8复制地图以及其他地图的过滤器

时间:2019-03-29 20:44:40

标签: java-8 hashmap java-stream

我试图通过与仅包含ID的键集的条件图进行比较来过滤原始图。基于条件图,我想从原始图进行过滤。

我拥有的原始地图是

Map<Integer, AppFeatureDTO> appFeatureMap = new TreeMap<>();

其结果将类似于

{
    "101": {
        "id": 101,
        "subFeature": {
            "1": {
                "id": 1,
                "title": "Title Value",
                "desc": "Description Value"
            }
        }
    },
    "102": {
        "id": 102,
        "subFeature": {
            "1": {
                "id": 1,
                "title": "Title Value",
                "desc": "Description Value"
            }
        }
    },
    "103": {
        "id": 103,
        "subFeature": {
            "1": {
                "id": 1,
                "title": "Title Value",
                "desc": "Description Value"
            },
            "2": {
                "id": 2,
                "title": "Title Value",
                "desc": "Description Value"
            },
            "3": {
                "id": 3,
                "title": "Title Value",
                "desc": "Description Value"
            },
            "4": {
                "id": 4,
                "title": "Title Value",
                "desc": "Description Value"
            }
        }
    }
}

和相应的类是:

class AppFeatureDTO {
    private int id;
    private Map<Integer, AppSubFeatureDTO> subFeature;
}
class AppSubFeatureDTO{
    private int id;
    private String title;
    private String description;
}

然后我有一个过滤器图,

Map<Integer, FeatureDTO> conditionFilterMap = new TreeMap<>();

结果类似

{
    "101": {
        "id": 101,
        "subFeature": {
            "1": {
                "id": 1,
            }
        }
    },
    "103": {
        "id": 103,
        "subFeature": {
            "2": {
                "id": 2
            },
            "4": {
                "id": 4
            }
        }
    }
}

过滤器映射的对应类是

class FeatureDTO {
    private int id;
    private Map<Integer, SubFeatureDTO> subFeature;
}
class SubFeatureDTO{
    private int id;
}

我想使用conditionFilterMap来过滤结果图,

{
"101": {
    "id": 101,
    "subFeature": {
        "1": {
            "id": 1,
            "title": "Title Value",
            "desc": "Description Value"
        }
    }
},
"103": {
    "id": 103,
    "subFeature": {
        "2": {
            "id": 2,
            "title": "Title Value",
            "desc": "Description Value"
        },
        "4": {
            "id": 4,
            "title": "Title Value",
            "desc": "Description Value"
        }
    }
}

}

我正在使用spring modelMapper将AppFeatureDTO复制到FeatureDTO。但是,过滤地图后,我没有发现任何线索。

您能否建议如何使用Java 8获取resultMap

即使我也无法想象使用Java 7或6的简单解决方案。

1 个答案:

答案 0 :(得分:1)

假设地图关键字与id字段相同:

Map<Integer, AppFeatureDTO> resultMap = conditionFilterMap.values().stream()
        .map(FeatureDTO::getId)
        .map(appFeatureMap::get)
        .collect(Collectors.toMap(AppFeatureDTO::getId, a -> new AppFeatureDTO(a.getId(),
                conditionFilterMap.get(a.getId()).getSubFeature().values().stream()
                        .map(SubFeatureDTO::getId)
                        .map(a.getSubFeature()::get)
                        .collect(Collectors.toMap(AppSubFeatureDTO::getId, x -> x)))));

如果需要TreeMap,请将参数(a, b) -> a, TreeMap::new添加到Collectors.toMap调用中。

非流媒体版本看起来并不差:

Map<Integer, AppFeatureDTO> resultMap = new TreeMap<>();
for (FeatureDTO f : conditionFilterMap.values()) {
    AppFeatureDTO a = appFeatureMap.get(f.getId());
    Map<Integer, AppSubFeatureDTO> resultSub = new TreeMap<>();
    for (SubFeatureDTO s : f.getSubFeature().values()) {
        resultSub.put(s.getId(), a.getSubFeature().get(s.getId()));
    }
    resultMap.put(a.getId(), new AppFeatureDTO(a.getId(), resultSub));
}