根据 Map 输入从 Collection<Objects> 中提取对象
Extract Objects from Collection<Objects> based on a Map input
我收集了大约 13360 个帐户对象,如下所示
输入数据:
Account(id,date,balance,region,cost)
Account("1","2019-07-24","X,"Y","Z")
Account("1","2019-07-24","C,"Y","Z")
Account("1","2019-07-23","X,"D","Z")
Account("1","2019-07-23","X,"Y","f")
Account("1","2019-07-22","X,"s","Z")
Account("2","2019-07-23","X,"A","Z")
Account("2","2019-07-23","X,"Y","d")
Account("2","2019-07-22","d,"Y","Z")
Account("2","2019-07-23","X,"s","Z")
Account("3","2019-07-24","d,"Y","d")
Account("4","2019-07-24","X,"Y","Z")
Account("4","2019-07-23","d,"Y","Z")
Account("5","2019-07-23","X,"d","Z")
Account("5","2019-07-22","X,"Y","Z")
过滤条件:
Map<id,date>
(1,2019-07-24), (2,2019-07-23),(5,2019-07-23)
预期结果是
Account("1","2019-07-24","X,"Y","Z")
Account("1","2019-07-24","C,"Y","Z")
Account("2","2019-07-23","X,"A","Z")
Account("2","2019-07-23","X,"Y","d")
Account("2","2019-07-23","X,"s","Z")
Account("5","2019-07-23","X,"d","Z")
因此我想检索某些帐户的最新数据
下面的代码示例只为我提供最近日期的数据,即特定帐户列表的(今天的日期)。但对于某些帐户,我没有今天日期的数据,因此我需要检索可用的最新数据
EntryObject eo = new PredicateBuilder.getEntryObject();
Predicate p = eo.get("id").in(1,2,5).and(eo.get("date").equals(todaysdate))
Collection<Account> coll = accounts.values(p);
您可以在 filteras:
中使用 anyMatch
List<Account> filteredAccounts = acccounts.stream()
.filter(acc -> map.entrySet()
.stream()
.anyMatch(e -> e.getKey().equals(acc.getId())
&& e.getValue().equals(acc.getDate())))
.collect(Collectors.toList());
这是一个更通用的解决方案,只获取一组 accountIds
的最新条目:
Set<Integer> accountIds = Set.of(1, 2, 5);
List<Account> result = accounts.stream()
.filter(a -> accountIds.contains(a.getId()))
.collect(Collectors.groupingBy(Account::getId, Collectors.groupingBy(Account::getDate, TreeMap::new, Collectors.toList())))
.values().stream()
.flatMap(m -> m.lastEntry().getValue().stream())
.collect(Collectors.toList());
首先,您仅根据 ID 过滤所需的帐户。之后你按 id 和日期对它们进行分组,这会给你这个中间结果:
{
1: {
2019-07-22: [{id: 1, date: 2019-07-22, balance: 'X', region: 's', cost: 'Z'}],
2019-07-23: [{id: 1, date: 2019-07-23, balance: 'X', region: 'D', cost: 'Z'}, {id: 1, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'f'}],
2019-07-24: [{id: 1, date: 2019-07-24, balance: 'X', region: 'Y', cost: 'Z'}, {id: 1, date: 2019-07-24, balance: 'C', region: 'Y', cost: 'Z'}]
},
2: {
2019-07-22: [{id: 2, date: 2019-07-22, balance: 'd', region: 'Y', cost: 'Z'}],
2019-07-23: [{id: 2, date: 2019-07-23, balance: 'X', region: 'A', cost: 'Z'}, {id: 2, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'd'}, {id: 2, date: 2019-07-23, balance: 'X', region: 's', cost: 'Z'}]
},
5: {
2019-07-22: [{id: 5, date: 2019-07-22, balance: 'X', region: 'Y', cost: 'Z'}],
2019-07-23: [{id: 5, date: 2019-07-23, balance: 'X', region: 'd', cost: 'Z'}]
}
}
最后,您只使用生成的地图的值,flatMap
它与分组 TreeMap 的最后一个值一起使用,以仅获取具有最新日期的列表。
最终结果是这样的:
[
{id: 1, date: 2019-07-24, balance: 'X', region: 'Y', cost: 'Z'},
{id: 1, date: 2019-07-24, balance: 'C', region: 'Y', cost: 'Z'},
{id: 2, date: 2019-07-23, balance: 'X', region: 'A', cost: 'Z'},
{id: 2, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'd'},
{id: 2, date: 2019-07-23, balance: 'X', region: 's', cost: 'Z'},
{id: 5, date: 2019-07-23, balance: 'X', region: 'd', cost: 'Z'}
]
我收集了大约 13360 个帐户对象,如下所示
输入数据:
Account(id,date,balance,region,cost)
Account("1","2019-07-24","X,"Y","Z")
Account("1","2019-07-24","C,"Y","Z")
Account("1","2019-07-23","X,"D","Z")
Account("1","2019-07-23","X,"Y","f")
Account("1","2019-07-22","X,"s","Z")
Account("2","2019-07-23","X,"A","Z")
Account("2","2019-07-23","X,"Y","d")
Account("2","2019-07-22","d,"Y","Z")
Account("2","2019-07-23","X,"s","Z")
Account("3","2019-07-24","d,"Y","d")
Account("4","2019-07-24","X,"Y","Z")
Account("4","2019-07-23","d,"Y","Z")
Account("5","2019-07-23","X,"d","Z")
Account("5","2019-07-22","X,"Y","Z")
过滤条件:
Map<id,date>
(1,2019-07-24), (2,2019-07-23),(5,2019-07-23)
预期结果是
Account("1","2019-07-24","X,"Y","Z")
Account("1","2019-07-24","C,"Y","Z")
Account("2","2019-07-23","X,"A","Z")
Account("2","2019-07-23","X,"Y","d")
Account("2","2019-07-23","X,"s","Z")
Account("5","2019-07-23","X,"d","Z")
因此我想检索某些帐户的最新数据
下面的代码示例只为我提供最近日期的数据,即特定帐户列表的(今天的日期)。但对于某些帐户,我没有今天日期的数据,因此我需要检索可用的最新数据
EntryObject eo = new PredicateBuilder.getEntryObject();
Predicate p = eo.get("id").in(1,2,5).and(eo.get("date").equals(todaysdate))
Collection<Account> coll = accounts.values(p);
您可以在 filteras:
中使用anyMatch
List<Account> filteredAccounts = acccounts.stream()
.filter(acc -> map.entrySet()
.stream()
.anyMatch(e -> e.getKey().equals(acc.getId())
&& e.getValue().equals(acc.getDate())))
.collect(Collectors.toList());
这是一个更通用的解决方案,只获取一组 accountIds
的最新条目:
Set<Integer> accountIds = Set.of(1, 2, 5);
List<Account> result = accounts.stream()
.filter(a -> accountIds.contains(a.getId()))
.collect(Collectors.groupingBy(Account::getId, Collectors.groupingBy(Account::getDate, TreeMap::new, Collectors.toList())))
.values().stream()
.flatMap(m -> m.lastEntry().getValue().stream())
.collect(Collectors.toList());
首先,您仅根据 ID 过滤所需的帐户。之后你按 id 和日期对它们进行分组,这会给你这个中间结果:
{
1: {
2019-07-22: [{id: 1, date: 2019-07-22, balance: 'X', region: 's', cost: 'Z'}],
2019-07-23: [{id: 1, date: 2019-07-23, balance: 'X', region: 'D', cost: 'Z'}, {id: 1, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'f'}],
2019-07-24: [{id: 1, date: 2019-07-24, balance: 'X', region: 'Y', cost: 'Z'}, {id: 1, date: 2019-07-24, balance: 'C', region: 'Y', cost: 'Z'}]
},
2: {
2019-07-22: [{id: 2, date: 2019-07-22, balance: 'd', region: 'Y', cost: 'Z'}],
2019-07-23: [{id: 2, date: 2019-07-23, balance: 'X', region: 'A', cost: 'Z'}, {id: 2, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'd'}, {id: 2, date: 2019-07-23, balance: 'X', region: 's', cost: 'Z'}]
},
5: {
2019-07-22: [{id: 5, date: 2019-07-22, balance: 'X', region: 'Y', cost: 'Z'}],
2019-07-23: [{id: 5, date: 2019-07-23, balance: 'X', region: 'd', cost: 'Z'}]
}
}
最后,您只使用生成的地图的值,flatMap
它与分组 TreeMap 的最后一个值一起使用,以仅获取具有最新日期的列表。
最终结果是这样的:
[
{id: 1, date: 2019-07-24, balance: 'X', region: 'Y', cost: 'Z'},
{id: 1, date: 2019-07-24, balance: 'C', region: 'Y', cost: 'Z'},
{id: 2, date: 2019-07-23, balance: 'X', region: 'A', cost: 'Z'},
{id: 2, date: 2019-07-23, balance: 'X', region: 'Y', cost: 'd'},
{id: 2, date: 2019-07-23, balance: 'X', region: 's', cost: 'Z'},
{id: 5, date: 2019-07-23, balance: 'X', region: 'd', cost: 'Z'}
]