Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java8 .getMethod() vs ::getMethod

I am new in Java8 ,and I created this piece of code that is working fine

 userService.getClient().findUsersByMarkets(marketIds)
                .stream()
                .filter(us -> !alreadyNotifiedUserIds.contains(us.getId()))
                .forEach(usersToBeNotified::add);

but under my understandings this piece of code should also work fine, but is not the case and I am wondering why

     userService.getClient().findUsersByMarkets(marketIds)
        .stream()
        .filter(us -> !alreadyNotifiedUserIds.contains(User::getId))
        .forEach(usersToBeNotified::add);
like image 388
carles xuriguera Avatar asked Nov 30 '18 15:11

carles xuriguera


1 Answers

User::getId is a reference to a function so it's not equivalent to contains(us.getId()).

see --> https://www.codementor.io/eh3rrera/using-java-8-method-reference-du10866vx to familiarise your self with method references.

like image 55
Ousmane D. Avatar answered Nov 01 '22 02:11

Ousmane D.