Created
May 17, 2021 15:22
-
-
Save gauravkukade/3df7496b88b2222ab34d685f5287e725 to your computer and use it in GitHub Desktop.
A Java program that checks if a String array contains a particular string value . 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
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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment