[英]How to apply reduce/collect filter in Java 8, having a separate index?
我试图通过删除已经在屏幕上显示的重复点来找出减少数据的方法。
这是我在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的一个Function来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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.