[英]How to apply reduce/collect filter in Java 8, having a separate index?
I'm trying to figure out a way to reduce my data by removing duplicate points that have already been display on the screen. 我试图通过删除已经在屏幕上显示的重复点来找出减少数据的方法。
Here's my code in Java 7 这是我在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);
}
}
Is it possible to implement this in Java 8? 是否可以在Java 8中实现它? I've thought about using a Function for the pixelRow to mapToInt(index, pixelRow); 我已经考虑过使用pixelRow的一个Function来mapToInt(index,pixelRow);
Function<PlotPoint, Integer> pixelRow = (n)->(int) (n.getY()-minHeight/heightInterval);
but didn't understand how to implement a reduce or collect using the current pixel or plotPoint and the previously saved plotPoint. 但不明白如何使用当前像素或plotPoint以及之前保存的plotPoint实现reduce或collect。
Any ideas? 有任何想法吗?
Here is your current implementation (with some assumptions) 这是您当前的实现(有一些假设)
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);
}
}
For Java 8 implementation, I changed the returned type to IntStream, so the caller will also get the benefit of streaming instead of pre-loaded List . 对于Java 8实现,我将返回的类型更改为IntStream,因此调用者也将获得流而不是预加载List的好处。 The caller can still collect the integers as list, if needed, by calling Collectors.toList on the returned stream. 如果需要,调用者仍然可以通过在返回的流上调用Collectors.toList来将整数收集为列表。
Here is the Java 8 implementation 这是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.