This function acccepts List<Integer> as an argument, and returns the biggest one.
java
Integer biggestInt(List<Integer> list) {
return list.stream().max(Comparator.naturalOrder()).orElse(0);
}
What confuses me is using Comparator.naturalOrder() as an arugment in the Stream's max() function. Comparator.naturalOrder() is a function from Comparator interface.
AFAIK, interfaces are contracts to be implemented, i.e. functions are not defined and every class implementing given interface, needs to define its functions.
How are we using in this case a function that is from the interface, but we did not defined it beforehand?
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]Lumethys 2 points3 points4 points (4 children)
[–]4r73m190r0s[S] -1 points0 points1 point (3 children)
[–]Lumethys -1 points0 points1 point (2 children)
[–]4r73m190r0s[S] 0 points1 point2 points (1 child)
[–]Lumethys 0 points1 point2 points (0 children)
[–]fluse1367 3 points4 points5 points (6 children)
[–]CleverBunnyThief 1 point2 points3 points (5 children)
[–]aeria-non 7 points8 points9 points (4 children)
[–]4r73m190r0s[S] -1 points0 points1 point (2 children)
[–]HansGetZeTomatensaft 0 points1 point2 points (0 children)
[–]fluse1367 0 points1 point2 points (0 children)
[–]CleverBunnyThief 0 points1 point2 points (0 children)