gpt4 book ai didi

java - 在 Java 流中插入值

转载 作者:行者123 更新时间:2023-11-30 02:34:28 24 4
gpt4 key购买 nike

我有一个包含大约 200,000 个刻度值(日期和值)的列表。该列表为每个日期提供刻度值。但这就是我的输出中需要的。所以我需要一些东西来在间隙中添加缺失的刻度值。

我正在使用 Java Stream API 来处理该列表。

class Tick {
public LocalDate date;
public BigDecimal value;

public Tick(LocalDate date, BigDecimal value) {
this.date = date;
this.value = value;
}
}

public class DateInterpolator {

public static void main(String [] args)
{
final ArrayList<Tick> list = new ArrayList<>();
list.add(new Tick(LocalDate.of(2017, 1, 1), BigDecimal.valueOf(7)));
list.add(new Tick(LocalDate.of(2017, 1, 3), BigDecimal.valueOf(8)));

final Stream<Tick> stream = list.stream();

// TODO no value for 2017-01-02 given.
// I want to use the value from the day before (7).
// Add black magic here.
}

}

是否可以向流中添加值,或者我可以获得不包含间隙的结果流吗?我可以对流进行前瞻(以检测间隙)吗?

最佳答案

您可以通过跟踪先前的值来检查是否存在间隙。一种方法是根据条目的索引直接访问列表。以下解决方案期望输入按日期排序。

final Stream<Tick> stream = IntStream.range(0, list.size())
.mapToObj((i) -> {
// 'sub' should contain one item. If there are gaps,
// sub will contain gap values as well, up to and including the current Tick.
final ArrayList<Tick> sub = new ArrayList<>();

Tick curr = list.get(i);

if(i > 0) {
Tick prev = list.get(i-1);

// Fill the gaps if there are any
for (LocalDate date = prev.date.plusDays(1); date.isBefore(curr.date); date = date.plusDays(1)) {
sub.add(new Tick(date, prev.value));
}
}

sub.add(curr); // add the current value

return sub;
})
.flatMap(List::stream);

或者基于流的实现:

private static Stream<Tick> fillGaps(Stream<Tick> ticks) {

final Var prev = new Var(); // required to be final, so a wrapper is needed to modify the instance

Stream<Tick> result = ticks
.map(curr -> {
final ArrayList<Tick> sub = new ArrayList<>();

if(prev.instance != null) {
for (LocalDate date = prev.instance.date.plusDays(1); date.isBefore(curr.date); date = date.plusDays(1)) {
sub.add(new Tick(date, prev.instance.value));
}
}

sub.add(curr);
prev.instance = curr;

return sub;
})
.flatMap( l -> l.stream());

return result;
}

// Helper class
class Var {
public Tick instance;
}

// Usage:
final Stream<Tick> ticks = fillGaps(stream);

flatMap 展平中间结果(由 mapToObject/map 创建):

[
Tick(2017-1-1, 7) // Last item is always an item in the original list
],
[
Tick(2017-1-2, 7), // Interpolated value based on the previous value
Tick(2017-1-3, 8) // Last item is always an item in the original list
]

如果有任何错误,请随时纠正我。

关于java - 在 Java 流中插入值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43468669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com