Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save gauravkukade/e2f15eea99ddb869b406ba90f439e419 to your computer and use it in GitHub Desktop.
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/
/**
* 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