如何基于重复的父对象合并列表中的子对象 [英] How to merge child objects in list based on duplicate parent object

查看:87
本文介绍了如何基于重复的父对象合并列表中的子对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个具有一对多关系的实体,这些实体通过复合主键连接在一起.由于Spring Data为oracle数据库生成了错误的计数独特查询,因此我具有带笛卡尔连接的SQL输出,这导致子对象的每一行都为父对象重复一行.我需要基于组合键找出不同的父对象,然后在列表中添加父对象的每个子对象并将其设置为父对象的属性

I have two entities with one to many relationship which are joined together by composite primary keys. Since Spring Data generates wrong count distinct query for oracle database , I have SQL output with cartesian join which leads to repeating row for parent object for every row of child object. I need to find out distinct parent objects based on composite keys and then add each child object for parent in a list and set it as property of parent object

我能够根据父对象的组合键找出不同的父对象.以下是相关代码

I am able to find out distinct parent object based on composite keys of the parent object. Following is the relevant code

private static <T> Predicate<T> distinctByKeys(Function<? super T, ?>... keyExtractors)
{
final Map<List<?>, Boolean> seen = new ConcurrentHashMap<>();
return t ->
{
final List<?> keys = Arrays.stream(keyExtractors)
.map(ke -> ke.apply(t))
collect(Collectors.toList());

return seen.putIfAbsent(keys, Boolean.TRUE) == null;
};
}

假设我有以下输入内容

list.add(new Book("Core Java", 200, new ArrayList(){{add("Page 1");}}));
list.add(new Book("Core Java", 200, new ArrayList(){{add("Page 2");}}));
list.add(new Book("Learning Freemarker", 150, new ArrayList(){{add("Page 15");}}));
list.add(new Book("Spring MVC", 300, new ArrayList(){{add("Page 16");}}));
list.add(new Book("Spring MVC", 300, new ArrayList(){{add("Page 17");}}));

我需要产生以下输出

Core Java,200, [Page 1, Page 2]
Learning Freemarker,150, [Page 15]
Spring MVC,300 , [Page 16, Page 17]

在这方面的任何帮助都将非常有帮助

Any help in this regard will be very helpful

推荐答案

一种选择是使用Collectors.toMap(),并使用titlepages值作为键.如果发现重复项,则可以合并两个列表:

One option is to use Collectors.toMap() using the title and pages value as key. If you find duplicates you can merge both lists:

Collection<Book> result = list.stream()
        .collect(Collectors.toMap(
                b -> Map.entry(b.getTitle(), b.getPages()),
                b -> new Book(b.getTitle(), b.getPages(), b.getList()),
                (b1, b2) -> new Book(b1.getTitle(), b1.getPages(),
                        Stream.concat(b1.getList().stream(), b2.getList().stream()).collect(Collectors.toList())),
                LinkedHashMap::new))
        .values();

或者,您可以使用Collectors.groupingBy():

List<Book> result = list.stream().collect(
        Collectors.groupingBy(b -> Map.entry(b.getTitle(), b.getPages()), LinkedHashMap::new,
                Collectors.flatMapping(b -> b.getList().stream(), Collectors.toList())))
        .entrySet().stream()
        .map(e -> new Book(e.getKey().getKey(), e.getKey().getValue(), e.getValue()))
        .collect(Collectors.toList());

第二种方法使用titlepages作为键来创建Map,并使用Collectors.flatMapping()合并所有书籍的列表.之后,将条目映射回Book对象.

The second approach creates a Map using title and pages as key and merging the lists of all books using Collectors.flatMapping(). After that you map the entries back to your Book object.

如果您不能使用Collectors.flatMapping(),请改为使用Collectors.mapping()`:

If you can not use Collectors.flatMapping() use Collectors.mapping()` instead:

List<Book> r = list.stream().collect(
        Collectors.groupingBy(b -> Map.entry(b.getTitle(), b.getPages()), LinkedHashMap::new,
                Collectors.mapping(Book::getList, Collectors.toList())))
        .entrySet().stream()
        .map(e -> new Book(e.getKey().getKey(), e.getKey().getValue(), 
                e.getValue().stream().flatMap(Collection::stream).collect(Collectors.toList())))
        .collect(Collectors.toList());

如果不能使用Map.entry(),请改为使用new AbstractMap.SimpleEntry<>().

If you can not use Map.entry() use new AbstractMap.SimpleEntry<>() instead.

两种情况下的结果都是这样:

The result in both cases will be this:

Book[title='Core Java', pages=200, list=[Page 1, Page 2]]
Book[title='Learning Freemarker', pages=150, list=[Page 15]]
Book[title='Spring MVC', pages=300, list=[Page 16, Page 17]]

这篇关于如何基于重复的父对象合并列表中的子对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆