gpt4 book ai didi

java - 将集合流传输到其他结构

转载 作者:行者123 更新时间:2023-12-02 02:32:52 25 4
gpt4 key购买 nike

我尝试将 Dictionary 的列表对象转换为包含列表的对象。

public class Dictionary {
private String dictCode;
private String dictName;
private String dictDescription;
private String dictElemCode;
private String dictElemName;
private String dictElemDescription;

}

//parent
public class DictDTO {

private String code;
private String value;
private String description;
private List<DictElemDTO> listDictElemDTO;
}

//children
public class DictElemDTO {

private String code;
private String value;
private String description;
}

Dictionary 类的示例数据:

Dictionary d1 = new Dictionary("a1", "dict1", "desc1", "elem_code_1", "elem_code_name", "elem_code_desc");
Dictionary d2 = new Dictionary("a1", "dict1", "desc1", "elem_code_2", "elem_code_name2", "elem_code_desc2");
Dictionary d3 = new Dictionary("a2", "dict2", "desc2", "elem_code_3", "elem_code_name3", "elem_code_desc3");

结果应该是这样的:

a1
------ elem_code_1
------ elem_code_2
a2
------ elem_code_3

我的流解决方案可以工作,但速度很慢,因为我多次使用列表流。

public static void main(String[] args) {
Dictionary d1 = new Dictionary("a1", "dict1", "desc1", "elem_code_1", "elem_code_name", "elem_code_desc");
Dictionary d2 = new Dictionary("a1", "dict1", "desc1", "elem_code_2", "elem_code_name2", "elem_code_desc2");
Dictionary d3 = new Dictionary("a2", "dict2", "desc2", "elem_code_3", "elem_code_name3", "elem_code_desc3");

List<Dictionary> list = ImmutableList.of(d1, d2, d3);


List<DictDTO> newList = list.stream().filter(distinctByKey(Dictionary::getDictCode)).map(t -> {
DictDTO dto = new DictDTO();
dto.setCode(t.getDictCode());
dto.setValue(t.getDictName());
dto.setDescription(t.getDictDescription());
dto.setListDictElemDTO(
list.stream().filter(e -> e.getDictCode().equals(t.getDictCode())).map(w -> {
DictElemDTO elemDto = new DictElemDTO();
elemDto.setCode(w.getDictElemCode());
elemDto.setValue(w.getDictElemName());
elemDto.setDescription(w.getDictElemDescription());
return elemDto;
}).collect(Collectors.toList())
);
return dto;
}).collect(Collectors.toList());

for (DictDTO dto : newList) {
System.err.println(dto.getCode());
for (DictElemDTO dtoE : dto.getListDictElemDTO()) {
System.err.println("------ " + dtoE.getCode());
}
}

}

static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
Map<Object, Boolean> seen = new ConcurrentHashMap<>();
return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}

在 Java 8 中使用流来解决此问题的更好方法是什么?

最佳答案

这看起来像是 Collectors.groupingBy 的工作:

Map<String,List<DictElemDTO>>
map = Stream.of(d1,d2,d3)
.collect(Collectors.groupingBy(Dictionary::getDictCode,
Collectors.mapping(d-> new DictElemDTO (d.getDictElemCode(),d.getDictElemName(),d.getDictElemDescription()),
Collectors.toList())));

这将为您提供字典代码到相应的 DictElemDTO 列表的映射。

创建 DictDTO 对象需要更多工作。

关于java - 将集合流传输到其他结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46828845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com