Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

function

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!

like image 966
codea Avatar asked Aug 25 '10 12:08

codea


People also ask

What is in operator in SQL?

The SQL IN Operator The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

What is like operator in Java?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters.


2 Answers

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<String> listOfStrings = new ArrayList<String>(); boolean hasString = listOfStrings.contains(something); 

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

like image 166
jjnguy Avatar answered Oct 17 '22 08:10

jjnguy


In SQL

x in ('Alice', 'Bob', 'Carol') 

In Java:

Arrays.asList("Alice", "Bob", "Carol").contains(x) 
like image 42
asmund.skalevik Avatar answered Oct 17 '22 08:10

asmund.skalevik