Skip to content

Instantly share code, notes, and snippets.

@dimo414
Created November 22, 2012 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dimo414/4132270 to your computer and use it in GitHub Desktop.
Save dimo414/4132270 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
import com.google.common.collect.*;
public class WordFreq {
public static void main(String[] args) throws FileNotFoundException {
final HashMultiset<String> counts = HashMultiset.create(
ImmutableList.copyOf(new Scanner(new File(args[0]))));
List<String> words = Lists.newArrayList(counts.elementSet());
Collections.sort(words, new Comparator<String>() {
public int compare(String l, String r) {
return counts.count(r) - counts.count(l);
}});
for(String word : words.subList(0, Integer.parseInt(args[1]))){
System.out.println(word + " : " + counts.count(word));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment