2016-05-10 15 views
3

我想通過刪除已經顯示在屏幕上的重複點來減少我的數據。如何在Java 8中應用reduce/collect過濾器,有單獨的索引?

這是我在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收集。

任何想法?

+0

您_might_能夠攻入流這一點,但它的將是天文數字比你已經得到的可讀性。堅持你現在的代碼。 –

+1

最大的障礙不在於索引,而在於相遇順序的依賴。 – Holger

回答

3

這裏是你的當前實現(有一些假設)

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,所以調用者也將獲得流媒體的利益,而不是預先加載列表。如果需要,調用者仍然可以通過在返回的流上調用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); 
    } 
} 
相關問題