@Override public void merge() { while (_bins.size() > getMaxBins()) { Gap<T> smallestGap = _gaps.pollFirst(); Bin<T> newBin = smallestGap.getStartBin().combine(smallestGap.getEndBin()); Gap<T> followingGap = _binsToGaps.get(smallestGap.getEndBin().getMean()); if (followingGap != null) { _gaps.remove(followingGap); } _bins.remove(smallestGap.getStartBin().getMean()); _bins.remove(smallestGap.getEndBin().getMean()); _binsToGaps.remove(smallestGap.getStartBin().getMean()); _binsToGaps.remove(smallestGap.getEndBin().getMean()); updateGaps(newBin); _bins.put(newBin.getMean(), newBin); } }