我有一个包含以下值的String[]
:
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
给定字符串 s
,是否有一种好的方法来测试 VALUES
是否包含 s
?
请使用以下-
String[] values = {"AB","BC","CD","AE"};
String s = "A";
boolean contains = Arrays.stream(values).anyMatch(v -> v.contains(s));
For arrays of limited length use the following (as given by camickr). This is slow for repeated checks, especially for longer arrays (linear search).
Arrays.asList(...).contains(...)
For fast performance if you repeatedly check against a larger set of elements
An array is the wrong structure. Use a TreeSet
and add each element to it. It sorts elements and has a fast exist()
method (binary search).
If the elements implement Comparable
& you want the TreeSet
sorted accordingly:
ElementClass.compareTo()
method must be compatable with ElementClass.equals()
: see Triads not showing up to fight? (Java Set missing an item)
TreeSet myElements = new TreeSet();
// Do this for each element (implementing *Comparable*)
myElements.add(nextElement);
// *Alternatively*, if an array is forceably provided from other code:
myElements.addAll(Arrays.asList(myArray));
Otherwise, use your own Comparator
:
class MyComparator implements Comparator<ElementClass> {
int compareTo(ElementClass element1; ElementClass element2) {
// Your comparison of elements
// Should be consistent with object equality
}
boolean equals(Object otherComparator) {
// Your equality of comparators
}
}
// construct TreeSet with the comparator
TreeSet myElements = new TreeSet(new MyComparator());
// Do this for each element (implementing *Comparable*)
myElements.add(nextElement);
The payoff: check existence of some element:
// Fast binary search through sorted elements (performance ~ log(size)):
boolean containsElement = myElements.exists(someElement);
请检查此内容
String[] VALUES = new String[]{"AB", "BC", "CD", "AE"};
String s;
for (int i = 0; i < VALUES.length; i++) {
if (VALUES[i].equals(s)) {
// do your stuff
} else {
//do your stuff
}
}
else
(因此,如果您正在该数组中查找“AB”,它将会去那里3次,因为3个值不是“AB”)。 - Bernhard Barker试试这个:
ArrayList<Integer> arrlist = new ArrayList<Integer>(8);
// use add() method to add elements in the list
arrlist.add(20);
arrlist.add(25);
arrlist.add(10);
arrlist.add(15);
boolean retval = arrlist.contains(10);
if (retval == true) {
System.out.println("10 is contained in the list");
}
else {
System.out.println("10 is not contained in the list");
}
Arrays.asList() -> 然后调用contains()方法总是可行的,但搜索算法更好,因为您无需创建轻量级列表包装器以围绕数组,这正是Arrays.asList()所做的。
public boolean findString(String[] strings, String desired){
for (String str : strings){
if (desired.equals(str)) {
return true;
}
}
return false; //if we get here… there is no desired String, return false.
}
使用 Array.BinarySearch(array,obj)
查找数组中是否存在给定的对象。
示例:
if (Array.BinarySearch(str, i) > -1)` → true --exists
false --不存在
Array.BinarySearch
和 Array.FindIndex
是 .NET 方法,不存在于 Java 中。 - ataylorimport java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
public class Test {
public static final List<String> VALUES =
Arrays.asList("AA", "AB", "BC", "CD", "AE");
public static void main(String args[]) {
Predicate<String> containsLetterA = VALUES -> VALUES.contains("AB");
for (String i : VALUES) {
System.out.println(containsLetterA.test(i));
}
}
}
http://mytechnologythought.blogspot.com/2019/10/java-8-predicate-test-method-example.html
https://github.com/VipulGulhane1/java8/blob/master/Test.java
由于我正在使用基本类型byte和byte[]处理低级Java,到目前为止,我发现最好的工具是来自bytes-javahttps://github.com/patrickfav/bytes-java,看起来是一份不错的工作。
Arrays.stream(VALUES).anyMatch(value -> StringUtils.equalsIgnoreCase("s", value));
java.util.Arrays
中找到一个简单的indexOf
和contains
,它们都包含直接的循环。是的,你可以在1分钟内编写它们;但我仍然去了StackOverflow,期望在JDK的某个地方找到它们。 - tucuxi