# Top K Frequent Words II

`Data Stream`, `HashMap`, `TreeSet`

### Description

Find top_k _frequent words in realtime data stream.

Implement three methods for Topk Class:

1. `TopK(k)`. The constructor.
2. `add(word)`. Add a new word.
3. `topk()`. Get the current top _k _frequent words.

If two words have the same frequency, rank them by alphabet.

### Example

``````TopK(2)
topk()
>> ["code", "lint"]
``````

## Solution

#### Jiuzhang's Solution: HashMap + TreeSet

2. 这里的TreeSet用的是根据words count来排序，并且count越高排序越靠前，因此可以想成（用PriorityQueue）的Max-Heap，与PriorityQueue相对应的，这里TreeSet因为可以直接用`pollLast()`删去last(highest) element，也就是移除最小的word count，因此这里可以使用Max-Heap的思想而不是Min-Heap。
``````/**
* This reference program is provided by @jiuzhang.com
*/

import java.util.NavigableSet;

public class TopK {
private Map<String, Integer> words = null;
private NavigableSet<String> topk = null;
private int k;

private Comparator<String> myComparator = new Comparator<String>() {
public int compare(String left, String right) {
if (left.equals(right))
return 0;

int left_count = words.get(left);
int right_count = words.get(right);
if (left_count != right_count) {
return right_count - left_count;
}
return left.compareTo(right);
}
};

public TopK(int k) {
// initialize your data structure here
this.k = k;
words = new HashMap<String, Integer>();
topk = new TreeSet<String>(myComparator);
}

if (words.containsKey(word)) {
if (topk.contains(word))
topk.remove(word);
words.put(word, words.get(word) + 1);
} else {
words.put(word, 1);
}

if (topk.size() > k) {
topk.pollLast();
}
}

public List<String> topk() {
List<String> results = new ArrayList<String>();
Iterator it = topk.iterator();
while(it.hasNext()) {
String str = (String)it.next();