In this tutorial, we will see “How to check if ‘VOWEL’ is present in a String using Java 8?” check vowel in string using java 8
Java 8 – How to check if VOWEL is present in a String? – Simplest Example Share on X
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Check if 'Vowel' is present in a String using Java 8 * @author Deepak Verma * */ import java.util.stream.IntStream; public class Check_Vowel_in_String_Java8_Example { public static void main(String[] args) { String s = "Hello World"; boolean hasVowel = IntStream.range(0, s.length()) .mapToObj(s::charAt) .anyMatch(c -> "aeiouAEIOU".indexOf(c) >= 0); System.out.println(hasVowel ? "Yes" : "No"); } } |
Output:
1 | Yes |
check vowel in string java 8
In this example, IntStream.range
method is used to create a stream of integers from 0 to s.length() - 1
, the mapToObj
method to map each integer to the corresponding character in the string, and the anyMatch
method is there to check if any of the characters are vowels. and Finally, the indexOf
method is used to check if a character is a vowel.
skip method java 8 stream
Java 8 – How to check if VOWEL is present in a String? – Simplest Example Share on X
Do you like this Post? – then check my other helpful posts:
- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8
Other Useful References: