|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: " Java: Check If Array Contains Value or Element" |
| 4 | +author: gaurav |
| 5 | +categories: [Java, Array] |
| 6 | +description: "In this quick beginner-friendly tutorial, we will see how to check if an array contains a value or an element." |
| 7 | +--- |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +We can check if an element or value is present or not in two types of arrays, |
| 12 | +1. For String Array |
| 13 | +2. For Primitive Array |
| 14 | + |
| 15 | +## 1. For String Array. |
| 16 | + |
| 17 | +For String Array, we can check if an array contains a particular string value using the `contains()` method. |
| 18 | + |
| 19 | +The code snippet is given below. |
| 20 | + |
| 21 | +```java |
| 22 | +Arrays.asList(yourArray).contains(yourValue); |
| 23 | +``` |
| 24 | + |
| 25 | +I have given a program below that checks if an array contains a particular string value. |
| 26 | + |
| 27 | +```java |
| 28 | +import java.util.Arrays; |
| 29 | + |
| 30 | +/** |
| 31 | + * A Java program that checks if a String array contains a particular string value |
| 32 | + * |
| 33 | + * @author Gaurav Kukade |
| 34 | + * |
| 35 | + */ |
| 36 | +public class CheckInStringArray { |
| 37 | + |
| 38 | + public static void main(String[] args) { |
| 39 | + |
| 40 | + String [] names = {"gaurav", "shubham", "krishna", "mayur"}; |
| 41 | + |
| 42 | + String checkForString = "mayur"; |
| 43 | + |
| 44 | + boolean stringPresentInArray = Arrays.asList(names).contains(checkForString); |
| 45 | + |
| 46 | + if(stringPresentInArray) { |
| 47 | + System.out.println("String value '"+checkForString+"' is present in names Array."); |
| 48 | + }else { |
| 49 | + System.out.println("String value '"+checkForString+"' is not present in names Array."); |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | +Output: |
| 56 | +``` |
| 57 | +String value 'mayur' is present in names Array. |
| 58 | +``` |
| 59 | +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/1914b7274cd5017364e78d2e6bc5f5bb) |
| 60 | + |
| 61 | +### Java 8 Update |
| 62 | + |
| 63 | +In Java 8, we have Streams API. We can use it to check if a String array contains a particular String value. |
| 64 | + |
| 65 | +I have given a complete program below. |
| 66 | + |
| 67 | +```java |
| 68 | +import java.util.Arrays; |
| 69 | + |
| 70 | +/** |
| 71 | + * A Java program that checks if a String array contains a particular string value |
| 72 | + * using the Java 8 Stream API. |
| 73 | + * |
| 74 | + * @author Gaurav Kukade |
| 75 | + * |
| 76 | + */ |
| 77 | +public class CheckInStringArrayUsingStream { |
| 78 | + |
| 79 | + public static void main(String[] args) { |
| 80 | + |
| 81 | + String [] names = {"gaurav", "shubham", "krishna", "mayur"}; |
| 82 | + |
| 83 | + String checkForString = "mayur"; |
| 84 | + |
| 85 | + boolean stringPresentInArray = Arrays.stream(names).anyMatch(checkForString::equals);; |
| 86 | + |
| 87 | + if(stringPresentInArray) { |
| 88 | + System.out.println("String value '"+checkForString+"' is present in names Array."); |
| 89 | + }else { |
| 90 | + System.out.println("String value '"+checkForString+"' is not present in names Array."); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | +Output: |
| 96 | +``` |
| 97 | +String value 'mayur' is present in names Array. |
| 98 | +``` |
| 99 | +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/3df7496b88b2222ab34d685f5287e725) |
| 100 | + |
| 101 | +## 2. For Primitive Array |
| 102 | + |
| 103 | +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) |
| 104 | + |
| 105 | +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. |
| 106 | + |
| 107 | +```java |
| 108 | +/** |
| 109 | + * A Java program to check if a primitive array contains a primitive value |
| 110 | + * |
| 111 | + * @author Gaurav Kukade at coderolls.com |
| 112 | + * |
| 113 | + */ |
| 114 | +public class CheckInPrimitiveArray { |
| 115 | + |
| 116 | + public static void main(String[] args) { |
| 117 | + |
| 118 | + int[] numbers = {1, 2, 3, 4, 5, 6, 7}; |
| 119 | + |
| 120 | + int intToCheck = 5; |
| 121 | + boolean contains = false; |
| 122 | + |
| 123 | + for(int number: numbers) { |
| 124 | + // check if number and intToCheck are equal, |
| 125 | + // if yes make contains as true and break the loop |
| 126 | + if(number == intToCheck) { |
| 127 | + contains = true; |
| 128 | + break; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + if(contains){ |
| 133 | + System.out.println("The numbers array contians the value '"+intToCheck+"'."); |
| 134 | + }else { |
| 135 | + System.out.println("The numbers array does not contian the value '"+intToCheck+"'."); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | +``` |
| 140 | +Output: |
| 141 | +``` |
| 142 | +The numbers array contians the value '5'. |
| 143 | +``` |
| 144 | +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/7cf0ba43baccbc9e828277c19f5c14a7) |
| 145 | + |
| 146 | +### Java 8 Update |
| 147 | + |
| 148 | + |
| 149 | +In Java 8, we have Streams API. We can use it check if a primitive array (int[], long[], char[], etc) contains a primitive value. |
| 150 | + |
| 151 | +We can use classes like `IntStream`, `DoubleStream` or `LongStream` to check whether an array contains `int`, `double` or `long` values respectively. |
| 152 | + |
| 153 | +I have given a complete program below. |
| 154 | + |
| 155 | +```java |
| 156 | +/** |
| 157 | + * A Java program to check if a primitive array contains a primitive value |
| 158 | + * using the Java 8 Stream API |
| 159 | + * |
| 160 | + * @author Gaurav Kukade at coderolls.com |
| 161 | + * |
| 162 | + */ |
| 163 | +public class CheckInPrimitiveArrayUsingStream { |
| 164 | + |
| 165 | + public static void main(String[] args) { |
| 166 | + |
| 167 | + int[] numbers = {1, 2, 3, 4, 5, 6, 7}; |
| 168 | + |
| 169 | + int intToCheck = 2; |
| 170 | + boolean contains = IntStream.of(numbers).anyMatch(x -> x == intToCheck); |
| 171 | + |
| 172 | + if(contains){ |
| 173 | + System.out.println("The numbers array contians the value '"+intToCheck+"'."); |
| 174 | + }else { |
| 175 | + System.out.println("The numbers array does not contian the value '"+intToCheck+"'."); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
| 179 | +``` |
| 180 | +Output: |
| 181 | +```java |
| 182 | +The numbers array contians the value '2'. |
| 183 | +``` |
| 184 | +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/e2f15eea99ddb869b406ba90f439e419) |
| 185 | + |
| 186 | +## Check if an array contains a value using the 'Apache Commons Lang' library |
| 187 | + |
| 188 | + |
| 189 | +Apache Commons Lang library has useful functions to manipulate the array. |
| 190 | + |
| 191 | +It is not recommended to add a new dependency just to check if your array contains a value. |
| 192 | + |
| 193 | +You can go with Apache Commons Lang library to perform such a simple operation if, |
| 194 | + |
| 195 | +- If your project is not using Java 8 |
| 196 | +- it already has a dependency on the 'Apache Commons Lang library' |
| 197 | + |
| 198 | +Since Apache Commons Lang library' is the most commonly used library, there are chances that your project may already have it. |
| 199 | +``` |
| 200 | +<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 --> |
| 201 | +<dependency> |
| 202 | + <groupId>org.apache.commons</groupId> |
| 203 | + <artifactId>commons-lang3</artifactId> |
| 204 | + <version>3.11</version> |
| 205 | +</dependency> |
| 206 | +``` |
| 207 | + |
| 208 | +If you want to check if an array contains a value using the Apache commons Lang Library utility functions, you can do it as given below. |
| 209 | + |
| 210 | +```java |
| 211 | +import org.apache.commons.lang3.ArrayUtils; |
| 212 | + |
| 213 | +/** |
| 214 | + * A java program to check if an array contains a value suing the |
| 215 | + * Apache common lang library's ArrayUtils.contains() method |
| 216 | + * |
| 217 | + * @author Gaurav Kukade at coderolls.com |
| 218 | + * |
| 219 | + */ |
| 220 | +public class ArrayUtilsExample { |
| 221 | + |
| 222 | + public static void main(String[] args) { |
| 223 | + |
| 224 | + // check in primitive ( int[]) array |
| 225 | + int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 226 | + int intToCheck = 8; |
| 227 | + |
| 228 | + boolean containsInt = ArrayUtils.contains(numbers, intToCheck); |
| 229 | + |
| 230 | + if(containsInt){ |
| 231 | + System.out.println("The numbers array contians the value '"+intToCheck+"'."); |
| 232 | + }else { |
| 233 | + System.out.println("The numbers array does not contian the value '"+intToCheck+"'."); |
| 234 | + } |
| 235 | + |
| 236 | + // check in String array |
| 237 | + String[] names = {"gaurav", "shubham", "krishna", "mayur"}; |
| 238 | + String stringToCheck = "gaurav"; |
| 239 | + |
| 240 | + boolean containsString = ArrayUtils.contains(names, stringToCheck); |
| 241 | + |
| 242 | + if(containsString) { |
| 243 | + System.out.println("String value '"+stringToCheck+"' is present in names array."); |
| 244 | + }else { |
| 245 | + System.out.println("String value '"+stringToCheck+"' is not present in names array."); |
| 246 | + } |
| 247 | + } |
| 248 | +} |
| 249 | +``` |
| 250 | +Output: |
| 251 | +``` |
| 252 | +The numbers array contians the '8' value. |
| 253 | +String value 'gaurav' is present in names array. |
| 254 | +``` |
| 255 | +Check the above code as [GitHub Gist](https://gist.github.com/gauravkukade/a93dd0faec3bebd75fa9833d5089d482) |
| 256 | + |
| 257 | +## Conclusion |
| 258 | + |
| 259 | +We have seen all the possible ways to check if an array contains a value or not in Java. |
| 260 | + |
| 261 | +We will summarize these ways below |
| 262 | + |
| 263 | +### 1. For String array |
| 264 | + |
| 265 | +We can use the following code snippet. |
| 266 | +```java |
| 267 | +Arrays.asList(yourArray).contains(yourValue); |
| 268 | +``` |
| 269 | +From Java 8 we can use Stream API to check if an array contains a value. |
| 270 | +```java |
| 271 | +String [] names = {"gaurav", "shubham", "krishna", "mayur"}; |
| 272 | +boolean stringPresentInArray = Arrays.stream(names).anyMatch("mayur"::equals);; |
| 273 | +``` |
| 274 | + |
| 275 | +### 2. For Primitive Array |
| 276 | + |
| 277 | +To check if a primitive array contains a value, we can use the simple for loop and add a equals condition in the loop. |
| 278 | + |
| 279 | +```java |
| 280 | +int[] numbers = {1, 2, 3, 4, 5, 6, 7}; |
| 281 | +boolean contains = false; |
| 282 | + |
| 283 | +for(int number: numbers) { |
| 284 | + if(number == 4) { |
| 285 | + contains = true; |
| 286 | + break; |
| 287 | + } |
| 288 | +} |
| 289 | +``` |
| 290 | +From Java 8, we can use classes like `IntStream`, `DoubleStream` or `LongStream` to check whether an array contains `int`, `double` or `long` value respectively. |
| 291 | +```java |
| 292 | +int[] numbers = {1, 2, 3, 4, 5, 6, 7}; |
| 293 | +boolean contains = IntStream.of(numbers).anyMatch(x -> x == 3); |
| 294 | +``` |
| 295 | + |
| 296 | +### Check using the `ArrayUtils.contains()` method of 'Apache common Lang' Library |
| 297 | + |
| 298 | +You can use the `ArrayUtils.contains()` method of Apache common Lang Library to check if an array contains a value. |
| 299 | + |
| 300 | +```java |
| 301 | +int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; |
| 302 | +boolean containsInt = ArrayUtils.contains(numbers, 7); |
| 303 | + |
| 304 | +String[] names = {"gaurav", "shubham", "krishna", "mayur"}; |
| 305 | +boolean containsString = ArrayUtils.contains(names, "gaurav"); |
| 306 | +``` |
| 307 | +I hope this article will help you to check if an array contains a value in Java. |
| 308 | + |
| 309 | +If you have any other ways to check check if an array contains a value or element using java programming language, please comment below. |
| 310 | + |
| 311 | +You can check my YouTube channel [here](https://www.youtube.com/channel/UCl31HHUdQbSHOQfc9L-wo3w) |
| 312 | + |
| 313 | +### Related Articles |
| 314 | + |
| 315 | +- [How To Get Current Timestamp In Java?](https://coderolls.com/how-to-get-current-timestamps-in-java/) |
| 316 | + |
| 317 | +- [How To Convert An Integer To String In Java](https://coderolls.com/convert-int-to-string/) |
| 318 | + |
| 319 | +- [How to convert String to Integer in Java](https://coderolls.com/convert-string-to-int/) |
| 320 | + |
| 321 | +- [How To Convert StringBuilder To String In Java?](https://coderolls.com/convert-stringbuilder-to-string-in-java/) |
| 322 | + |
| 323 | +- [How Do I Compare Strings In Java](https://coderolls.com/compare-strings-in-java/) |
| 324 | + |
| 325 | +- [How To Reverse A String In Java (5 ways)](https://coderolls.com/reverse-a-string-in-java/) |
| 326 | + |
| 327 | + |
0 commit comments