Given a string and a character, the task is to make a function which counts the occurrence of the given character in the string using Stream API.
Input: str = "geeksforgeeks", c = 'e' Output: 4 'e' appears four times in str. Input: str = "abccdefgaa", c = 'a' Output: 3 'a' appears three times in str.
- Convert the string into character stream
- Check if the character in the stream is the character to be counted using filter() function.
- Count the matched characters using the count() function
Below is the implementation of the above approach:
Related Article: Program to count occurrence of a given character in a string
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- Character Stream Vs Byte Stream in Java
- Program to count occurrence of a given character in a string
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Find the Nth occurrence of a character in the given String
- Remove the first and last occurrence of a given Character from a String
- Generate two output strings depending upon occurrence of character in input string.
- Generate two output strings depending upon occurrence of character in input string in Python
- Minimize the length of string by removing occurrence of only one character
- Difference between Stream.of() and Arrays.stream() method in Java
- Rearrange given string to maximize the occurrence of string t
- foreach() loop vs Stream foreach() vs Parallel Stream foreach()
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Longest suffix such that occurrence of each character is less than N after deleting atmost K characters
- Replace all occurrences of character X with character Y in given string
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Count substrings that starts with character X and ends with character Y
- Find the count of M character words which have at least one character repeated
- Find the first non-repeating character from a stream of characters
- Queue based approach for first non-repeating character in a stream
- Convert Java Object to Json String using Jackson API
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.