Does Java have a IN operator or function like SQL duplicate

0 votes

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!

Feb 2 in Java by Neha
• 8,560 points
159 views

1 answer to this question.

0 votes

The Java language is designed to be powerful but also simple. There is no such operator in Java at the language level, but certainly libraries have been written to facilitate such queries.

If you want to know if some object is a member of some set of objects, then instead of an array, you should use -- what else? -- a Set. These data structures naturally allows such queries, and better yet, they're optimized for such queries.

You can easily check if a given string is in a Set<String> like this:

    String[] arr = { "Alice", "Bob", "Carol" };
    Set<String> names = new HashSet<String>(Arrays.asList(arr));

    System.out.println(names.contains("Alice")); // true
    System.out.println(names.contains("Dean")); // false

Using a HashSet, contains is a constant-time operation. This is much better than a linear search through an array.

You should familiarize yourself with what data structures are made available for you by the Java Collections Framework. They allow you to write codes that are idiomatic, maintainable, flexible, and supported by many of the powerful algorithms available for these data structures.

In SQL

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

In Java:

Arrays.asList("Alice", "Bob", "Carol").contains(x)
answered Feb 2 by Vaani
• 6,820 points

Related Questions In Java

0 votes
0 answers

I need to Post api to database using java in intellij. After testing sample api through postman i got an Sql exception saying Syntax error at or near ',' Please tell me where i made a mistake

public Result setModelMasterParams(){         try{             long tenantId = request().getHeader("tenant_id")==null                     || request().getHeader("tenant_id").equalsIgnoreCase("")                     || ...READ MORE

Aug 19, 2020 in Java by Sriram
• 120 points

recategorized Aug 19, 2020 by Niroj 640 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,900 points
2,204 views
0 votes
2 answers

How to find out a single character appears in String or not in Java?

You can use string.indexOf('s'). If the 's' is present in string, ...READ MORE

answered Aug 7, 2018 in Java by Sushmita
• 6,900 points
2,726 views
0 votes
1 answer

What is ?: this operator called in java ? how dose it function?

? :  is the Conditional operator and is ...READ MORE

answered Jun 11, 2018 in Java by sophia
• 1,400 points
527 views
0 votes
2 answers

How to move or copy a file in Java

The new JAVA Specification Request 203 -NIO will ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,170 points
256 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 60,653 views
0 votes
2 answers

How can we pass a function as a parameter in java?

Java 8 and above Using Java 8+ lambda ...READ MORE

answered Aug 28, 2018 in Java by Daisy
• 8,120 points
381 views
0 votes
1 answer

What is a name function in JavaScript & how to define it?

A named function declares a name as ...READ MORE

answered Mar 7, 2019 in Others by Frankie
• 9,830 points
771 views
0 votes
1 answer

Does Java have a IN operator or function like SQL

A Java-language was designed to be powerful ...READ MORE

answered May 30 in Others by Sohail
• 740 points
15 views
0 votes
1 answer

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

You can use java.util.Collection.contains() for collections.  If a non-null ...READ MORE

answered May 31 in Others by nisha
• 2,160 points
16 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP