Count occurrence of a given character in a string using Stream API in Java Last Updated : 29 May, 2019 Comments Improve Suggest changes 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 Count occurrence of a given character in a string using Stream API in Java C code_r Follow Improve Article Tags : Strings Java DSA Java-Stream-programs Practice Tags : JavaStrings Similar Reads Count Occurrences of a Given Character in a String Given a string S and a character 'c', the task is to count the occurrence of the given character in the string.Examples: Input : S = "geeksforgeeks" and c = 'e'Output : 4Explanation: 'e' appears four times in str.Input : S = "abccdefgaa" and c = 'a' Output : 3Explanation: 'a' appears three times in 6 min read Count occurrences of a character in a repeated string Given an integer N and a lowercase string. The string is repeated infinitely. The task is to find the No. of occurrences of a given character x in first N letters.Examples: Input : N = 10 str = "abcac"Output : 4Explanation: "abcacabcac" is the substring from the infinitely repeated string. In first 8 min read Count the number of unique characters in a given String Given a string, str consisting of lowercase English alphabets, the task is to find the number of unique characters present in the string. Examples: Input: str = âgeeksforgeeksâOutput: 7Explanation: The given string âgeeksforgeeksâ contains 7 unique characters {âgâ, âeâ, âkâ, âsâ, âfâ, âoâ, ârâ}. Inp 14 min read Count of camel case characters present in a given string Given a string S, the task is to count the number of camel case characters present in the given string. The camel case character is defined as the number of uppercase characters in the given string. Examples: Input: S = "ckjkUUYII"Output: 5Explanation: Camel case characters present are U, U, Y, I an 7 min read Count strings from given array having all characters appearing in a given string Given an array of strings arr[][] of size N and a string S, the task is to find the number of strings from the array having all its characters appearing in the string S. Examples: Input: arr[][] = {"ab", "aab", "abaaaa", "bbd"}, S = "ab"Output: 3Explanation: String "ab" have all the characters occur 6 min read Count of substrings formed using a given set of characters only Given a string str and an array arr[] of K characters, the task is to find the number of substrings of str that contain characters only from the given character array arr[]. Note: The string str and the arr[] contain only lowercase alphabets. Examples: Input: S = "abcb", K = 2, charArray[] = {'a', ' 8 min read Count occurrences of a sub-string with one variable character Given two strings a and b, and an integer k which is the index in b at which the character can be changed to any other character, the task is to check if b is a sub-string in a and print out how many times b occurs in a in total after replacing the b[k] with every possible lowercase character of Eng 5 min read Total length of string from given Array of strings composed using given characters Given a list of characters and an array of strings, find the total length of all strings in the array of strings that can be composed using the given characters.Examples: Input: string = ["mouse", "me", "bat", "lion"], chars = "eusamotb" Output: 10 Explanation: The strings that can be formed using t 6 min read Printing frequency of each character just after its consecutive occurrences Given a string in such a way that every character occurs in a repeated manner. Your task is to print the string by inserting the frequency of each unique character after it and also eliminating all repeated characters. Examples: Input: GeeeEEKKKssOutput: G1e3E2K3s2 Input: ccccOddEEEOutput: c4O1d2E3 3 min read 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 Like