搜索
您的当前位置:首页java中list对象转list <String>、转Map<String,Object>等各种复杂转换

java中list对象转list <String>、转Map<String,Object>等各种复杂转换

来源:世旅网

list对象转成list

List<String> memberIdList = list.stream().map(AssignDto::getMemberId)
				.collect(Collectors.toList());

list转map对象

Map<String, OrderVo> unMemberMap = orderVo.stream()
					.collect(Collectors.toMap(OrderVo::getMemberId,o -> o));

list转map自定义对象

Map<String, AssignDto> complatesMap = complateBalances.stream()
						.collect(Collectors.toMap(BusinessApplyBalance::getApplyPartyCode,
								balance -> new AssignDto(balance.getApplyPartyCode(), balance.getApplyChargeCode(),
										balance.getAmount(), CommonConstant.NO_AMOUNT)));

LIst排序

List<FundVoucherBalanceVo> sortList =  r.stream().sorted(Comparator.comparing(FundVoucherBalanceVo::getStatus).
	       thenComparing(FundVoucherBalanceVo::getValidTime,Comparator.nullsLast(Date::compareTo)).reversed()).collect(Collectors.toList());

对map的值的列表元素大小倒序排序

Map<String, List<TbAlgorithmVo>> algorithmVoMap
、List<Map.Entry<String, List<TbAlgorithmVo>>> algorithmVoMapList = algorithmVoMap.entrySet().stream()  
                .sorted(Collections.reverseOrder(Map.Entry.comparingByValue(Comparator.comparingInt(List::size))))  
                .collect(Collectors.toList());

List自定义排序

 List<SysMenu> listNew2 = listNew.stream()
                .sorted(Comparator.comparing(SysMenu::getOrderNum,(x,y)->{
                    if(x == null && y != null){
                        return 1;
                    }else if(x !=null && y == null){
                        return -1;
                    }else if(x == null && y == null){
                        return -1;
                    }else{
                        //按照读取的list顺序排序
                        for(SysMenu menu : listNew){
                            if(menu.getOrderNum().equals(x) || menu.getOrderNum().equals(y)){
                                if(x.equals(y)){
                                    return 0;
                                }else if(menu.getOrderNum().equals(x)){
                                    return -1;
                                }else{
                                    return 1;
                                }
                            }
                        }
                        return 0;
                    }
                })).collect(Collectors.toList());

list过滤符合条件的list数据

List<BusinessApplyBalance> processBalances = results.stream()
						.filter(balance -> !balance.getStatus().equals(BusinessApplyStatus.COMPLETED.getCateCode()))
						.collect(Collectors.toList());

list转换为list

List<AssignDto> soldProxyAmountList = r.getData().stream().map(balance -> new AssignDto(balance.getMemberId(),
						balance.getGuid(), balance.getSaleAmountMonth().subtract(balance.getSaledAmountMonth()), 
							CommonConstant.NO_AMOUNT))
						.collect(Collectors.toList());

list中根据一个条件做一个函数处理转Map

Map<String, Long> groupByMemberIdAmount = leftCouponAmounts.stream().collect(Collectors.groupingBy(
						o -> o.getMemberId(), Collectors.summingLong(o -> o.getBalanceAmount().longValue())));

list转map<String,List>

Map<String, List<UserAttendanceClockPeriodVo>> userRegistRecordMap = clockVoList.stream().collect(Collectors.toMap(UserAttendanceClockPeriodVo::getUserId, part ->
		  Lists.newArrayList(part),(List<UserAttendanceClockPeriodVo> newValueList,List<UserAttendanceClockPeriodVo> oldValueList)-> {	
	       		oldValueList.addAll(newValueList);
	       		return oldValueList;
	      }));	 
 第二种
 Map<String, List<TbAlgorithmVo>> algorithmVoMap = algoVoList.stream()
				.collect(Collectors.groupingBy(TbAlgorithmVo::getIndustrySceneNames));

LIst拷贝新LIst

List<TbCusAlarmRecordBO> alarmRecordList = BeanCopyUtils.copyListProperties(alarmRecordLists, TbCusAlarmRecordBO::new,
                    (s, t) -> {
                        t.setAlarmType(s.getAlarmType());
                        t.setAlarmTime(s.getAlarmTime());
                        TbCusAreaBO tbCusAreaBO = areaBOMap.get(s.getCameraGbtId());
                        t.setAreaCode(tbCusAreaBO.getAreaCode());
                        t.setAddress(tbCusAreaBO.getAddress());
                    });

因篇幅问题不能全部显示,请点此查看更多更全内容

Top