Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 流集合到其他结构_Java_Java 8_Java Stream - Fatal编程技术网

Java 流集合到其他结构

Java 流集合到其他结构,java,java-8,java-stream,Java,Java 8,Java Stream,我尝试将字典的列表对象转换为包含列表的对象 public class Dictionary { private String dictCode; private String dictName; private String dictDescription; private String dictElemCode; private String dictElemName; private String dictElemDescription; }

我尝试将
字典的列表对象转换为包含列表的对象

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;
}
结果应该是这样的:

a1
------ elem_code_1
------ elem_code_2
a2
------ elem_code_3
我的stream解决方案工作正常,但速度很慢,因为我不止一次使用列表流

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;
    }
publicstaticvoidmain(字符串[]args){
字典d1=新字典(“a1”、“dict1”、“desc1”、“elem_code_1”、“elem_code_name”、“elem_code_desc”);
字典d2=新字典(“a1”、“dict1”、“desc1”、“elem_code_2”、“elem_code_name2”、“elem_code_desc2”);
字典d3=新字典(“a2”、“dict2”、“desc2”、“elem_code_3”、“elem_code_name3”、“elem_code_desc3”);
列表=不可修改的列表(d1、d2、d3);
List newList=List.stream().filter(distinctByKey(Dictionary::getDictCode)).map(t->{
DictDTO dto=新DictDTO();
setCode(t.getDictCode());
setValue(t.getDictName());
setDescription(t.getDictDescription());
dto.setListDictElemdo(
list.stream().filter(e->e.getDictCode().equals(t.getDictCode()).map(w->{
DictElemDTO elemDto=新DictElemDTO();
setCode(w.getDictElemCode());
setValue(w.getDictElemName());
setDescription(w.getDictElemDescription());
返回elemdo;
}).collect(收集器.toList())
);
返回dto;
}).collect(Collectors.toList());
for(DictDTO-dto:newList){
System.err.println(dto.getCode());
for(DictElemDTO dtoE:dto.getListDictElemDTO()){
System.err.println(“----”+dtoE.getCode());
}
}
}
静态谓词distinctByKey(函数键提取器){
Map seen=新的ConcurrentHashMap();
返回t->seen.putIfAbsent(keydextractor.apply(t),Boolean.TRUE)==null;
}

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

这看起来像是
收集器的工作

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())));
Map
map=水流(d1、d2、d3)
.collect(收集器).groupingBy(字典::getDictCode,
Collectors.mapping(d->new DictElemDTO(d.getDictElemCode(),d.getDictElemName(),d.getdictelemddescription()),
收藏家;
这将为您提供字典代码到相应的
DictElemDTO
s列表的映射


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

这看起来像是
收集器的工作。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())));
Map
map=水流(d1、d2、d3)
.collect(收集器).groupingBy(字典::getDictCode,
Collectors.mapping(d->new DictElemDTO(d.getDictElemCode(),d.getDictElemName(),d.getdictelemddescription()),
收藏家;
这将为您提供字典代码到相应的
DictElemDTO
s列表的映射


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

为什么要使用流呢?我看不出使用流有多大价值(你只是用它来过滤…),你到底为什么要使用流呢?我不认为使用流有多大价值(你只是用它来过滤…),你能为这个问题添加完整的解决方案吗?你能为这个问题添加完整的解决方案吗?