Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Monday, October 12, 2020

Java Program to Check if An Array Contains a Given Value or Not (+Java 8)

1. Overview

In this article, you'll learn how to check an array contains the given value in it or not.

It is not mandatory to have complete knowledge of the arrays but you should know the following.

how to create and initialize an array in java.

How to declare the variables in java?

Java Program to Check if An Array Contains a Given Value or Not (+Java 8)


2. Example 1 To check the given number in the array using the native approach

This is a simple approach to search the given value in an array and this is called a linear search algorithm.

package com.javaprogramto.programs.arrays.search;

public class FIndValue {

	public static void main(String[] args) {

		int[] array = { 1, 4, 6, 2, 5 };

		int findValueOf = 6;

		boolean isFound = false;
		for (int i = 0; i < array.length; i++) {

			if (array[i] == findValueOf) {
				isFound = true;
				break;
			}

		}

		if (isFound) {
			System.out.println("Number " + findValueOf + " is found in the  array");
		} else {
			System.out.println("Number " + findValueOf + " is not found in the  array");
		}
	}

}

Output:

Number 6 is found in the  array

In the above example for loop is used to iterate over the array values and compare each value with the findValueOf variable value.

3. Example 2 To find the number using Java 8 Streams for primitive values

Next, let us use the Java 8 Stream API to check the given value in an array or not.

Now, we are checking with primitives values so you should use the IntStream.

First, convert an array to int stream using IntStream.of() method and call anyMatch() method to check value.

import java.util.stream.IntStream;

public class IntStreamFindValue {

	public static void main(String[] args) {

		int[] array = { 10, 30, 20, 90, 40, 60 };

		int findValueOf = 90;

		boolean isFound = false;

		isFound = IntStream.of(array).anyMatch(condition -> condition == findValueOf);

		if (isFound) {
			System.out.println("Number " + findValueOf + " is found in the array in java 8");
		} else {
			System.out.println("Number " + findValueOf + " is not found in the  array in java 8");
		}
	}

}

Output:

Number 90 is found in the array in java 8

If a number is found in the array then anyMatch() returns true else false.

4. Example 3 To find the number using Java 8 Streams for non-primitive values

In the above section, you've seen for primitive arrays but how can you find in the string array.

Interestingly, the same anyMatch() method works for any type of array.

import java.util.Arrays;

public class IntStreamFindStringValue {

	public static void main(String[] args) {

		String[] stringArray = new String[] { "hello", "how", "life", "is", "going" };

		String findString = "java";

		boolean isFound = false;

		isFound = Arrays.stream(stringArray).anyMatch(value  -> value.equals(findString));

		if (isFound) {
			System.out.println("String " + findString + " is found in the array in java 8");
		} else {
			System.out.println("String " + findString + " is not found in the  array in java 8");
		}
	}

}

Output:

String java is not found in the  array in java 8

5. Conclusion

In this article, you've seen the different ways to check if an array contains a given value or not and also examples using java 8 api.

As usual, all examples are over GitHub.

Arrays

Stream.anyMatch()

No comments:

Post a Comment

Please do not add any spam links in the comments section.