Monday, 30 May 2016

Does Java have a "IN" operator or function like SQL?




I want to know if there's a way of doing something like this in Java :



if(word in stringArray) {
...
}


I know I can make a function for this but I just want to know if Java has already something for this.



Thank you!


Answer



There are many collections that will let you do something similar to that. For example:



With Strings:



String s = "I can has cheezeburger?";
boolean hasCheese = s.contains("cheeze");


or with Collections:



List listOfStrings = new ArrayList();
boolean hasString = listOfStrings.contains(something);


However, there is no similar construct for a simple String[].


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...