Java stream()用法集合单独取出对象中一个属性成为集合或数组

集合

List<DictEntity> dictEntityList = dictService.findByType(6);

取出属性为集合

List<String> stateNameList = dictEntityList.stream().map(DictEntity::getName).collect(Collectors.toList());

取出属性为数组

Long[] ids = dictEntityList.stream().map(DictEntity::getId).toArray(Long[]::new);

集合去重

dictEntityList.stream().distinct().collect(Collectors.toList());

 

posted @ 2021-07-20 09:34  南北12345678  阅读(10305)  评论(0编辑  收藏  举报