Skip to content

Instantly share code, notes, and snippets.

@sovietspy2
Created August 18, 2022 14:37
Show Gist options
  • Save sovietspy2/e7abb501045921603441ec3524a0789e to your computer and use it in GitHub Desktop.
Save sovietspy2/e7abb501045921603441ec3524a0789e to your computer and use it in GitHub Desktop.
Java stuff
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for (Integer i : arr) {
// MAP COMPUTE EXAMPLE
map.compute(i, (key,val) -> {
if (val==null) {
return 0;
} else {
return val+1;
}
});
}
// GET ENTRY FROM MAP
Map.Entry<Integer, Integer> e = map.entrySet()
.stream()
.max(Map.Entry.comparingByValue())
.get();
Integer maxCount = e.getValue();
==== sort map get single value // Comparator.reverseOrder()
return map.entrySet()
.stream()
.filter(i-> maxCount.equals(i.getValue()))
.map( i-> i.getKey())
.sorted(Comparator.naturalOrder())
.findFirst()
.get();
===========
@kdwayne22
Copy link

J

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment