north.mister
north.mister

Reputation: 510

How to combine multiple boolean comparisons into one

I'm trying to find a short way to write this (in Java) (note in this case top is a Node in a linkedlist)

if(top.data.equals("A") || top.data.equals("B") || top.data.equals("C") ||....)
    postfix.push(pop());

My goal is something like

if(top.data.equals("A", "B", "C", "D", ....)

Is there a way to do this in Java? (I don't even know what to call it to do further research in the API and elsewhere).

Thanks for any help.

Upvotes: 1

Views: 1452

Answers (2)

Josh M
Josh M

Reputation: 11947

if("ABC".indexOf(top.data.toString()) > -1){ .... }

Or

if("ABC".contains(top.data.toString())) { .... }

Upvotes: 0

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79847

if (Arrays.asList("A","B","C").contains(top.data))

Upvotes: 6

Related Questions