Check for a Value in an Array in Java

Exercise:

Write a Java Program to check if an array contains a given value.

1.Check if Int Array contains a given value

Click Here to View the Solution!
public class ContainsValue {
     public static void main(String[] args) {
        int[] num = {1,2,3,4,11,12,13,14,15,16,17};
        int toFind = 13;
        boolean found = false;
 
        for (int n : num) {
            if (n == toFind) {
                found = true;
                break;
            }
        }
 
        if(found)
            System.out.println(toFind + " is found.");
        else
            System.out.println(toFind + " is not found.");
    }
} 
Click Here to View the Output!
13 is found.
Click Here to View the Explanation!

This program is used to find out that whether a given number is present in an array by using a for-each loop.

  • Initially, an integer array num is initialized and contains 11 integer elements. Also, an integer variable toFind is initialized that will hold the given number ‘13’ that is to be found.
  • A for-each loop will iterate between all the elements of the array num and compare each element with the value of toFind.
  • If an element becomes equal to toFind, found will be set true and a message will be printed “13 is found”. If not, then found will remain false and loop will go to the next iteration.

2.Check using Stream

Click Here to View the Solution!
import java.util.stream.IntStream;
 public class ContainsValue {
 
    public static void main(String[] args) {
        int[] num = {1, 2, 3, 4, 5,6,7,8,9,10};
        int toFind = 4;
        boolean found = IntStream.of(num).anyMatch(n -> n == toFind);
        if(found)
            System.out.println(toFind + " is found.");
        else
            System.out.println(toFind + " is not found.");
    }
} 
Click Here to View the Output!
4 is found.
Click Here to View the Explanation!
  • In this program, a given number is checked in an array by using InStream of the Stream package in Java and Stream’s anyMatch() method.
  • Initially, an integer array num is initialized and contains 5 integer elements. Also, an integer variable toFind is initialized that will hold the given number ‘4’ that is to be found.
  • This program uses a method anyMatch() after converting an array into an InStream instead of using the for-each loop like the previous program.
  • A Boolean type variable found will hold the anyMatch() method which takes a predicate ‘n’ and an expression n == toFind that will return a Boolean value.
  • In the anyMatch() method, the predicate will compare every element in the stream with the toFind and returns either true or false.
  • If this method returns true, then found will also return true and print 4 is found.

3.check for non primitive Data type

Click Here to View the Solution!
import java.util.Arrays;
public class ContainsValue {
     public static void main(String[] args) {
     String[] strings = {"One", "Two", "Three", "Four","Five", "Six" };
     String toFind = "Four";
 
 boolean found = Arrays.stream(strings).anyMatch(t -> t.equals(toFind));
         if(found)
            System.out.println(toFind + " is found.");
        else
            System.out.println(toFind + " is not found.");
    }
} 
Click Here to View the Output!
Five is found.
Click Here to View the Explanation!
  • In this program, a given string is checked in a non-primitive string array by using InStream of the Stream package in Java and Stream’s anyMatch() method..
  • Initially, a non-primitive string array strings is initialized and contains 6 string elements. Also, a string variable toFind is initialized that will hold the given string ‘Five’ that is to be found.
  • In the Boolean type variable found, the array is first converted into a stream by using the stream() method of Arrays and then the anyMatch() method that will compare all the elements in the stream with toFind to check whether it is present in the array.
  • If the anyMatch() method returns true, then found will also return true. Else, false.