@Override public void accept(String key, float[] value) { if (allowedPredicate.test(key)) { double score = rescoreFn.applyAsDouble(key, scoreFn.applyAsDouble(value)); // Only proceed if score can possibly exceed (cached) minimum score in the queue. if (score > topScoreLowerBound) { // If full, if (topN.size() >= howMany) { // Must double-check against next value because new one may still not be bigger if (score > (topScoreLowerBound = topN.peek().getSecond())) { // Swap in new, larger value for old smallest one topN.poll(); topN.add(new Pair<>(key, score)); } } else { topN.add(new Pair<>(key, score)); } } } }