gpt4 book ai didi

java - 如何在具有单独索引的 Java 8 中应用 reduce/collect 过滤器?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:42 25 4
gpt4 key购买 nike

我正在尝试找出一种方法,通过删除已显示在屏幕上的重复点来减少我的数据。

这是我在 Java 7 中的代码

public List<Integer> getFilterIndexes(List<PlotPoint> pixels) {
List<Integer> indexResults = new ArrayList<Integer>(pixels.size());
HashSet<Integer> magList = new HashSet<Integer>(pixels.size());
int pixelStartIndex = 0;

for (int i=1; i < pixels.size(); i++) {
if (pixels.get(i).getX() - pixels.get(pixelStartIndex).getX() < widthInterval) {
int pixelRow = (int) ((pixels.get(i).getY() - minHeight / heightInterval);
if (!magList.add(pixelRow) {
continue;
}
} else {
pixelStartIndex = i;
magList.clear();
}

indexResult.add(i);
}
}

是否可以在 Java 8 中实现它?我考虑过将 pixelRow 的函数用于 mapToInt(index, pixelRow);

Function<PlotPoint, Integer> pixelRow = (n)->(int) (n.getY()-minHeight/heightInterval);

但不明白如何使用当前像素或 plotPoint 和先前保存的 plotPoint 来实现 reduce 或 collect。

有什么想法吗?

最佳答案

这是您当前的实现(有一些假设)

public class LegacyPixelsContainer {
private final int widthInterval = 10;
private final int minHeight = 10;
private final int heightInterval = 10;
private final List<PlotPoint> pixels;

public LegacyPixelsContainer(List<PlotPoint> pixels) {
this.pixels = Collections.unmodifiableList(pixels);
}

public List<Integer> getFilteredIndexes() {
List<Integer> indexResults = new ArrayList<>(pixels.size());
HashSet<Integer> magList = new HashSet<>();
int pixelStartIndex = 0;

for (int i = 1; i < pixels.size(); i++) {
if (getPixelAt(i).getX() - getPixelAt(pixelStartIndex).getX() < widthInterval) {
int pixelRow = getPixelAt(i).getY() - minHeight / heightInterval;

if (!magList.add(pixelRow)) {
continue;
}
} else {
pixelStartIndex = i;
magList.clear();
}

indexResults.add(i);
}

return indexResults;
}

private PlotPoint getPixelAt(int i) {
return pixels.get(i);
}
}

对于 Java 8 实现,我将返回类型更改为 IntStream,因此调用者也将获得流式传输的好处,而不是预加载的 List。如果需要,调用者仍然可以通过在返回的流上调用 Collectors.toList 来收集整数作为列表。

这是 Java 8 的实现

public class PixelsContainer {
private final int widthInterval = 10;
private final int minHeight = 10;
private final int heightInterval = 10;
private final List<PlotPoint> pixels;

public PixelsContainer(List<PlotPoint> pixels) {
this.pixels = Collections.unmodifiableList(pixels);
}

public IntStream getFilteredIndexes() {
Set<Integer> magList = new HashSet<>();
AtomicInteger pixelStartIndex = new AtomicInteger(0);

return IntStream.range(1, pixels.size())
.mapToObj(i -> processIndex(i, magList, pixelStartIndex))
.filter(OptionalInt::isPresent)
.mapToInt(OptionalInt::getAsInt);
}

private OptionalInt processIndex(int i, Set<Integer> magList, AtomicInteger pixelStartIndexContainer) {
int pixelStartIndex = pixelStartIndexContainer.get();

if (getPixelAt(i).getX() - getPixelAt(pixelStartIndex).getX() < widthInterval) {
int pixelRow = getPixelAt(i).getY() - minHeight / heightInterval;

if (!magList.add(pixelRow)) {
return OptionalInt.empty();
}
} else {
pixelStartIndexContainer.set(i);
magList.clear();
}

return OptionalInt.of(i);
}

private PlotPoint getPixelAt(int i) {
return pixels.get(i);
}
}

关于java - 如何在具有单独索引的 Java 8 中应用 reduce/collect 过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37150011/

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