Skip to content

Instantly share code, notes, and snippets.

@hankliu5
Last active February 14, 2016 05:28
Show Gist options
  • Save hankliu5/87ad63e914363c50510f to your computer and use it in GitHub Desktop.
Save hankliu5/87ad63e914363c50510f to your computer and use it in GitHub Desktop.
// puts wordsMap's key and value into ArrayList.
List<Map.Entry<String, Integer>> listData =
new ArrayList<Map.Entry<String, Integer>>(wordsMap.entrySet());
/**
* sorts the data based on the order of values by using Comparator class.
*/
Collections.sort(listData, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> entry1,
Map.Entry<String, Integer> entry2) {
return (entry2.getValue() - entry1.getValue());
}
}
);
// Subtracts the amounts of dataList to the amount we want to see.
listData = listData.subList(0, wordsToPrint);
// Prints out all keys and values in listData by using lambda expression.
for (Map.Entry<String, Integer> entry : listData) {
printWords(entry.getKey());
}
commonInBS.close();
fileInBS.close();
}
/**
* prints out the key and value in normalized format.
* @param {String} word - the key from the sorted listData.
*/
private static void printWords(String word) {
System.out.println(word + " : " + wordsMap.get(word));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment