Javatpoint Logo
Javatpoint Logo

Distinct Character Count Java Stream

In the world of Java programming, there are numerous scenarios where you might need to count the distinct characters in a given string. Whether we are working on a text analysis tool, a word game, or any application that deals with text data, knowing how to calculate the distinct character count efficiently is a valuable skill. Java Streams provide an elegant and concise way to achieve this. In this section, we will explore different methods for counting distinct characters in a string using Java Streams, complete with code examples and detailed explanations.

Method 1: Using Streams and Sets

One of the simplest and most efficient ways to count distinct characters in a string is by leveraging Java Streams and a Set. Here's a step-by-step implementation:

DistinctCharacterCountExample.java

Output:

Distinct Character Count: 9

Explanation

We convert the input string into a stream of characters using inputString.chars(). Then, we map each character's Unicode value back to a char to obtain a stream of characters using .mapToObj(c -> (char) c). We collect these characters into a Set using Collectors.toSet(), ensuring that only distinct characters are retained. Finally, we calculate the size of the Set, which gives us the count of distinct characters.

Method 2: Using Streams with Distinct and Count

Java Streams provide a convenient distinct method to filter out duplicate elements. We can use this method in combination with count to calculate the distinct character count.

DistinctCharacterCountExample.java

Output:

Distinct Character Count: 9

Explanation

We first convert the input string to a character stream and then map it to a stream of Character objects. We apply the distinct method to keep unique characters in the stream. Finally, we use the count method to get the count of distinct characters.

Method 3: Using Collectors.toCollection() with a LinkedHashSet

Another approach is to use the Collectors.toCollection() method with a LinkedHashSet, which maintains the order of insertion while ensuring distinct elements.

DistinctCharacterCountExample.java

Output:

Distinct Character Count: 9

Explanation

We convert the input string to a character stream and map it to a stream of Character objects. We use Collectors.toCollection(LinkedHashSet::new) to collect the distinct characters. It automatically maintains the insertion order. We obtain the count by getting the size of the LinkedHashSet.

Method 3: Counting Distinct Characters with Collectors.groupingBy() Method

DistinctCharacterCountExample.java

Output:

Distinct Character Count: 11

Explanation

Convert the input string to a character stream and then map it to a stream of Character objects. Use Collectors.groupingBy() to group the characters based on their identity (i.e., the character itself) and count them using Collectors.counting(). It results in a map where the keys are distinct characters, and the values are the counts of each character. Finally, we calculate the size of the map to get the count of distinct characters.

Conclusion

Counting distinct characters in a string is a common task in Java programming, and Java Streams provide elegant solutions to tackle this problem efficiently. The methods presented here allows us to count distinct characters in a concise and readable manner, making your code more maintainable and expressive. Whether you prefer using Set, distinct, or LinkedHashSet, we have multiple options to choose from, each offering distinct advantages in different scenarios.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA