Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java in operator

For the one millionth time, I would have liked to use an IN operator in Java, similar to the IN operator in SQL. It could just be implemented as compiler syntactic sugar. So this

if (value in (a, b, c)) {
}
else if (value in (d, e)) {
}

...would really be awesome. In fact, the above is the same as the rather verbose (and not adapted for primitives) construct here:

if (Arrays.asList(a, b, c).contains(value)) {
}
else if (Arrays.asList(d, e).contains(value)) {
}

Or like this for int, long and similar types:

switch (value) {
  case a:
  case b:
  case c:
    // ..
    break;

  case d:
  case e:
    // ..
    break;
 }

Or maybe there could be even more efficient implementations.

Question:

Is something like this going to be part of Java 8? How can I make such a suggestion, if not? Or is there any equivalent construct that I could use right now?

like image 903
Lukas Eder Avatar asked Sep 12 '11 15:09

Lukas Eder


People also ask

Is there an in operator in Java?

Java has no "in" operator. For arrays you need to iterate them and look for a matching item. For Lists you can use the contains method.

What operator is == in Java?

== operator is a type of Relational Operator in Java used to check for relations of equality. It returns a boolean result after the comparison and is extensively used in looping statements and conditional if-else statements.

Is it && or & in Java?

& is a bitwise operator and compares each operand bitwise. It is a binary AND Operator and copies a bit to the result if it exists in both operands. (A & B) will give 12 which is 0000 1100. Whereas && is a logical AND operator and operates on boolean operands.


4 Answers

You can write a helper method to do it.

public static <T> boolean isIn(T t, T... ts) {
    for(T t2: ts) 
      if (t.equals(t2)) return true;
    return false;
}

// later
if (isIn(value, a,b,c)) {

} else if (isIn(value, d,e)) {

}
like image 141
Peter Lawrey Avatar answered Oct 09 '22 11:10

Peter Lawrey


Using op4j:

Op.onListFor(a,b,c).get().contains(value);

Using the same approach, you could create a helper classes Is with a method in:

class Is<T> {
    private T value;

    public Is( T value ) { this.value = value; }

    public boolean in( T... set ) {
        for( T item : set ) {
            if( value.equals( item ) ) {
                return true;
            }
        }

        return false;
    }

    public static <T> Is<T> is( T value ) {
        return new Is<T>( value );
    }
}

with a static import, you can write:

if(is(value).in(a,b,c)) {
}
like image 28
Aaron Digulla Avatar answered Oct 09 '22 11:10

Aaron Digulla


There has been a very old proposal for collection literals.

Currently there is Sets.newHashSet in Guava which is pretty similar to Arrays.asList.

like image 26
Miserable Variable Avatar answered Oct 09 '22 10:10

Miserable Variable


You are looking for the Java Community Process

like image 26
Adam Batkin Avatar answered Oct 09 '22 10:10

Adam Batkin