public static <T1 extends Comparable<? super T1>, T2 extends Comparable<? super T2>> Pair<T1, T2> of(T1 first, T2 second) { return new ComparablePair<>(first, second); }
@Override public synchronized void add(Integer key, long count) { Preconditions.checkNotNull(key, "key can't be null"); Preconditions.checkElementIndex(key, counts.length, "key"); Preconditions.checkArgument(count >= 0, "count to add must be non-negative, got %s", count); if (count == 0) { return; } long currentCount = counts[key]; counts[key] += count; if (isInTop[key]) { topPairs.remove(new ComparablePair<Long, Integer>(currentCount, key)); topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); } else if (topPairs.size() < k) { topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); isInTop[key] = true; smallestTopCount = Math.min(smallestTopCount, counts[key]); } else if (counts[key] > smallestTopCount) { ComparablePair<Long, Integer> smallestTopPair = topPairs.pollFirst(); isInTop[smallestTopPair.getSecond()] = false; topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); isInTop[key] = true; smallestTopCount = topPairs.first().getFirst(); } }
@Override public synchronized void add(T key, long count) { Preconditions.checkNotNull(key, "key can't be null"); Preconditions.checkArgument(count >= 0, "count to add must be non-negative, got %s", count); if (count == 0) { return; } Long currentCount = counts.get(key); if (currentCount == null) { currentCount = 0L; } long updatedCount = currentCount + count; counts.put(key, updatedCount); if (topKeys.contains(key)) { topPairs.remove(new ComparablePair<Long, T>(currentCount, key)); topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); } else if (topPairs.size() < k) { topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); topKeys.add(key); smallestTopCount = Math.min(smallestTopCount, updatedCount); } else if (updatedCount > smallestTopCount) { ComparablePair<Long, T> smallestTopPair = topPairs.pollFirst(); topKeys.remove(smallestTopPair.getSecond()); topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); topKeys.add(key); smallestTopCount = topPairs.first().getFirst(); } }
@Override public synchronized void add(T key, long count) { Preconditions.checkNotNull(key, "key can't be null"); Preconditions.checkArgument(count >= 0, "count to add must be non-negative, got %s", count); if (count == 0) { return; } Long currentCount = counts.get(key); if (currentCount == null) { currentCount = 0L; } long updatedCount = currentCount + count; counts.put(key, updatedCount); if (topKeys.contains(key)) { topPairs.remove(new ComparablePair<Long, T>(currentCount, key)); topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); } else if (topPairs.size() < k) { topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); topKeys.add(key); smallestTopCount = Math.min(smallestTopCount, updatedCount); } else if (updatedCount > smallestTopCount) { ComparablePair<Long, T> smallestTopPair = topPairs.pollFirst(); topKeys.remove(smallestTopPair.getSecond()); topPairs.add(new ComparablePair<Long, T>(updatedCount, key)); topKeys.add(key); smallestTopCount = topPairs.first().getFirst(); } }
@Override public synchronized void add(Integer key, long count) { Preconditions.checkNotNull(key, "key can't be null"); Preconditions.checkElementIndex(key, counts.length, "key"); Preconditions.checkArgument(count >= 0, "count to add must be non-negative, got %s", count); if (count == 0) { return; } long currentCount = counts[key]; counts[key] += count; if (isInTop[key]) { topPairs.remove(new ComparablePair<Long, Integer>(currentCount, key)); topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); } else if (topPairs.size() < k) { topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); isInTop[key] = true; smallestTopCount = Math.min(smallestTopCount, counts[key]); } else if (counts[key] > smallestTopCount) { ComparablePair<Long, Integer> smallestTopPair = topPairs.pollFirst(); isInTop[smallestTopPair.getSecond()] = false; topPairs.add(new ComparablePair<Long, Integer>(counts[key], key)); isInTop[key] = true; smallestTopCount = topPairs.first().getFirst(); } }