闲聊数组数据的处理java--Stream

关于一些数组数据的处理,一些常用的方法,在这里记录下,防止自己忘了还要来回找。

一般来说,对一些List的处理都牵涉到分组、排序、归并、定位等,中间可能牵涉到与数据库的交互,当然一些jar包中对数组数据都进行了比较好的封装,大家可以注意下,防止自己需要使用“特别”的方法实现,不仅麻烦,而且也不优雅。

1、数据分批

一些批数据处理,会牵涉到数据的分组。

List<Integer> list = Arrays.asList(23, 18, 20, 2, 12, 9, 6, 14, 1, 87, 15, 99);
//1、直接分组(推荐)
 List<List<Integer>> partition = Lists.partition(list, 10); //10个一组,封装到partition中
//2、可以个人处理
 int num = list.size()/10 + list.size()%10==0?0:1; //计算要切割成几份
 // 然后使用循环截取。不过要注意的是要和list的大小比较,防止下标溢出       
      for (int i = 0; i < num; i++) {
            if((i+1)*10<list.size()){
                List<Integer> integerList = list.subList(i * 10, (i + 1) * 10);
                // integerList to do sth
            } else {
                List<Integer> integerList = list.subList(i * 10, list.size());
                // integerList to do sth
            }
        }

2、数据求和

   List<Book> bookList = new ArrayList<>();
        // BigDecimal类型求和
        BigDecimal sum = bookList.stream().map(book -> book.getPrice()).reduce(BigDecimal.ZERO, BigDecimal::add);

        // int、double、long类型求和
        int sumInt = bookList.stream().mapToInt(book -> book.getWidth()).sum();
        long sumLong = bookList.stream().mapToLong(book -> book.getLength()).sum();
        double sumDouble = bookList.stream().mapToDouble(book -> book.getHight()).sum();

3、定制排序

//定制排序,可以根据条件定义排序,一个或多个条件均可以,下面适示意的是从大到小排序
// 判断条件是自己写的,可以根据需要来,仿写的话没啥大问题哈
public class Book implements Comparable<Book> {

    private String name;

    private BigDecimal price ;

    private Long length;

    private Integer width;

    private Double hight;

    @Override
    public int compareTo(Book o) {
        if(o.getWidth()>this.getWidth()){
            return 1;
        } else if(o.getWidth()<this.getWidth()){
            return -1;
        }
        return 0;
    }
 }

4、数据按条件分组

这种不知道大家遇到过没有,但是也是比较常用的,比如:查到一些书籍,根据书籍所在的人进行分组,当然,这个在数据库也能做,但是有些时候是需要在代码中处理的。还有就是批量子查询,然后数据对应的时候,就是一组一组对应的。


        List<Book> bookList = new ArrayList<>();

        Book book = new Book();
        book.setWidth(12);
        book.setName("Ebook");
        bookList.add(book);

        Book book1 = new Book();
        book1.setWidth(2);
        book1.setName("java");
        bookList.add(book1);

        Book book2 = new Book();
        book2.setWidth(17);
        book2.setName("java");
        bookList.add(book2);

        Map<String, List<Book>> groupMap= bookList.stream().collect(Collectors.groupingBy(vo -> vo.getName()));

        System.out.println(JSON.toJSONString(groupMap));

打印输出是按照名称进行分组的:

{"java":[{"name":"java","width":2},{"name":"java","width":17}],"Ebook":[{"name":"Ebook","width":12}]}

分组的处理使用的是vo -> vo.getName(),大家可以根据需要自己实现自己的逻辑

5、数据按key,value处理

扫描二维码关注公众号,回复: 13036118 查看本文章

有时候一个对象可能需要拆分成key,value的形式,方便后续的操作,这种也是有方法帮忙处理的

       List<Book> bookList = new ArrayList<>();

        Book book = new Book();
        book.setWidth(12);
        book.setName("Ebook");
        bookList.add(book);

        Book book1 = new Book();
        book1.setWidth(2);
        book1.setName("java");
        bookList.add(book1);

        Book book2 = new Book();
        book2.setWidth(17);
        book2.setName("java");
        bookList.add(book2);
        
        // 这个是固定写法,后面的 (p1, p2) -> p2)是指如果有两个value相同,是选择前一个p1,还是选择后一个p2作为map的value,如下,名称java对应的就是后一个了(17),value覆盖
        Map<String, Integer> map = bookList.stream().collect(Collectors.toMap(voKey -> voKey.getName(), voValue -> voValue.getWidth(), (p1, p2) -> p2));
        System.out.println(JSON.toJSONString(map));

当然,也可以把key一样的value累加或者拼接起来。

       Map<String, Integer> map = bookList.stream().collect(Collectors.toMap(voKey -> voKey.getName(), voValue -> voValue.getWidth(), (p1, p2) -> p1+p2));

补充:6.过滤去重

根据某个字段去重

        List<Book> bookList = new ArrayList<>();
        bookList.add(new Book("语文书", "12.3", "张三"));
        bookList.add(new Book("语文书", "12.3", "李四"));
        bookList.add(new Book("数学书", "16.3", "张三"));
        bookList.add(new Book("英语书", "11.3", "王五"));
        bookList.add(new Book("数学书", "16.3", "何柳"));
        // 过滤掉bookList中 书籍相同的数据
        List<Book> resultList = bookList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() ->
                new TreeSet<>(Comparator.comparing(Book::getName))), ArrayList::new));
        System.out.println("结果:" + JSONArray.toJSONString(resultList));

结果打印:

结果:[{"author":"张三","name":"数学书","price":"16.3"},{"author":"王五","name":"英语书","price":"11.3"},{"author":"张三","name":"语文书","price":"12.3"}]


看自己需要,如果还需要其他复杂的处理也可以实现,不过如果再复杂的话就要考虑下自己的设计了,看看是否可以优化设计哦~

暂时想到的list处理也就这么多,以后想到了再补充吧!

看着还行就给个赞呗~

No sacrifince ,no victory!

猜你喜欢

转载自blog.csdn.net/zsah2011/article/details/108848401