{{ site.name }}
+{{ site.name }}
{{ site.description }}
diff --git a/_posts/array/2021-05-17-java-check-if-array-contains-value-or-element.md b/_posts/array/2021-05-17-java-check-if-array-contains-value-or-element.md new file mode 100644 index 0000000..6bc9971 --- /dev/null +++ b/_posts/array/2021-05-17-java-check-if-array-contains-value-or-element.md @@ -0,0 +1,327 @@ +--- +layout: post +title: " Java: Check If Array Contains Value or Element" +author: gaurav +categories: [Java, Array] +description: "In this quick beginner-friendly tutorial, we will see how to check if an array contains a value or an element." +--- + +In this quick beginner-friendly tutorial, we will see how to check if an array contains a value or an element using the Java Programming Language. + +We can check if an element or value is present or not in two types of arrays, +1. For String Array +2. For Primitive Array + +## 1. For String Array. + +For String Array, we can check if an array contains a particular string value using the `contains()` method. + +The code snippet is given below. + +```java +Arrays.asList(yourArray).contains(yourValue); +``` + +I have given a program below that checks if an array contains a particular string value. + +```java +import java.util.Arrays; + +/** + * A Java program that checks if a String array contains a particular string value + * + * @author Gaurav Kukade + * + */ +public class CheckInStringArray { + + public static void main(String[] args) { + + String [] names = {"gaurav", "shubham", "krishna", "mayur"}; + + String checkForString = "mayur"; + + boolean stringPresentInArray = Arrays.asList(names).contains(checkForString); + + if(stringPresentInArray) { + System.out.println("String value '"+checkForString+"' is present in names Array."); + }else { + System.out.println("String value '"+checkForString+"' is not present in names Array."); + } + + } +} +``` +Output: +``` +String value 'mayur' is present in names Array. +``` +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/1914b7274cd5017364e78d2e6bc5f5bb) + +### Java 8 Update + +In Java 8, we have Streams API. We can use it to check if a String array contains a particular String value. + +I have given a complete program below. + +```java +import java.util.Arrays; + +/** + * A Java program that checks if a String array contains a particular string value + * using the Java 8 Stream API. + * + * @author Gaurav Kukade + * + */ +public class CheckInStringArrayUsingStream { + + public static void main(String[] args) { + + String [] names = {"gaurav", "shubham", "krishna", "mayur"}; + + String checkForString = "mayur"; + + boolean stringPresentInArray = Arrays.stream(names).anyMatch(checkForString::equals);; + + if(stringPresentInArray) { + System.out.println("String value '"+checkForString+"' is present in names Array."); + }else { + System.out.println("String value '"+checkForString+"' is not present in names Array."); + } + } +} +``` +Output: +``` +String value 'mayur' is present in names Array. +``` +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/3df7496b88b2222ab34d685f5287e725) + +## 2. For Primitive Array + +When we have to check if a primitive array (int[], long[], char[], etc) contains any particular primitive values, we have to loop over the array and check the condition manually. ( Till Java 7) + +I have given a program below that shows how to check if a primitive array (int[], long[], char[], etc) contains a primitive value or not. + +```java +/** + * A Java program to check if a primitive array contains a primitive value + * + * @author Gaurav Kukade at coderolls.com + * + */ +public class CheckInPrimitiveArray { + + public static void main(String[] args) { + + int[] numbers = {1, 2, 3, 4, 5, 6, 7}; + + int intToCheck = 5; + boolean contains = false; + + for(int number: numbers) { + // check if number and intToCheck are equal, + // if yes make contains as true and break the loop + if(number == intToCheck) { + contains = true; + break; + } + } + + 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+"'."); + } + } +} +``` +Output: +``` +The numbers array contians the value '5'. +``` +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/7cf0ba43baccbc9e828277c19f5c14a7) + +### Java 8 Update + + +In Java 8, we have Streams API. We can use it check if a primitive array (int[], long[], char[], etc) contains a primitive value. + +We can use classes like `IntStream`, `DoubleStream` or `LongStream` to check whether an array contains `int`, `double` or `long` values respectively. + +I have given a complete program below. + +```java +/** + * 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+"'."); + } + } +} +``` +Output: +```java +The numbers array contians the value '2'. +``` +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/e2f15eea99ddb869b406ba90f439e419) + +## Check if an array contains a value using the 'Apache Commons Lang' library + + +Apache Commons Lang library has useful functions to manipulate the array. + +It is not recommended to add a new dependency just to check if your array contains a value. + +You can go with Apache Commons Lang library to perform such a simple operation if, + +- If your project is not using Java 8 +- it already has a dependency on the 'Apache Commons Lang library' + +Since Apache Commons Lang library' is the most commonly used library, there are chances that your project may already have it. +``` + +