问题描述
解析文件后,我得到了CustomObject1的列表。 CustomObject1如下。
class CustomObject1{
List<CustomObject2> type;
List<CustomObject2> mapping;
}
class CustomObject2 {
String name;
String surname;
}
CustomObject1的列表示例就像
CustomObject2 obj1 = new CustomObject2();
CustomObject2 obj2 = new CustomObject2();
CustomObject2 type1 = new CustomObject2();
CustomObject2 type2 = new CustomObject2();
CustomObject1 listofObj1 = new CustomObject1(List.of(type1,type2),List.of(obj1,obj2));
另一个CustomObject实例
CustomObject2 obj3 = new CustomObject2();
CustomObject2 obj4 = new CustomObject2();
CustomObject2 type3 = new CustomObject2();
CustomObject2 type4 = new CustomObject2();
//Existing ** type1 ** has another mapping
CustomObject1 listofObj2 = new CustomObject1(List.of(type1,type3,type4),List.of(obj3,obj4));
//Final list of CustomObject1 that we need to convert is
List<CustomObject1> finalList = List.of(listofObj1,listofObj2);
预期的输出就像
{type1 = [obj1,obj2,obj3,obj4],type2 = [obj1,obj2],type3 = [obj3,obj4],type4 = [obj3,obj4]}
我用以下代码实现了映射
Map<CustomObject2,Set<CustomObject2>> finalMapping = new HashMap<>();
finalList.forEach(listItem-> listItem.getType().forEach(type -> {
if (!finalMapping.containsKey(type)) {
finalMapping.put(type,Set.copyOf(listItem.getMapping()));
} else {
Set<CustomObject2> combined = concat(finalMapping.get(type).stream(),listItem.getMapping().stream())
.collect(toSet());
finalMapping.put(type,combined);
}
}
));
是否有任何方法可以在Java 8中使用lamda实现到Map
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)