/**
* Null-safely convert between two maps by applying a function to each key and value. A predicate is also specified to filter
* the results, in case the mapping function were to generate duplicate keys, etc.
*/
public static <T1, T2, U1, U2> Map<U1, U2> convert(Map<T1, T2> toConvert,
Function<? super T1, ? extends U1> keyConverter,
Function<? super T2, ? extends U2> valueConverter,
BiPredicate<U1, U2> resultFilter) {
if (toConvert == null) {
return null;
}
Map<U1, U2> result = toConvert.entrySet().stream()
.map(e -> new SimpleImmutableEntry<>(keyConverter.apply(e.getKey()),
valueConverter.apply(e.getValue())))
.filter(p -> resultFilter.test(p.getKey(), p.getValue()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
return Collections.unmodifiableMap(result);
}
TypeConverter.java 文件源码
java
阅读 41
收藏 0
点赞 0
评论 0
项目:aws-sdk-java-v2
作者:
评论列表
文章目录