java - Running a lambda in a loop
问题描述
I am creating an event by providing a Instant
of time.
serviceContainer.consumeEvent(event ->
event.setPayload(new TriggerCalculation(Instant.ofEpochMilli(serviceContainer.currentTimeMillis())
)));
However, I would like to add a fixed interval of time to the Instant
and create a new event with the new Instant
.
serviceContainer.consumeEvent(event ->
event.setPayload(new TriggerCalculation(Instant.ofEpochMilli(serviceContainer.currentTimeMillis()).plus(1, ChronoUnit.DAYS)
)));
However, I cannot do this in a for loop since index i
needs to be final:
for(int i=1; i<7; i++){
serviceContainer.consumeEvent(event ->
event.setPayload(new TriggerCalculation(Instant.ofEpochMilli(serviceContainer.currentTimeMillis()).plus(i, ChronoUnit.DAYS)
)));
}
How can loop a lambda in java if I want to increment a value within the lambda?
解决方案
你可以用IntStream.range
这个
IntStream.range(0, 7)
.forEach(i -> serviceContainer.consumeEvent(event ->
event.setPayload(new TriggerCalculation(Instant.ofEpochMilli(serviceContainer.currentTimeMillis()).plus(i, ChronoUnit.DAYS)
))));
推荐阅读
- python - 将 ARIMAX 预测分解为 exog 变量和 ARIMA 参数的贡献
- android - 如何检测 Jetpack Compose 中变换手势的结束?
- node.js - Node Express - PayloadTooLargeError:请求实体太大
- java - 为什么记录视图和记录按钮会抛出异常?(不是任何其他问题的重复,也没有找到解决方案)
- python-3.x - 将数据框的一列压缩到基于 Python3 中具有相同值的另一列的列表
- python - 关于win10toast和win10toast-click,通知中心有问题
- pine-script - 如何将 TradingView 上的复杂指标转换为 MT4 的语言?
- .net - System.Text.Json 如何反序列化
- python - Dask:真正懒惰的排序
- javascript - React useQuery 钩子一直在组件内运行