I have a list of players and want to get the player with the min rating. I could do:
Player player1 = Collections.min(players,Comparator.comparing(Player::getRating));
Or I could do:
Player player2 = players.stream().min(Comparator.comparing(Player::getRating)).get();
What's better in terms of performance or anything else?
It should not make a great difference.
We could argue that the stream way could take advantage of the parallel stream option while Collections.min() could not.
And in both cases you present, the boxing may have a cost with a big list. So you should favor comparingInt()/comparingDouble()/comparingLong() to comparing().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With