如何在Java8中应用reduce/collect过滤器,使用单独的索引?

如何在Java8中应用reduce/collect过滤器,使用单独的索引?,java,functional-programming,java-8,Java,Functional Programming,Java 8,我试图找出一种方法,通过删除屏幕上已经显示的重复点来减少我的数据 以下是我在Java7中的代码 public List<Integer> getFilterIndexes(List<PlotPoint> pixels) { List<Integer> indexResults = new ArrayList<Integer>(pixels.size()); HashSet<Integer> magList = new HashS

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

以下是我在Java7中的代码

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);
   }
 }
公共列表GetFilterIndex(列表像素){
List indexResults=newarraylist(pixels.size());
HashSet magList=新的HashSet(pixels.size());
int pixelStartIndex=0;
对于(int i=1;i
有可能在Java8中实现这一点吗? 我考虑过使用一个函数将pixelRow映射到mapToInt(索引,pixelRow)

函数pixelRow=(n)->(int)(n.getY()-minHeight/heightInterval);
但不了解如何使用当前像素或绘图点以及以前保存的绘图点实现减少或收集


有什么想法吗?

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

公共类LegacyPixelsContainer{
私有最终整数宽度间隔=10;
私人最终整数最小高度=10;
专用最终内部高度间隔=10;
私有最终列表像素;
公共LegacyPixelsContainer(列出像素){
this.pixels=Collections.unmodifiableList(像素);
}
公共列表getFilteredIndex(){
List indexResults=newarraylist(pixels.size());
HashSet magList=新HashSet();
int pixelStartIndex=0;
对于(int i=1;i
对于Java 8实现,我将返回的类型更改为IntStream,因此调用者也将获得流而不是预加载列表的好处。如果需要,调用者仍然可以通过调用返回流上的Collectors.toList来收集整数作为列表

下面是Java8实现

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);
    }
}
公共类像素容器{
私有最终整数宽度间隔=10;
私人最终整数最小高度=10;
专用最终内部高度间隔=10;
私有最终列表像素;
公共像素容器(列出像素){
this.pixels=Collections.unmodifiableList(像素);
}
公共IntStream GetFilteredIndex(){
Set magList=新HashSet();
AtomicInteger pixelStartIndex=新的AtomicInteger(0);
返回IntStream.range(1,pixels.size())
.mapToObj(i->processIndex(i,magList,pixelStartIndex))
.filter(Optionant::iPresent)
.mapToInt(optionant::getAsInt);
}
私有Optionant processIndex(int i、Set magList、AtomicInteger pixelStartIndexContainer){
int pixelStartIndex=pixelStartIndexContainer.get();
if(getPixelAt(i).getX()-getPixelAt(pixelStartIndex).getX()
您可能可以将其破解到流中,但它的可读性将大大低于您已有的代码。请继续使用您当前的代码。最大的障碍不是索引,而是对遭遇顺序的依赖性。
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);
    }
}
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);
    }
}