JAVA stream使用自定义Collector

这篇博客展示了如何使用Java Stream API和自定义Collector实现数据的分组和转换,将Book数据按category和level进行归类,转换为更易读的格式。文章通过一个具体的例子和测试用例,详细解释了Collector接口的实现过程。
摘要由CSDN通过智能技术生成

Book数据

[
  {
    "name": "name-1-1",
    "category": 1,
    "level": 1,
    "author": "a",
    "price": 22.0
  },
  {
    "name": "name-1-2",
    "category": 1,
    "level": 2,
    "author": "b",
    "price": 21.0
  },
  {
    "name": "name-1-3",
    "category": 1,
    "level": 3,
    "author": "c",
    "price": 22.0
  },
  {
    "name": "name-2-1",
    "category": 2,
    "level": 1,
    "author": "d",
    "price": 22.0
  },
  {
    "name": "name-3-1",
    "category": 3,
    "level": 1,
    "author": "e",
    "price": 22.0
  },
  {
    "name": "name-1-1--",
    "category": 1,
    "level": 1,
    "author": "f",
    "price": 22.0
  },
  {
    "name": "name-2-1--",
    "category": 2,
    "level": 1,
    "author": "g",
    "price": 22.0
  },
  {
    "name": "name",
    "category": 1,
    "level": 1,
    "author": "h",
    "price": 22.0
  }
]

转成 每个category每个level book详情

{
    "1":[
        {
            "level":1,
            "books":[
                {
                    "name":"name-1-1",
                    "price":22,
                    "author":"a"
                },
                {
                    "name":"name-1-1--",
                    "price":22,
                    "author":"f"
                },
                {
                    "name":"name",
                    "price":22,
                    "author":"h"
                }
            ]
        },
        {
            "level":2,
            "books":[
                {
                    "name":"name-2-1",
                    "price":22,
                    "author":"d"
                },
                {
                    "name":"name-2-1--",
                    "price":22,
                    "author":"g"
                }
            ]
        },
        {
            "level":3,
            "books":[
                {
                    "name":"name-3-1",
                    "price":22,
                    "author":"e"
                }
            ]
        }
    ],
    "2":[
        {
            "level":1,
            "books":[
                {
                    "name":"name-1-2",
                    "price":21,
                    "author":"b"
                }
            ]
        }
    ],
    "3":[
        {
            "level":1,
            "books":[
                {
                    "name":"name-1-3",
                    "price":22,
                    "author":"c"
                }
            ]
        }
    ]
}

自定义Collector

@Data
public class Book {

    /**
     * 书名
     */
    private String name;
    /**
     * 品类
     */
    private Integer category;

    /**
     * 级别:入门,初级,。。。
     */
    private Integer level;
    /**
     * 作者
     */
    private String author;
    /**
     * 价格
     */
    private Double price;

    @Data
    public static class Group {

        private Integer level;

        private List<BookInfo> books;

        @Data
        public static class BookInfo {

            private String name;
            private Double price;
            private String author;
        }
    }

    public static class MyCollector implements Collector<Book, Table<Integer, Integer, List<Group.BookInfo>>, Map<Integer, List<Group>>> {

        /**
         * 初始化中间容器
         */
        @Override
        public Supplier<Table<Integer, Integer, List<Group.BookInfo>>> supplier() {
            return HashBasedTable::create;
        }

        /**
         * 输入放入到中间容器
         */
        @Override
        public BiConsumer<Table<Integer, Integer, List<Group.BookInfo>>, Book> accumulator() {
            return (table, book) -> {
                List<Group.BookInfo> books = table.get(book.getCategory(), book.getLevel());
                if (books == null) {
                    books = new ArrayList<>();
                }
                Group.BookInfo info = new Group.BookInfo();
                info.setAuthor(book.getAuthor());
                info.setName(book.getName());
                info.setPrice(book.getPrice());
                books.add(info);
                table.put(book.getCategory(), book.getLevel(), books);
            };
        }

        /**
         * 重复项合并
         */
        @Override
        public BinaryOperator<Table<Integer, Integer, List<Group.BookInfo>>> combiner() {
            return (a, b) ->
                Stream.concat(a.cellSet().stream(), b.cellSet().stream())
                    .collect(Tables.toTable(Table.Cell::getRowKey,
                        Table.Cell::getColumnKey,
                        Table.Cell::getValue,
                        (x, y) -> {
                            x.addAll(y);
                            return x;
                        }, HashBasedTable::create));
        }

        /**
         * 容器输出
         */
        @Override
        public Function<Table<Integer, Integer, List<Group.BookInfo>>, Map<Integer, List<Group>>> finisher() {
            return (table) -> {
                Map<Integer, List<Group>> result = new HashMap<>();
                table.columnMap().forEach((key, value) -> {
                    List<Group> groups = value.entrySet().stream()
                        .map(t -> {
                            Group group = new Group();
                            group.setBooks(t.getValue());
                            group.setLevel(t.getKey());
                            return group;
                        }).collect(Collectors.toList());
                    result.put(key, groups);
                });
                return result;
            };
        }

        @Override
        public Set<Characteristics> characteristics() {
            return EnumSet.of(Characteristics.CONCURRENT);
        }
    }
}

测试


	private List<Book> books;
    private Gson gson;

 	@Before
    public void before() {
        gson = new Gson();
        InputStream inputStream = this.getClass().getResourceAsStream("/book.json");
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream))) {
            String json = buffer.lines().collect(Collectors.joining());
            Gson gson = new Gson();
            TypeToken<List<Book>> typeToken = new TypeToken<>() { };
            books = gson.fromJson(json, typeToken.getType());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void myCollectorTest() {
        Map<Integer, List<Book.Group>> collect = books.stream().collect(new Book.MyCollector());
        System.out.println(gson.toJson(collect));
    }

源码地址

https://github.com/googalAmbition/googol/blob/master/java-base/src/main/java/com/tcoding/demo/base/stream/Book.java

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tcoding

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值