如何确定在Java数组中是否包含特定值?
String[] values = {"AB","BC","CD","AE"}; boolean contains = Arrays.stream(values).anyMatch("s"::equals);
要检查的阵列是否int,double或long包含一个值使用IntStream,DoubleStream或LongStream分别。
例
int[] a = {1,2,3,4}; boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
引用数组不好。对于这种情况,我们要紧紧追赶。从Java SE 9开始,我们有了Set.of。
Set.of
private static final Set<String> VALUES = Set.of( "AB","BC","CD","AE" );
“给出String,是否有测试VALUES是否包含s的好方法?”
VALUES.contains(s)