java8 list 流操作

1、去重工具类

public class StreamtUtil {
    public 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;
    }
}

2.去重并赋值

        // 从数据库中查询出的对象集合
        List<HR> hrList= hRMapper.selectJoinByExample(example);
        // 判端是否为空
        if (CollectionUtils.isNotEmpty(hrList)) {
            // 进行去重拆解封装
            List<StaffVO> staffs= hrList.stream()
                .filter(StreamUtil.distinctByKey(HR::getStationId)
                .map(hr-> {
                    StaffVO staffVO = new StaffVO ();
                    staffVO.setId(hr.getStationId());
                    staffVO.setName(hr.getStationName());
                    return staffVO;
                }).collect(Collectors.toList());
        }

 3.获取对象关联id(字符串拼接一对多)去重转integer

        // 从数据库中查询出的对象集合
        List<HR> hrList= hRMapper.selectJoinByExample(example);
        // 判端是否为空
        if (CollectionUtils.isNotEmpty(hrList)) {
            // 将客户pid拼接成字符串("1,2","3","2,5,6","1,3")
            String pidStr = hrList.stream().map(HR::getPid()).collect(Collectors.joining(",");
            // 进行去重转换
            List<Integer> staffs= Arrays.asList(pidStr.split(",")).stream()
                .map(s-> Integer.valueOf(s.trim())).collect(Collectors.toList());
        }

 4.将对象中的两个字段转为map

List<User> list = new ArrayList<>();
Map<Integer, String> map = list.stream().collect(Collectors.toMap(User::getId, User::getType));

5.集合排序

List<OfficeDetail> detailList = queryList.stream().sorted(Comparator.comparing(OfficeDetail::getEndDate,
        Comparator.nullsLast(Long::compareTo)).
        thenComparing(OfficeDetail::getStartDate, Comparator.reverseOrder())).collect(Collectors.toList());

from tq_office
where groupID = #{groupID} and type = #{type} and status = 1 and action != 2 order by IF(endDate=0,99990101 ,endDate ) ASC ,startDate desc

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值