StreamSummary implements ITopK<T> { public List<Counter<T>> topK(int k) { List<Counter<T>> topK = new ArrayList<Counter<T>>(k); for (ListNode2<Bucket> bNode = bucketList.head(); bNode != null; bNode = bNode.getPrev()) { Bucket b = bNode.getValue(); for (Counter<T> c : b.counterList) { if (topK.size() == k) { return topK; } topK.add(c); } } return topK; } StreamSummary();  StreamSummary(int capacity); int getCapacity(); @Override boolean add(T item); @Override boolean add(T element, long incrementCount); T offerReturnDropped(T item, int incrementCount); Pair<Boolean, T> offerReturnAll(T item, long incrementCount); @Override List<CountEntry<T>> peek(int k); List<Counter<T>> topK(int k); long size(); @Override String toString();  }
@Test public void testTopK() { StreamSummary<String> vs = new StreamSummary<String>(3); String[] stream = {"X", "X", "Y", "Z", "A", "B", "C", "X", "X", "A", "C", "A", "A"}; for (String i : stream) { vs.add(i); } List<Counter<String>> topK = vs.topK(3); for (Counter<String> c : topK) { assertTrue(Arrays.asList("A", "C", "X").contains(c.getItem())); } }