33

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?

Community
  • 1
  • 1
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509

5 Answers5

21

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)) {

}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
14

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)) {
}
Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Awesome! I was just going to ask if there is a library for precisely this kind of DSL!! Thanks a lot – Lukas Eder Nov 08 '11 at 14:02
  • Funny, how Op4j is hardly used, though. When you compare the downloads with those of [Lambda-J](http://code.google.com/p/lambdaj), for instance – Lukas Eder Nov 08 '11 at 14:41
  • Yeah, it's one of these gems that sit in a browser tab, begging to be used but somehow, there never is the time to start with it... – Aaron Digulla Nov 08 '11 at 15:43
  • I've added a feature request to add your suggested `Is` class to the library. `Are` might be a good class as well for `if (are(val1, val2).allIn(a, b, c))`: https://sourceforge.net/apps/phpbb/op4j/viewtopic.php?f=2&t=15 – Lukas Eder Nov 14 '11 at 11:08
7

There has been a very old proposal for collection literals.

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

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • 1
    I was sure that I had seen something like this before. So it's not part of project coin, neither for Java 8... But with this, I could write `[a, b, c].contains(value)`. That would be acceptable. So the Guava solution wouldn't be less verbose. I think the overhead of a `HashSet` for a constant, low number of elements is not worth the trouble, compared to `Arrays.asList()`... – Lukas Eder Sep 12 '11 at 16:05
  • 1
    The performance should be similar for both collection types for small sizes. The problem is that it is created every time it is used, though hotspot will probably optimize it. With collection literals containing literals that becomes trivial. – Miserable Variable Sep 12 '11 at 16:23
6

You are looking for the Java Community Process

Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
  • 1
    Thanks, I know JCP. I also know the [coin-dev](http://mail.openjdk.java.net/mailman/listinfo/coin-dev) mailing list. How can I increase chances of being heard? – Lukas Eder Sep 12 '11 at 15:55
1

I doubt something like an IN operator would be made available, as there are already multiple ways of doing this(like using switch) as you yourself pointed out.

And I think requirement list for project-coin and J8 is already fully loaded to be anything like this to be considered.

Suraj Chandran
  • 24,433
  • 12
  • 63
  • 94
  • 1
    That's what I feared (J8 being already fully loaded). But currently, there is nothing as neat as a simple `IN` operator to actually do what an `IN` operator does. The switch is horribly verbose. `Arrays.asList(...)` contains might be an option – Lukas Eder Sep 12 '11 at 16:02
  • you're right. Probably, my suggestion would go too far. I'd prefer Hermal's reference to Joshua Bloch's proposition of having collection literals... – Lukas Eder Sep 12 '11 at 16:08
  • 2
    @Suraj Chandran: I agree: a language should not support too many programming idioms, even it that makes the code shorter, it makes the language and the compiler more complex. – Giorgio Sep 12 '11 at 18:18