Java 8 | Fetch top N unique numbers from list
Below Java 8 example shows how to fetch top N unique numbers from ArrayList.
Traditional looping will require more logic to first remove duplicates, sort numbers and then store N numbers to different list.
Java 8 provides stream API to do all the above steps in single line of code.
Descending order (N = 3)
Traditional looping will require more logic to first remove duplicates, sort numbers and then store N numbers to different list.
Java 8 provides stream API to do all the above steps in single line of code.
Descending order (N = 3)
Ascending order (N = 3)
Comments
Post a Comment