阅读 62

分享一点java8学习的知识

1.取出List里面Id的集合可以用stream这样操作:

Function<Map<String,object>,String> applyAssetFunction = applyAsset ->{

String objectId = (String)applyAsset.get(ClassNameCons.DATA_ENTITY.REF_DATA_OBJECT);

String mobjectId = (String)applyAsset.get(ClassNameCons.DATA_ENTITY.REF_M_DATA_OBJECT);

return StringUtils.isEmpty(ObjectId)?objectId:mobjectId

}

List dataObjectIds = assetInfoList.stream().map(applyAssetFunction).filter(item->!StringUtils.isEmpty(item)).collect(Collectors.toList)

2.取出list对象里面的某个字段的集合;

JSONArray devArrays = JSONObject.parseArray(devOffice);

List devUserList = devArrays.stream.map(obj ->(String)(JSONObject)obj).get("targetId")).collect(Collectors.toList());

3.Stream判断list里面是否有相关参数;返回boolean类型

boolean flag = applyApproveFlowListquery.stream().filter(ApplyApproveFlow ->"reject".equals(applyApproveFlow.getApproveOption())).findAny().ispresent();

4.Stream根据id进行去重

Stream wfProcessInstanceDTOStream = content.parallelStream().filter( (WfProcessInstanceDTO::getId)); List result = wfProcessInstanceDTOStream.collect(Collectors.toList());

5.stream去除重复对象的方法

List addNodeSetCollect = wfNodeSetList.stream().filter(item -> wfNodeSets.stream().noneMatch(tmp -> tmp.getProcTaskKey().equals(item.getProcTaskKey()))).collect(Collectors.toList()); List deleteNodeSetCollect = wfNodeSets.stream().filter(item -> wfNodeSetList.stream().noneMatch(tmp -> tmp.getProcTaskKey().equals(item.getProcTaskKey()))).collect(Collectors.toList());

6.Stream根据时间进行排序;

Stream sorted = content.stream().sorted(Comparator.comparing(WfProcessInstanceDTO::getTranspondTime)); List collect = sorted.collect(Collectors.toList());

7.元素逆序

list.stream().sorted(Comparator.comparing(Student::getAge).reversed())