【发布时间】:2019-10-06 01:41:35
【问题描述】:
我目前使用的是 Java 8,无法切换到更高版本。由于我找不到类似的东西,也许有人知道已经实施的解决方案。可以是第三方库,也可以使用 lambda 等。没有限制。任何帮助表示赞赏!
有一个具有这种结构的列表,例如:
| ID | Date | Status | Name | Description | Currency |
|----|------------|--------|------|------------------|----------|
| 1 | 2019-10-01 | 1 | 'A' | 'Description A' | USD |
| 1 | 2019-10-01 | 3 | 'A' | 'Description A2' | USD |
| 1 | 2019-10-01 | 4 | 'A2' | 'Description A2' | USD |
| 2 | 2019-10-03 | 1 | 'B' | 'Description B' | USD |
| 2 | 2019-10-03 | 2 | 'B' | 'Description B' | BRL |
| 2 | 2019-10-03 | 4 | 'B' | 'Description B' | USD |
| 3 | 2019-10-05 | 1 | 'C' | 'Description C' | JPY |
| 3 | 2019-10-05 | 2 | 'C' | 'Description C2' | JPY |
问题是根据带有字段的原始列表创建另一个列表:
- 日期;
- 身份证;
- 时间状态已更改;
- 时间名称或描述已更改;
- 时代货币改变;
此外,还需要更多字段(如果这使问题过于复杂,我可以将其分成其他问题):
上次更改日期与特定 ID 的第一个日期之间的天数(称为“老化”);
显示更改的类型:
7.1 '名称/描述'如果仅更改名称或描述
7.2 仅更改货币时的“货币”
7.3 'Both' 如果名称 OR 描述 AND 货币发生变化
不必是List。它可以是任何类型的集合。
第一个列表来自 SQL 查询。这就是我开始编码的内容:
public List<Obj> getResultList() {
List<Obj> resultList = new ArrayList<>();
Map<String, Integer> counterMap = new HashMap<>();
counterMap.put("status", 0);
counterMap.put("name", 0);
counterMap.put("description", 0);
counterMap.put("currency", 0);
StreamEx.of(sourceList).forPairs((current, next) -> {
compareCurrentAndNext(current, next);
});
int countStatus = counterMap.get("status");
// etc
}
private void compareCurrentAndNext(final Obj current,
final Obj next) {
if (current.getId().equals(next.getId())) {
if (!current.getStatus().equals(next.getStatus())) {
counterMap.replace("status", counterMap.get("status") + 1);
}
// etc
}
}
它使用 StreamEx 库 (https://github.com/amaembo/streamex):
<dependency>
<groupId>one.util</groupId>
<artifactId>streamex</artifactId>
<version>0.7.0</version>
</dependency>
其实属性还有很多,这里我把真正的问题简化了。
【问题讨论】:
-
您尝试了什么,或者您只是希望我们为您编写代码? --- 您期望该输入的输出是什么?
-
在问题中添加了有关它的信息。
标签: java list count java-stream aggregate