0

I have been wondering for sometime why java doesn't have any keyword like "IN" that is used in SQL/PL-SQL. It's quite a useful keyword that helps to compare value to several other values at once instead of writing ugly code with || or lengthy if-else! Any propositions of this kind in JSRs?

PS: I am not looking for a workaround, but I wanted to know if there was any particular reason that this functionality doesn't exist in java or any proposals in near future

Joshua
  • 40,822
  • 8
  • 72
  • 132
Ozyman
  • 503
  • 1
  • 6
  • 16
  • 3
    There's `Collection.contains()`. Doesn't that do what you want? Please post some example (pseudo-) code of what you'd like to see. – Joachim Sauer Oct 29 '12 at 16:13
  • 1
    I think he means something like `SELECT * FROM table WHERE value IN ('Foo','Bar');` - the key point being that you can concisely specify the list to compare against... – DNA Oct 29 '12 at 16:16
  • This seems relevant: http://stackoverflow.com/q/1217228/938695 – Austin Henley Oct 29 '12 at 16:22
  • This is Java, not Groovy :D, in groovy there is a operator "in" :D – Grim Oct 29 '12 at 16:27

7 Answers7

1

I haven't seen any firm proposals to add this to Java. There is a blog post (and another) discussing the possibility adding of list literals, which would get you most of the way there using contains() - this seems to have been discussed for Java 7 but not adopted.

Update: Looks like list literals have been deferred until Java 8 - see this article, for example.

Rather than using contains() and direct Array literals or Lists, you could define a helper using varargs:

boolean in(Object o, Object... vals)
{
    return Arrays.asList(vals).contains(o);
}

So that you could do:

if (in(x, "a", "b", "c"))
{
    // Do something
}

But that's still a bit ugly and verbose compared to the SQL IN or the Python:

if x in ['a','b','c']:
    print "yes"
DNA
  • 42,007
  • 12
  • 107
  • 146
  • This is the workaround, the best one at that. I necessitates creating an Arraylist everytime I need a comparision, which doesn't give the same ease as a native keyword! – Ozyman Oct 29 '12 at 17:17
  • Yes; the problem is not so much the lack of the `in` keyword, but the fact that array literals are verbose, and there are no List literals in Java. – DNA Oct 29 '12 at 17:21
0

java.util.Collection has the methods contains and containsAll which might get the job done, but it's hard to tell from your question.

Gustav Barkefors
  • 5,016
  • 26
  • 30
0

But Java can easily be connected to a database(JDBC). You do have a for-each construct though that is helpful and similar to IN:

for (Suit suit : suits)
    for (Rank rank : ranks)
        sortedDeck.add(new Card(suit, rank));
Caffeinated
  • 11,982
  • 40
  • 122
  • 216
0

In Java, you can create a collection and use the method boolean contains(Object o).

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
0

In collections

Collection.contains(Object obj)

and In String you have String.contains(CharSequence c)

PermGenError
  • 45,977
  • 8
  • 87
  • 106
0

I think so,

that realy is a missing Feature.

For example:

String[] list = new String[]{"Tim","Tammy","Tod"};
if("Ted" in list){
    System.out.println("Found!");
}

would be realy easy to understand.

Grim
  • 1,938
  • 10
  • 56
  • 123
  • i guess most of the time its not clear that the operator .equals() or == or .compareTo() should be used to compare "Tim" and "Ted" for example. – Grim Oct 29 '12 at 16:24
  • Well, if it is an intelligent comparator, it will do string comparision for string values and non-string comparision for non-string values! – Ozyman Oct 29 '12 at 17:12
  • Case sensitive? Charset sensitive? Trimed? Regular Expressions? – Grim Oct 29 '12 at 17:15
0

I would do something like,

List<String> values = ArrayLists.create("AVALUE", "BVALUE");

if (values.contains(yourValue))
// Do whatever
The Cat
  • 2,375
  • 6
  • 25
  • 37
  • This is kind of a workaround, isn't it. Creating an arraylist everytime I need a comparision looks like a compromise! – Ozyman Oct 29 '12 at 17:13