Java 是否有“IN”运算符或类似 SQL 的函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3565954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Java have a "IN" operator or function like SQL?
提问by codea
I want to know if there's a way of doing something like this in Java :
我想知道是否有办法在 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.
我知道我可以为此创建一个函数,但我只想知道 Java 是否已经为此提供了一些功能。
Thank you!
谢谢!
采纳答案by jjnguy
There are many collections that will let you do something similar to that. For example:
有很多集合可以让你做类似的事情。例如:
With Strings
:
与Strings
:
String s = "I can has cheezeburger?";
boolean hasCheese = s.contains("cheeze");
or with Collections
:
或与Collections
:
List<String> listOfStrings = new ArrayList<String>();
boolean hasString = listOfStrings.contains(something);
However, there is no similar construct for a simple String[]
.
但是,对于简单的String[]
.
回答by kgiannakakis
Java has no "in" operator.
Java 没有“in”运算符。
For arrays you need to iterate them and look for a matching item.
对于数组,您需要迭代它们并寻找匹配项。
For Lists you can use the contains
method.
对于列表,您可以使用该contains
方法。
回答by mdma
If the array is sorted, you can use Arrays.binarySearchto find the data.
如果数组已排序,则可以使用Arrays.binarySearch查找数据。
Like this:
像这样:
if (Arrays.binarySearch(stringArray, word)>=0) {
// word found in the array.
}
If the array is unsorted, commons lang, has the ArrayUtils.indexOfmethod.
如果数组未排序,commons lang 有ArrayUtils.indexOf方法。
Finally, if you are searching a large array, then a parallel search may be worthwhile using ParallelArray
, from JSR166 - coming in Java SE 7 and available now as a download. This articlegives an introduction.
最后,如果您正在搜索一个大数组,那么并行搜索可能值得使用ParallelArray
,来自 JSR166 - 来自 Java SE 7,现在可以下载。本文进行介绍。
回答by Brian
if(myArrayList.contains("hello"))
{
// yay
}
回答by bwawok
give us more details on what your problem looks like, and what you need to check. There are several different ways to do stuff like this. If you need to keep asking if item A is in set B.. make set B a HashSet, and you can call contains very quick. You can get the same effect in several java-ish ways, but they will vary depending on your data etc.
向我们提供有关您的问题的详细信息以及您需要检查的内容。有几种不同的方法可以做这样的事情。如果您需要不断询问项目 A 是否在集合 B 中.. 使集合 B 成为HashSet,并且您可以非常快速地调用 contains 。您可以通过多种 java-ish 方式获得相同的效果,但它们会因您的数据等而异。
回答by Fish
It first depends what the array is an array of. If it is an array of the exact words you can convert it to a regular collection and use the contains() method. If it is an array of sentences or phrases etc. you must iterate over it to tell.
它首先取决于数组是什么数组。如果它是精确单词的数组,您可以将其转换为常规集合并使用 contains() 方法。如果它是一组句子或短语等,您必须遍历它才能判断。
The most general solution that will catch all these is to iterate as follows.
捕获所有这些的最通用的解决方案是迭代如下。
for(String s : stringArray)
{
if(s.indexOf(word) > 0) return true;
}
return false;
This looks at every string in the array, and checks to see if the word is contained anywhere within it. It will return true after the first occurrance is found.
这会查看数组中的每个字符串,并检查单词是否包含在其中的任何位置。找到第一次出现后,它将返回 true。
回答by polygenelubricants
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.
Java 语言的设计既强大又简单。在语言级别,Java 中没有这样的运算符,但肯定已经编写了一些库来促进此类查询。
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 optimizedfor such queries.
如果您想知道某个对象是否是某个对象集的成员,那么您应该使用 --还有什么来代替数组?——一个Set
。这些数据结构自然允许此类查询,而且更好的是,它们针对此类查询进行了优化。
You can easily check if a given string is in a Set<String>
like this:
您可以轻松检查给定的字符串是否是Set<String>
这样的:
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.
使用HashSet
,contains
是一个恒定时间的操作。这比通过数组进行线性搜索要好得多。
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.
您应该熟悉 Java 集合框架为您提供的数据结构。它们允许您编写惯用的、可维护的、灵活的代码,并得到许多可用于这些数据结构的强大算法的支持。
See also
也可以看看
- Java Tutorials/Collections Framework
List<E>
,Set<E>
,Queue<E>
,Map<K,V>
,SortedMap<K,V>
,NavigableSet<E>
, etc.
- Effective Java 2nd Edition, Item 25: Prefer lists to arrays
- Java 教程/集合框架
- Effective Java 第 2 版,第 25 条:列表优先于数组
回答by gpeche
You can use java.util.Collection.contains() for collections. If what you have is a non-null array, you can use java.util.Arrays.asList(array).contains().
您可以将 java.util.Collection.contains() 用于集合。如果您拥有的是非空数组,则可以使用 java.util.Arrays.asList(array).contains()。
回答by yobro
tabs[j].indexOf("lelamondeestgrand...")!=0
回答by asmund.skalevik
In SQL
在 SQL 中
x in ('Alice', 'Bob', 'Carol')
In Java:
在 Java 中:
Arrays.asList("Alice", "Bob", "Carol").contains(x)