如何以及何时转换不同类型的对象列表的列表
我想知道何时(分组后和收集到list< list< object>>
)以及如何(在收集收集的组后重新流程)。
我可以将每个对象的每个子列表转换
[Exon{45 2021-01-01 4}, Exon{45 2021-01-01 4}, Sup{45 2021-01-01 4}]
and
[Exon{33 2023-01-01 2}, Sup{33 2023-01-01 2}]
and
[Exon{21 2022-01-01 5}, Sup{21 2022-01-01 5}]
为一个名为globaldata
的新对象类型吗?
任何建议将不胜感激。
这个问题与我以前的问题有关: https://stackoverflow.com/a/a/72368904/3072470
这是Dan提供的代码收集到列表的对象列表: https://ideone.com/ostitq
public class GlobalData {
private UUID id = UUID.randomUUID();
private IncomeCode incomeCode;
private LocalDate endDate;
private String codeRef;
private boolean taxable;
private String pw = "";
private BigDecimal area = BigDecimal.ZERO;
//private BigDecimal taxableIncome = BigDecimal.ZERO;
private BigDecimal value = BigDecimal.ZERO;
private BigDecimal exonValueType = BigDecimal.ZERO;
private BigDecimal Income = BigDecimal.ZERO;
private BigDecimal exemptedIncome = BigDecimal.ZERO;
private BigDecimal refIncome = BigDecimal.ZERO;
}
List<GlobalData> globalTaxDataList = listRes.stream()
.map(this::toGlobalData)
.collect(Collectors.toList());
private GlobalData toGlobalData(List<Object> objects) {
GlobalData globalData = new GlobalData();
objects // list of mix Exons and Sups objects (Exon and Sup contain more, fields incomeCode, endDate and codeRef are used only for grouping) ex: [Exon{45 2021-01-01 4}, Exon{45 2021-01-01 4}, Sup{45 2021-01-01 4}]
.forEach(o -> {
if (o instanceof Exon) {
globalData.setValue(globalData.getValue()
.add(o.getValue()))
if (o.getxxx()> 100) {
globalData.setIncome(globalData.getIncome()
.add(calcMethod(o)))
}
//more business code here
}
if (o instanceof Sup) {
globalData.setValue(globalData.getValue()
.add(o.getValue()))
if (o.getxxx()> 100) {
globalData.setIncome(globalData.getIncome()
.add(calcMethod(o)))
}
//more business code here
}
});
return globalData;
}
I'm wondering WHEN (just after grouping and before collecting to List<List<Object>>
) and HOW (re-stream the collected groups after collecting them).
Can I transform each sub-list of objects
[Exon{45 2021-01-01 4}, Exon{45 2021-01-01 4}, Sup{45 2021-01-01 4}]
and
[Exon{33 2023-01-01 2}, Sup{33 2023-01-01 2}]
and
[Exon{21 2022-01-01 5}, Sup{21 2022-01-01 5}]
into a new object type called GlobalData
?
Any suggestion will be appreciated.
This question is linked to my previous question :
https://stackoverflow.com/a/72368904/3072470
Here is the code provided by Dan for grouping my different Objects into a list of objects collected to a list:
https://ideone.com/OSTItQ
public class GlobalData {
private UUID id = UUID.randomUUID();
private IncomeCode incomeCode;
private LocalDate endDate;
private String codeRef;
private boolean taxable;
private String pw = "";
private BigDecimal area = BigDecimal.ZERO;
//private BigDecimal taxableIncome = BigDecimal.ZERO;
private BigDecimal value = BigDecimal.ZERO;
private BigDecimal exonValueType = BigDecimal.ZERO;
private BigDecimal Income = BigDecimal.ZERO;
private BigDecimal exemptedIncome = BigDecimal.ZERO;
private BigDecimal refIncome = BigDecimal.ZERO;
}
List<GlobalData> globalTaxDataList = listRes.stream()
.map(this::toGlobalData)
.collect(Collectors.toList());
private GlobalData toGlobalData(List<Object> objects) {
GlobalData globalData = new GlobalData();
objects // list of mix Exons and Sups objects (Exon and Sup contain more, fields incomeCode, endDate and codeRef are used only for grouping) ex: [Exon{45 2021-01-01 4}, Exon{45 2021-01-01 4}, Sup{45 2021-01-01 4}]
.forEach(o -> {
if (o instanceof Exon) {
globalData.setValue(globalData.getValue()
.add(o.getValue()))
if (o.getxxx()> 100) {
globalData.setIncome(globalData.getIncome()
.add(calcMethod(o)))
}
//more business code here
}
if (o instanceof Sup) {
globalData.setValue(globalData.getValue()
.add(o.getValue()))
if (o.getxxx()> 100) {
globalData.setIncome(globalData.getIncome()
.add(calcMethod(o)))
}
//more business code here
}
});
return globalData;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的上一个问题中,您将其作为输出a
list&lt; list&lt; object&gt;&gt;
,而现在您要将每个list&lt; object&gt;
映射到单个globalData
实例。请注意,我仅根据您的问题得到的几个细节,并且不了解您的班级之间的关系。
这也是我发布了整个解决方案以测试上面代码的链接。但是,由于托管解决方案的服务器上存在执行时间限制,因此不会显示输出,但是您可以检查链接以复制代码并在计算机上进行测试。
我还提供了一些虚拟课程来模拟示例中显示的某些方法的行为。
https://ideone.com/rn5xva
In your previous question, you're having as output a
List<List<Object>>
while now you want to map eachList<Object>
to a singleGlobalData
instance.Mind that I'm giving you this answer only basing on the few details that I've got from your question and with no knowledge of the relationships between your classes.
Here is also a link where I've posted my entire solution to test the code above. However, since there is an execution time limit on the server hosting the solution, the output won't be displayed, but you could check the link anyway to copy the code and test it on your machine.
I've also provided some dummy classes to mock the behavior of some of the methods shown in your example.
https://ideone.com/rN5XVa