• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Predicate "and" default method

 
Ranch Hand
Posts: 182
1
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello everyone,

I am currently studying the SYBEX OCP book by Selikoff and Boyarsky.  On page 178, the book has an explanation of how to use the default method and from the Predicate Functional Interface:

Suppose that we have these two Predicates:



Now we want a Predicate for brown eggs and another for all other colors of eggs. We
could write this by hand:



This works, but it’s not great. It’s a bit long to read, and it contains duplication. What if
we decide the letter e should be capitalized in eggs? We’d have to change it in three variables:
egg, brownEggs, and otherEggs.

A better way to deal with this situation is to use two of the default methods on
Predicate:



Neat! Now we are reusing the logic in the original Predicates to build two new ones.




I cannot seem to understand the use of the and method here. Can anyone explain???
Thanks !!
Ioanna
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you read the documentation for Predicate#and()? What it does is create another Predicate object which represents
egg && brown.
It would be possible to create separate Predicates for egg and Egg with the Predicate#or() method, too. Try the two operations separately before you try anything like this:-Duplicating discussion in our Java8 and OCP fora.
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Beware: In classical logic you would write that as
(egg ∨ eggCap) ∧ brown
because the precedence of ∧ is higher than ∨. In the case of method calls, the dot operator has the same precedence as itelf throughout, but associates to the left, so the expresssion I showed you would be equivalent toThat happens to give the same as the classical logic expression, with or without () in the Java®, but if you have something like
egg ∨ eggCap ∧ brown
that is a different expression, possibly incorrect, which you would write asorin order to maintain the precedences. That is why I said to try simpler expressions first.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic