Java 一个用于检查元素是否在列表中的衬垫
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2135379/
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
One liner to check if element is in the list
提问by Vishal
I have been working on and off with Java/Python. Now in this situation I want to check if the element is in the list and do stuff...
我一直在使用 Java/Python。现在在这种情况下,我想检查元素是否在列表中并执行操作...
Python says:
Python 说:
if "a" in ["a", "b", "c"]:
print "It's there!"
Does java provide any one liner for this rather than creating ArrayList / Set or similar data structure in steps and adding elements to it?
java 是否为此提供了任何一种衬垫,而不是逐步创建 ArrayList / Set 或类似的数据结构并向其中添加元素?
Thanks
谢谢
采纳答案by Mark Elliot
回答by rayd09
There is a boolean contains(Object obj) method within the List interface.
List 接口中有一个 boolean contains(Object obj) 方法。
You should be able to say:
你应该能够说:
if (list.contains("a")) {
System.out.println("It's there");
}
According to the javadoc:
根据javadoc:
boolean contains(Object o)
Returns true if this list contains the specified element.
More formally, returns true if and only if this list contains at
least one element e such that (o==null ? e==null : o.equals(e)).
回答by VoidPointer
You can use java.util.Arrays.binarySearch to find an element in an array or to check for its existence:
您可以使用 java.util.Arrays.binarySearch 在数组中查找元素或检查其是否存在:
import java.util.Arrays;
...
char[] array = new char[] {'a', 'x', 'm'};
Arrays.sort(array);
if (Arrays.binarySearch(array, 'm') >= 0) {
System.out.println("Yes, m is there");
}
Be aware that for binarySearch to work correctly, the array needs to be sorted. Hence the call to Arrays.sort() in the example. If your data is already sorted, you don't need to do that. Thus, this isn't strictly a one-liner if you need to sort your array first. Unfortunately, Arrays.sort() does not return a reference to the array - thus it is not possible to combine sort and binarySearch (i.e. Arrays.binarySearch(Arrays.sort(myArray), key)) does not work).
请注意,要使 binarySearch 正常工作,需要对数组进行排序。因此在示例中调用了 Arrays.sort()。如果您的数据已经排序,则不需要这样做。因此,如果您需要先对数组进行排序,那么这并不是严格意义上的单行代码。不幸的是, Arrays.sort() 不返回对数组的引用 - 因此不可能组合 sort 和 binarySearch(即 Arrays.binarySearch(Arrays.sort(myArray), key)) 不起作用)。
If you can afford the extra allocation, using Arrays.asList() seems cleaner.
如果您负担得起额外的分配,使用 Arrays.asList() 似乎更干净。
回答by Andreas Dolk
If he really wants a one liner without any collections, OK, he can have one:
如果他真的想要一个没有任何收藏品的单衬,好吧,他可以拥有一个:
for(String s:new String[]{"a", "b", "c")) if (s.equals("a")) System.out.println("It's there");
*smile*
*微笑*
(Isn't it ugly? Please, don't use it in real code)
(是不是很丑?拜托,不要在真正的代码中使用它)
回答by Peter Lawrey
You could try using Strings with a separator which does not appear in any element.
您可以尝试使用带有未出现在任何元素中的分隔符的字符串。
if ("|a|b|c|".contains("|a|"))
回答by Tom Hawtin - tackline
In JDK7:
在 JDK7 中:
if ({"a", "b", "c"}.contains("a")) {
Assuming the Project Coin collections literals project goes through.
假设 Project Coin collections 文字项目通过。
Edit:It didn't.
编辑:它没有。
回答by Encipher
public class Itemfound{
public static void main(String args[]){
if( Arrays.asList("a","b","c").contains("a"){
System.out.println("It is here");
}
}
}
}
This is what you looking for. The contains() method simply checks the index of element in the list. If the index is greater than '0' than element is present in the list.
这就是你要找的。contains() 方法只是检查列表中元素的索引。如果索引大于“0”,则列表中存在元素。
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
回答by PPartisan
I would use:
我会用:
if (Stream.of("a","b","c").anyMatch("a"::equals)) {
//Code to execute
};
or:
或者:
Stream.of("a","b","c")
.filter("a"::equals)
.findAny()
.ifPresent(ignore -> /*Code to execute*/);