Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Processing Large Files in Java, Variation 6
--- ReadFileJavaApplicationBufferedReader5.java 2019-01-05 20:40:11.000000000 -0800
+++ ReadFileJavaApplicationBufferedReader6.java 2019-01-05 20:40:15.000000000 -0800
@@ -115,16 +115,9 @@
}
}
- LinkedList<Entry<String, Integer>> list = new LinkedList<>(map.entrySet());
+ Entry<String, Integer> common = Collections.max(map.entrySet(), Entry.comparingByValue());
- Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
- public int compare(Map.Entry<String, Integer> o1,
- Map.Entry<String, Integer> o2)
- {
- return (o2.getValue()).compareTo(o1.getValue());
- }
- });
- System.out.println("The most common first name is: " + list.get(0).getKey() + " and it occurs: " + list.get(0).getValue() + " times.");
+ System.out.println("The most common first name is: " + common.getKey() + " and it occurs: " + common.getValue() + " times.");
Instant commonNameEnd = Instant.now();
long timeElapsedCommonName = Duration.between(commonNameStart, commonNameEnd).toMillis();
System.out.println("Most common name time: " + timeElapsedCommonName + "ms");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.