Created
August 18, 2022 14:37
-
-
Save sovietspy2/e7abb501045921603441ec3524a0789e to your computer and use it in GitHub Desktop.
Java stuff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
=========== |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
J