java8 List常用操作总结

@Data
public class TestDemo {

    //获取第pageNum页数据
    public static List<?> partition(ArrayList<?> list, int pageSize, int pageNum) {
        //将List按照pageSize拆分成多个List
        List<? extends List<?>> partition = Lists.partition(list, pageSize);
        //总页数
        int pages = partition.size();
        pageNum = pageNum <= 0 ? 0 : (pageNum <= (pages - 1) ? pageNum-1 : (pages - 1));
        return partition.get(pageNum);
    }

    public static void main(String[] args) {
        List<TestDemo> list = new ArrayList<>();
        list.add(new TestDemo(1L,11L,100L));
        list.add(new TestDemo(2L,11L,600L));
        list.add(new TestDemo(3L,33L,333L));
        list.add(new TestDemo(4L,33L,null));
        list.add(new TestDemo(5L,44L,null));
        //(一、转Map)如果有重复的key,则保留key1,舍弃key2
        Map<Long, TestDemo> map = list.stream().collect(Collectors.toMap(TestDemo::getId, item -> item, (key1, key2) -> key1));

        //(二、排序)
        // Comparator.comparing(类::属性一).reversed(); 得到排序结果后再排序
        //Comparator.comparing(类::属性一,Comparator.reverseOrder());是直接进行排序
        //升序、空值排在最前面、reversed反转排序
        List<TestDemo> sortList = list.stream().sorted(
                Comparator.comparing(TestDemo::getTotal, Comparator.nullsFirst(Long::compareTo)).reversed()
        ).collect(Collectors.toList());
        //属性一升序,属性二降序
        sortList = list.stream().sorted(
                Comparator.comparing(TestDemo::getId).thenComparing(TestDemo::getGroup, Comparator.reverseOrder())
        ).collect(Collectors.toList());

        //(三、分组)
        Map<Long, List<TestDemo>> groupMapList = list.stream().collect(Collectors.groupingBy(TestDemo::getGroup));
        Map<Long, TestDemo> groupMapObject = list.stream().collect(
                //取分组中的一条数据
                Collectors.groupingBy(TestDemo::getGroup, Collectors.collectingAndThen(Collectors.toList(), item -> item.get(0)))
        );
        //分组求和得到新集合
        List<TestDemo> groupSumList = new ArrayList<>();
        list.parallelStream().collect(Collectors.groupingBy(TestDemo::getGroup, Collectors.toList()))
                .forEach((id, groupList) -> {
                    groupList.stream().reduce(
                            (a, b) ->  new TestDemo(a.getId(), a.getGroup(), a.getTotal(), b.getTotal())
                    ).ifPresent(groupSumList::add);
                });

        //(四、去重)
        List<String> stringList = new ArrayList<String>() {{ add("A"); add("A"); add("B"); add("B"); add("C"); }};
        stringList = stringList.stream().distinct().collect(Collectors.toList());
        //对象属性去重
        List<TestDemo>  distinctList = list.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(()
                        -> new TreeSet<>(Comparator.comparing(TestDemo::getGroup))), ArrayList::new));

        //(五、提取)
        List<Long> groupNumList = list.stream()
            .map(TestDemo::getGroup) //流转化为Long
            .distinct() //去重
            .collect(Collectors.toList());

        //(六、过滤)
        List<TestDemo> filterList = list.stream().filter(item -> item.getTotal() != null && item.getTotal() < 200).collect(Collectors.toList());
        List<TestDemo> filtersList = list.stream()
                .filter(item -> {//多条件过滤
                    if(item.getTotal() == null) {
                        return false;
                    }
                    if (item.getTotal() > 200 && item.getTotal() < 400) {
                        return true;
                    }
                    return false;
                }).collect(Collectors.toList());

        //(七、取值)
        // 平均数
        Double avg1 = list.stream().filter(item -> item.getTotal() != null).mapToLong(TestDemo::getTotal).average().orElse(0);//为空的不参与计算
        Double avg2 = list.stream().filter(item -> {
            if(item.getTotal() == null) { //为空的参与计算
                item.setTotal(0L);//避免计算时报空指针错误
            }
            return true;
        }).collect(Collectors.averagingLong(TestDemo::getTotal));
        // 最大值
        Long max = list.stream().mapToLong(TestDemo::getTotal).max().orElse(0);
        // 最小值
        Long min = list.stream().mapToLong(TestDemo::getTotal).min().orElse(0);
        //求和
        Long sum = list.stream().mapToLong(TestDemo::getTotal).sum();
    }

    private Long id;
    private Long group;
    private Long total;
    public TestDemo(){}

    public TestDemo(Long id, Long group, Long total) {
        this.id = id;
        this.group = group;
        this.total = total;
    }

    public TestDemo(Long id, Long group, Long aTotal, Long bTotal) {
        this.id = id;
        this.group = group;
        this.total = (aTotal==null ? 0 : aTotal) + (bTotal==null ? 0 : bTotal);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值