Java 8 How to check an Array contains a specific value ?

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, I am going to show you how to check an array contains a specific value in Java 8.
import java.util.Arrays;
import java.util.stream.DoubleStream;
import java.util.stream.IntStream;
import java.util.stream.LongStream;

public class Array_Contains {

    public static void main(String[] args) {
        
        int[] i = {10,20,30,40};
        boolean contains = IntStream.of(i).anyMatch(x -> x == 40);
        System.out.println("is int[] contains 40   : "+contains);
        
        double[] f = {1.2d,2.9d,3.0d,4.7d};
        boolean contains2 = DoubleStream.of(f).anyMatch(x -> x == 4.7d);
        System.out.println("is double[] contains 4.7d : "+contains2);
        
        long[] l = {198,165,78,140};
        boolean contains3 = LongStream.of(l).anyMatch(x -> x == 20);
        System.out.println("is long[] contains 20 : "+contains3);
        
        String[] s = new String[] {"AA","BB","CC","DD"};
        System.out.println("is String[] contains CD : "+Arrays.asList(s).contains("CD"));

    }

}
Output :
is int[] contains 40   : true
is double[] contains 4.7d : true
is long[] contains 20 : false
is String[] contains CD : false
Happy Learning 🙂

How to Convert Iterable to Stream Java 8
C Program – Find Largest and Smallest number in an Array
How to convert List to Map in Java 8
How to get Stream count in Java 8
How to Merge Streams in Java 8
Bubble Sort In Java
How to Sort an Array in Parallel in Java 8
Java how to convert ArrayList to Array Example
How to Convert Java 8 Stream to Array
How to Shuffle an Array using Java
Java Program To Find Largest and Smallest Number in Array
Java Program to find Smallest and Largest element in Array
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator

How to Convert Iterable to Stream Java 8
C Program – Find Largest and Smallest number in an Array
How to convert List to Map in Java 8
How to get Stream count in Java 8
How to Merge Streams in Java 8
Bubble Sort In Java
How to Sort an Array in Parallel in Java 8
Java how to convert ArrayList to Array Example
How to Convert Java 8 Stream to Array
How to Shuffle an Array using Java
Java Program To Find Largest and Smallest Number in Array
Java Program to find Smallest and Largest element in Array
Java 8 How to convert Stream to List
How to Filter a Map in Java 8
User defined sorting with Java 8 Comparator
 


Δ
 Java8 – Install Windows
 Java8 – foreach
 Java8 – forEach with index
 Java8 – Stream Filter Objects
 Java8 – Comparator Userdefined
 Java8 – GroupingBy
 Java8 – SummingInt
 Java8 – walk ReadFiles
 Java8 – JAVA_HOME on Windows
 Howto – Install Java on Mac OS
 Howto – Convert Iterable to Stream
 Howto – Get common elements from two Lists
 Howto – Convert List to String
 Howto – Concatenate Arrays using Stream
 Howto – Remove duplicates from List
 Howto – Filter null values from Stream
 Howto – Convert List to Map
 Howto – Convert Stream to List
 Howto – Sort a Map
 Howto – Filter a Map
 Howto – Get Current UTC Time
 Howto – Verify an Array contains a specific value
 Howto – Convert ArrayList to Array
 Howto – Read File Line By Line
 Howto – Convert Date to LocalDate
 Howto – Merge Streams
 Howto – Resolve NullPointerException in toMap
 Howto -Get Stream count
 Howto – Get Min and Max values in a Stream
 Howto – Convert InputStream to String