Count occurrence of a given character in a string using Stream API in Java Last Updated : 29 May, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Examples: 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. Approach: 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: Java // Java program to count occurrences // of a character using Stream import java.util.stream.*; class GFG { // Method that returns the count of the given // character in the string public static long count(String s, char ch) { return s.chars() .filter(c -> c == ch) .count(); } // Driver method public static void main(String args[]) { String str = "geeksforgeeks"; char c = 'e'; System.out.println(count(str, c)); } } Output: 4 Related Article: Program to count occurrence of a given character in a string Comment More infoAdvertise with us Next Article LongStream count() in Java with examples C code_r Follow Improve Article Tags : Java Java-Stream-programs Practice Tags : Java Similar Reads Java Program to Get a Character from a String Given a String str, the task is to get a specific character from that String at a specific index. Examples:Input: str = "Geeks", index = 2Output: eInput: str = "GeeksForGeeks", index = 5Output: F Below are various ways to do so: Using String.charAt() method: Get the string and the indexGet the speci 5 min read Java Program to Count the Number of Lines, Words, Characters, and Paragraphs in a Text File Counting the number of characters is essential because almost all the text boxes that rely on user input have certain limitations on the number of characters inserted. For example, the character limit on a Facebook post is 63206 characters. Whereas for a tweet on Twitter, the character limit is 140 3 min read Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read Count the pairs of vowels in the given string Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.Examples: Input: str = "abaebio" Output: 2 (a, e) and (i, o) are the only valid pairs.Input: str = "aeoui" Output: 4 Approach: Starting from the first character of the string to 5 min read LongStream count() in Java with examples LongStream count() returns the count of elements in the stream. LongStream count() is present in java.util.stream.LongStream Syntax : long count() Example 1 : Count the elements in LongStream. Java // Java code for LongStream count() // to count the number of elements in // given stream import java. 2 min read StringBuffer codePointCount() method in Java with Examples The codePointCount() method of StringBuffer class is used to return the number of Unicode code points in the specified range of beginIndex to endIndex of String contained by StringBuffer. This method takes beginIndex and endIndex as a parameter where beginIndex is the index of the first character of 3 min read Like