addressalign-toparrow-leftarrow-leftarrow-right-10x10arrow-rightbackbellblockcalendarcameraccwcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-checkcircle-with-crosscircle-with-pluscontroller-playcredit-cardcrossdots-three-verticaleditemptyheartexporteye-with-lineeyefacebookfolderfullheartglobe--smallglobegmailgooglegroupshelp-with-circleimageimagesinstagramFill 1languagelaunch-new-window--smalllight-bulblightning-boltlinklocation-pinlockm-swarmSearchmailmediummessagesminusmobilemoremuplabelShape 3 + Rectangle 1ShapeoutlookpersonJoin Group on CardStartprice-ribbonprintShapeShapeShapeShapeImported LayersImported LayersImported Layersshieldstar-shapestartickettrashtriangle-downtriangle-uptwitteruserwarningyahooyoutube

Re: [ljc] Java 8 Stream filter & Predicate negate

From: SJ
Sent on: Monday, November 16, 2015, 1:04 PM
Could you elaborate more Richard, why do you prefer the lambda approach? 

Sleiman

On Mon, Nov 16, 2015 at 12:30 PM, Mike Burton <[address removed]> wrote:
Hi Richard,

I would have agreed with you until I read Heinz Kabutz's "Lambda reduction principle "

So now I'm coming round to a preference for:
.filter(negate(String::isEmpty))

Best Regards

Mike Burton
(Sent from my iPhone)



On 16 Nov 2015, at 07:50, Richard Warburton <[address removed]> wrote:

Hi John,

I'm still getting use to Java 8, so thought I'ld ask.

Has anyone found a good/clean way using .filter and accessing the Predicate negate()

e.g.

.stream()
  .filter(MyObject::isSomething())
  ....

.stream()
  .filter(p -> !p.isSomething())
  ....

I'm aware the predicate has negate() but not sure what is the cleanest was to solve the issue. Creating my own custom predicate seams over the top just to access negate().

I know a few people have offered ways to solve this problem and that's great, but I'm inclined to say don't. The negate method on the Predicate interface is really nice if you've got a method that takes a predicate as a parameter and you want to negate it, but is it actually better than simply using the not operator.

I would pick:
.filter(str -> !str.isEmpty())

Over either of the following any day of the week:
.filter(predicate(String::isEmpty).negate())
.filter(negate(String::isEmpty))


regards,

  Richard Warburton





--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Meetup on behalf of Richard Warburton from LJC - London Java Community.
To report this message or block the sender, please click here
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]




--
Please Note: If you hit "REPLY", your message will be sent to everyone on this mailing list ([address removed])
This message was sent by Meetup on behalf of Mike Burton from LJC - London Java Community.
To report this message or block the sender, please click here
Set my mailing list to email me As they are sent | In one daily email | Don't send me mailing list messages

Meetup, POB 4668 #37895 NY NY USA 10163 | [address removed]

People in this
group are also in: