Shorter way of checking several arguments to same value in a if-statement?

Arne O. Ose :

Is there an easy way to compare several arguments to the same value? To exemplify what I mean I have written this code which doesn't work, but illustrates what I'm looking for:

if ((arg1 && arg2 && arg3) > 0)

Instead of needing to write:

if (arg1 > 0 && arg2 > 0 && arg3 > 0)

Benefits would, in my opinion, be easier to read the code, and only needing to change one value.

Mureinik :

One neat trick is to stream all the arguments and use allMatch with the predicate you want to check:

if (IntStream.of(arg1, arg2m arg3).allMatch(x -> x > 0)) {

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91650&siteId=1