How do we determine if a particular value exists in an array?

Asked 1 years ago, Updated 1 years ago, 112 views

Let's say we have an array of String[] with the following values.

public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};

Given the strings, is there a good way to check if s is present in the VALUES array?

java java-8 array

2022-09-22 15:20

1 Answers

Arrays.asList(yourArray).contains(yourValue)

This method is not available for arrays that store primary types.

Stream allows you to find specific values in an array that has values of int, double, and long types.

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);


2022-09-22 15:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.