Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

BiPredicate List::contains  RSS feed

 
Levent Erguder
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys , i am Levent ,
I am preparing OCP8 for upgrade my OCP6 certificate.

As you know ;



how can be this code valid ?
0)contains method is not static.
boolean contains(Object o)

1) and method should be like ;
boolean contains(Object  , Object)
boolean contains(List , Integer)



A Comprehensive OCPJP 8 Certication Guide



Thank you.
 
Roel De Nijs
Sheriff
Posts: 11338
177
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before Java 8 you would create this anonymous class to do exactly the same

And to fix your compiler error, you need something like

So the method signature of your static containsTwo method is actually containsTwo(List<Integer> list, Integer num) (if you try to switch these parameter types, you'll get a compiler error).
 
Piet Souris
Rancher
Posts: 1984
67
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What helped me when it comes to understanding method references was to write out lambda's with explicit parameters. Once I got the hang of that, it turned out to be relatively easy to start understanding and using method references. For instance, have a look at a simplified version of your class. At first I use lambda's with explicit parameters, that gives me a clue how many parameters are involved. Then it is easy to write equivalent versions that use method references. See if this makes sense:
 
Pete Letkeman
Ranch Foreman
Posts: 683
19
Android Chrome IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piet Souris wrote:What helped me when it comes to understanding method references was to write out lambda's with explicit parameters.

Good advice for those of us, like me, who need to know about lambdas and the Predicate interface for the OCA 1Z0-808 exam.

Thanks.
 
Levent Erguder
Greenhorn
Posts: 10
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for replies
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!