Created
May 17, 2021 15:28
-
-
Save gauravkukade/e2f15eea99ddb869b406ba90f439e419 to your computer and use it in GitHub Desktop.
A Java program to check if a primitive array contains a primitive value using the Java 8 Stream API. Check the blogpost at https://coderolls.com/java-check-if-array-contains-value-or-element/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Java program to check if a primitive array contains a primitive value | |
* using the Java 8 Stream API | |
* | |
* @author Gaurav Kukade at coderolls.com | |
* | |
*/ | |
public class CheckInPrimitiveArrayUsingStream { | |
public static void main(String[] args) { | |
int[] numbers = {1, 2, 3, 4, 5, 6, 7}; | |
int intToCheck = 2; | |
boolean contains = IntStream.of(numbers).anyMatch(x -> x == intToCheck); | |
if(contains){ | |
System.out.println("The numbers array contians the value '"+intToCheck+"'."); | |
}else { | |
System.out.println("The numbers array does not contian the value '"+intToCheck+"'."); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment