How to test that an array contains a certain value?

0 votes
If I have an array and if I want to find certain value from the array, then how to do it?
Apr 17, 2018 in Java by Daisy
• 8,050 points
69 views

2 answers to this question.

0 votes

We can use a Stream to check whether an array contains specific int, long or double values.

For this solution, we have to import IntStream library.

import java.util.stream.IntStream;

Following is the solution to find a specific integer value.

public static void main(String[] args) {

  int[] a = {1,2,3,4};

  boolean result = IntStream.of(a).anyMatch(x -> x == 5);

  System.out.println(result);

 }

}

Output is:

false
answered Apr 17, 2018 by parth295
• 4,640 points
0 votes
public static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
private static final String[] VALUES = new String[] {"AB","BC","CD","AE"};
private static final Set<String> VALUES = new HashSet<String>(Arrays.asList(
     new String[] {"AB","BC","CD","AE"}
));
answered Jul 17, 2018 by Sushmita
• 6,880 points

Related Questions In Java

+1 vote
3 answers

How to convert a List to an Array in Java?

Either: Foo[] array = list.toArray(new Foo[list.size()]); or: Foo[] array = ...READ MORE

answered Aug 7, 2018 in Java by Akrati
• 3,150 points
69 views
0 votes
2 answers

How to rotate an array from a particular index?

private static void rotate(int[] arr, int order) ...READ MORE

answered Aug 29 in Java by Sirajul
• 39,540 points
31 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,050 points
59 views
0 votes
2 answers

How to convert an array to arraylist?

In Java 9 you can use: List<String> list= List.of("Hello", "World", ...READ MORE

answered Aug 1, 2018 in Java by samarth295
• 2,190 points
52 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 39,540 points
190 views
0 votes
5 answers

How to compare Strings in Java?

String fooString1 = new String("foo"); String fooString2 = ...READ MORE

answered Jul 12, 2018 in Java by Daisy
• 8,050 points
136 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
65 views
0 votes
1 answer

How to divide a string in two parts

String s="yourstring"; boolean flag = true; for(int i=0;i<s.length();i++) { ...READ MORE

answered Apr 13, 2018 in Java by Rishabh
• 3,540 points
54 views
0 votes
3 answers

How to sort an array in java?

import java.util.Arrays; public class Sort { ...READ MORE

answered Aug 24, 2018 in Java by parth295
• 4,640 points
83 views
0 votes
2 answers

How to round up a value to 2 decimal places?

double d = 2.34568; DecimalFormat f = new ...READ MORE

answered Aug 30, 2018 in Java by Sushmita
• 6,880 points
71 views