English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input/Output (I/O)

Java Reader/Writer

Java Other Topics

Java program to check if an array contains a given value

Comprehensive Java Examples

In this program, you will learn how to check if an array contains a given value in Java.

Example1: Check if the Int array contains the given value

public class Contains {
    public static void main(String[] args) {
        int[] num = {1, 2, 3, 4, 5};
        int toFind = 3;
        boolean found = false;
        for (int n : num) {
            if (n == toFind) {
                found = true;
                break;
            }
        }
        if(found)
            System.out.println(toFind + " Found");
        else
            System.out.println(toFind + " Not Found");
    }
}

When running the program, the output is:

3 Found

In the above program, we have an integer array stored in the variable num. Similarly, the number to be found is stored in toFind

Now, we use a foreach loop to traverse all elements of num and check whether toFind is equal to n separately

If it is, we set find to true and exit the loop. If not, we move on to the next iteration

Example2: Use Stream to check if the array contains the given value

import java.util.stream.IntStream;
public class Contains {
    public static void main(String[] args) {
        int[] num = {1, 2, 3, 4, 5};
        int toFind = 7;
        boolean found = IntStream.of(num).anyMatch(n -> n == toFind);
        if(found)
            System.out.println(toFind + " Found");
        else
            System.out.println(toFind + " Not Found");
    }
}

When running the program, the output is:

7 Not found

In the above program, we did not use a foreach loop, but converted the array to IntStream and used its anyMatch() method

The anyMatch() method takes a predicate, expression, or function that returns a boolean. In our example, the predicate compares each element n in the stream to toFind and returns true or false

If any of the elements n return true, then found will also be set to true

Example3: Check if the array contains the given value of a non-primitive type

import java.util.Arrays;
public class Contains {
    public static void main(String[] args) {
        String[] strings = {"One", "Two", "Three", "Four", "Five"};
        String toFind = "Four";
        boolean found = Arrays.stream(strings).anyMatch(t -> t.equals(toFind));
        if(found)
            System.out.println(toFind + " Found");
        else
            System.out.println(toFind + " Not Found");
    }
}

When running the program, the output is:

Four Found

In the above program, we used the non-primitive data type String and used the Arrays.stream() method to first convert it to a stream, and then used anyMatch() to check if the array contains the given value toFind

Comprehensive Java Examples