How do I determine whether an array contains a particular value in Java

0 votes

I have a String[] with values like so:

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

Given String s, is there a good way of testing whether VALUES contains s?

Dec 21, 2020 in Java by Rajiv
• 8,910 points
993 views

1 answer to this question.

0 votes
Arrays.asList(yourArray).contains(yourValue)

Warning: this doesn't work for arrays of primitives (see the comments).


Since java-8 you can now use Streams.

String[] values = {"AB","BC","CD","AE"};
boolean contains = Arrays.stream(values).anyMatch("s"::equals);

To check whether an array of int, double or long contains a value use IntStream, DoubleStream or LongStream respectively.

Example

int[] a = {1,2,3,4};
boolean contains = IntStream.of(a).anyMatch(x -> x == 4);
answered Dec 21, 2020 by Gitika
• 65,910 points

Related Questions In Java

0 votes
0 answers

How do I check if an array includes a value in JavaScript?

What is the shortest and most efficient ...READ MORE

Sep 28, 2022 in Java by Nicholas
• 7,760 points
436 views
0 votes
2 answers

How do I convert a String to an int in Java?

Use the lines of code mentioned below:- String ...READ MORE

answered Feb 9, 2022 in Java by Soham
• 9,700 points
2,611 views
0 votes
1 answer

How do I check if a string contains a substring in Java?

Hi@akhtar, The first and foremost way to check ...READ MORE

answered Dec 30, 2020 in Java by MD
• 95,440 points

edited Jul 5, 2023 by Khan Sarfaraz 51,375 views
0 votes
0 answers

How do I declare and initialize an array in Java?

How do I declare and initialize an ...READ MORE

Feb 8, 2022 in Java by Edureka
• 120 points
279 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,029 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
979 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
3,174 views
0 votes
2 answers

What is the syntax to initialize an array?

Rather than learning un-Official websites learn from ...READ MORE

answered Aug 2, 2018 in Java by samarth295
• 2,220 points
703 views
0 votes
1 answer

How do I generate random integers within a specific range in Java?

Before Java 1.7, the standard way to ...READ MORE

answered Dec 22, 2020 in Java by Gitika
• 65,910 points
578 views
0 votes
4 answers

How do I check if a file exists in Java?

To test to see if a file ...READ MORE

answered Dec 29, 2020 in Java by Rajiv
• 8,910 points
59,326 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP