我有一个String[]这样的值:
String[]
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
鉴于String s,是否有一种测试是否VALUES包含的好方法s?
String s
VALUES
s
Arrays.asList(yourArray).contains(yourValue)
警告:这不适用于基元数组(请参阅注释)。
String[] values = {"AB","BC","CD","AE"}; boolean contains = Arrays.stream(values).anyMatch("s"::equals);
int要检查, doubleor的数组是否long包含值,请分别使用IntStream, DoubleStreamor LongStream。
int
double
long
IntStream
DoubleStream
LongStream
int[] a = {1,2,3,4}; boolean contains = IntStream.of(a).anyMatch(x -> x == 4);