Count occurrence of a given character in a string using Stream API in Java


Introduction

In this tutorial, we will implement an approach that counts how many times a particular character appears in a string using the Stream API in Java. A string is a collection of characters and we will use a String class method to separate string characters.

We will take an input string and define a character we want to count. The functions used in this approach are the chars() method of the String class and the filter() method of the Stream interface.

  • char() = It is a String class instance method. It returns the intstream values. The stream contains the characters code point values in the string.

  • filter() = it is used to consider the predicted values. It helps in selecting the required stream of values.

Stream API in Java was introduced in Java 8 which is a collection of classes and objects. This method simplifies the object processing process and enhances the readability of the code. It helps Java coders by providing flexibility in handling complex tasks using its various methods and pipeline process.

Example 1

String = “tutorialspoint”
Character = t

Output

the occurrence of the character “t” in the input string is 4 times.

In the above example, the input string is “tutorialspoint”. The task is to count how many times the “t” character is present in the input string. As we can see, “t” appears 4 times in the string.

Example 2

String = “Helloworld”
Character = “e”

Output

= 1

In the above example, the input string is “Helloworld” and the character is “e”. The task is to count the repetition of character “e” in the string. The “e” appears only 1 time in the input string.

  • chars().filter() − It is a String class library function which returns a stream of character values of the argument string. The filter() method along with it restricts the unwanted stream of chars.

string_name.chars().filter()
  • count() − It counts the stream characters and returns their number. After calling the count() method of the Java Stream interface, access to the stream is not possible.

long count() 

Algorithm

  • Take an input string.

  • Define the desired character and find its occurrence in the input string.

  • Use the Stream API filter() method to remove unmatched characters.

  • Use the chars() method of the String class to convert the string into characters.

  • Print the output.

Logic 1 Example

We will implement the above example in Java for the input string “tutorialspoint” and count the occurrences of the character "t" in it. To implement this example, we will use some library functions that are listed as −

import java.util.stream.*;
  
public class GFG {
   // Method that returns the count of the given
   // character in the string
   public static long count(String st, char chr){
      return st.chars().filter(ch -> ch == chr).count();
   }
  
   // Driver method
   public static void main(String args[]){
      String st = "tutorialspoint";
      char ch = 't';
      System.out.println("The occurrence of t in the string is:");
      System.out.println(count(st, ch));
   }
}

Output

The occurrence of t in the string is:
3

Logic 2 Example

import java.util.stream.IntStream;
public class CharacterCounter{
   public static void main(String[] args) {
      String s = "Hello, World!";
      char c = 'o';

      long cnt = countOccurrences(s, c);
      System.out.println("Occurrences of '" + c + "': " + cnt);
   }
   public static long countOccurrences(String s, char c){
      return s.chars().filter(ch -> ch == c).count();
   }
}

Output

Occurrence of ‘o’ : 2

Conclusion

In this tutorial, we learn about streaming in Java. We developed codes and two logics to find a particular character in an input string. We used the chars() and filter() methods to implement examples to find the occurrence of a particular character in a given string.

The Stream class's filter() method filters out unwanted characters from the input string. The chars() method of the String class divides the input string into characters.

Updated on: 31-Jul-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements