Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 8 - Can I use lambda to make sure field is not in list?

How can I convert the following pseudo code into a Java 8 lambda?:

...
if(fee.getTaxID() not in (126,127,128))
{
...

I'm hoping that by leveraging lambdas I can avoid the following code:

...
if(fee.getTaxID() != 126 && fee.getTaxID() != 127 && fee.getTaxID() != 128))
{
...

The goal is that I can replace the 126,127,128 in the pseudo code with a List<Integer>

Thanks

like image 661
Roberto Murphy Avatar asked Dec 01 '25 18:12

Roberto Murphy


1 Answers

If you really want to do it using lambda, you can do this:

if(IntStream.of(126,127,128).noneMatch(t -> t == fee.getTaxID())) {

}

It does not make the code shorter though.

like image 181
Mrinal Avatar answered Dec 05 '25 09:12

Mrinal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!