1. Introduction
In this tutorial, We'll be learning how to check whether a value is present in the array using linear and Binary search.
Next, using java methods such as contains() and Stream API anyMatch() method with primitive and String values.
Examples:
Input: arr[] = [6, 7, 10, 5, 70, 9], input 5
Output: true
Input: arr[] = [0, 8, -9, 56, 8], input = -5
Output: false
Check if a value is present in an Array in Java
2. Linear Search
In a linear search approach, each value in the array is compared with the given element.
package com.javaprogramto.arrays.find; public class LinearSearch { public static void main(String[] args) { int[] array = {6, 7, 10, 5, 70, 9}; boolean found = false; int valueToBeFound = 5; for(int value : array){ if( value == valueToBeFound){ found = true; } } if(found){ System.out.println("value 5 is present"); } else { System.out.println("value 5 is not present"); } } }
Output:
value 5 is present
3. Arrays Binary Search
Binary Search is another approach to check the value is in the list or array.
But, the input array must be sorted otherwise this logic will not work.
Arrays API has a binarySearch() method that returns a boolean value. If the array contains a value then true is returned.
package com.javaprogramto.arrays.find; import java.util.Arrays; public class BinarySearch { public static void main(String[] args) { int[] array = {6, 7, 10, 15, 70, 90}; int found = -1; int valueToBeFound = 15; found = Arrays.binarySearch(array, valueToBeFound); if(found >= 0){ System.out.println("value 15 is present"); } else { System.out.println("value 15 is not present"); } String[] names = {"ram", "sita", "zuga"}; found = Arrays.binarySearch(names, "yama"); if(found >= 0){ System.out.println("String yama is present"); } else { System.out.println("String yama is not present"); } } }
Output:
value 15 is present
String yama is not present
4. Contains() To Determine Whether an Array Contains a Particular Value in Java
package com.javaprogramto.arrays.find; import java.util.Arrays; import java.util.List; public class ContainsExample { public static void main(String[] args) { Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9}; ListintList = Arrays.asList(values); if (intList.contains(7)) { System.out.println("7 is present in the values array"); } else { System.out.println("7 is not present in the values array"); } String[] fruites = {"banana", "jack fruit", "orange", "mango", "apple"}; List fruitesList = Arrays.asList(fruites); if (fruitesList.contains("apple")) { System.out.println("apple is present in the fruitesList array"); } else { System.out.println("apple is not present in the fruitesList array"); } } }
Output:
7 is present in the values array
apple is present in the fruitesList array
5. Java 8 anyMatch() to check array contains value or not
This is very easy to do in java 8 stream api. anyMatch() method is stream terminal operation.
First, Convert array into Stream and invoke anyMatch() method.
package com.javaprogramto.arrays.find; import java.util.Arrays; public class Java8AnyMatch { public static void main(String[] args) { Integer[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9}; boolean foundInt = Arrays.stream(values).anyMatch(value -> value == 6); if (foundInt) { System.out.println("6 is present in the values array using java 8 stream api using anyMatch() method"); } else { System.out.println("6 is not present in the values array using java 8 stream api using anyMatch() method"); } String[] fruites = {"banana", "jack fruit", "orange", "mango", "apple"}; boolean foundFruit = Arrays.stream(fruites).anyMatch(fruitName -> "apple".equalsIgnoreCase(fruitName)); if (foundFruit) { System.out.println("apple is present in the fruitesList array using java 8 stream api using anyMatch() method"); } else { System.out.println("apple is not present in the fruitesList array using java 8 stream api using anyMatch() method"); } } }
Output:
6 is present in the values array using java 8 stream api using anyMatch() method
apple is present in the fruitesList array using java 8 stream api using anyMatch() method
In the similar way, you can check if array element is null in java 8 anyMatch() method.
6. Java 8 IntStream and Double Stream to Check value in Array
package com.javaprogramto.arrays.find; import java.util.stream.DoubleStream; import java.util.stream.IntStream; public class IntDoubleStream { public static void main(String[] args) { int[] values = {1, 2, 3, 4, 5, 6, 7, 8, 9}; boolean foundInt = IntStream.of(values).anyMatch(value -> value == 6); if (foundInt) { System.out.println("value 8 check using IntStream anyMatch method."); } else { System.out.println("value 8 not found using IntStream"); } double[] decimals = {1.1, 2.2, 3.3, 4.4, 5.5}; boolean foundDouble = DoubleStream.of(decimals).anyMatch(value -> value == 5.5); if (foundDouble) { System.out.println("value 5.5 check using DoubleStream anyMatch method."); } else { System.out.println("value 5.5 not found using DoubleStream"); } } }
Output:
value 8 check using IntStream anyMatch method.
value 8 check using DoubleStream anyMatch method.
7. Conclusion
In this article, you've seen how to check value is present in the array or not. Various solutions are shown using the traditional approach and Java Streams concepts.
Read more on :
Java 8 Stream API
Convert Stream to IntStream
Ref
Mkyong
As usual, all programs shown are over GitHub.
Read more on :
Java 8 Stream API
Convert Stream to IntStream
Ref
Mkyong
As usual, all programs shown are over GitHub.
No comments:
Post a Comment
Please do not add any spam links in the comments section.