In this tutorial we will go over how to get distinct characters with their count in a String?
We will use 3 strings:
- crunchify
- crunchify.com
- Best Tech Blog – crunchify
This program will iterate over all characters and counts the number of times character appears.
Here is a complete program
JavaDistinctChar.java
package crunchify.java.tutorials; import java.util.HashMap; import java.util.List; import java.util.Map; /** * * @author https://crunchify.com * In Java How to get distinct characters with their count in a String? * */ public class JavaDistinctChar { public static void main(String[] args) { crunchifyPrintDistinctChar("crunchify"); crunchifyPrintDistinctChar("crunchify.com"); crunchifyPrintDistinctChar("Best Tech Blog - crunchify"); } private static void crunchifyPrintDistinctChar(String input) { // Map: An object that maps keys to values. // A map cannot contain duplicate keys; each key can map to at most one value. Map<Character, Integer> crunchifyCountMap = new HashMap<>(); for (char c : input.toCharArray()) // merge(): If the specified key is not already associated with a value or is associated with null, // associates it with the given non-null value. // Otherwise, replaces the associated value with the results of the given remapping function, or removes if the result is null. crunchifyCountMap.merge(c, 1, Integer::sum); crunchifyPrint(crunchifyCountMap); // mapToObj(): Returns an object-valued Stream consisting of the results of applying the given function to the elements of this stream. List<Character> list = input.chars().mapToObj(c -> (char) c).toList(); // forEach(): Performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception. // Actions are performed in the order of iteration, if that order is specified. Exceptions thrown by the action are relayed to the caller. // The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, // unless an overriding class has specified a concurrent modification policy. list.forEach(c -> crunchifyCountMap.merge(c, 1, Integer::sum)); crunchifyPrint(crunchifyCountMap); } // Simple Print Method private static void crunchifyPrint(Map<Character, Integer> crunchifyCountMap) { System.out.println(crunchifyCountMap); } }
Just run above program as Java Application and you will see result as below.
{r=1, c=2, u=1, f=1, h=1, y=1, i=1, n=1} {r=2, c=4, u=2, f=2, h=2, y=2, i=2, n=2} {r=1, c=3, u=1, f=1, h=1, y=1, i=1, m=1, .=1, n=1, o=1} {r=2, c=6, u=2, f=2, h=2, y=2, i=2, m=2, .=2, n=2, o=2} { =4, B=2, c=3, e=2, f=1, g=1, h=2, i=1, l=1, -=1, n=1, o=1, r=1, s=1, T=1, t=1, u=1, y=1} { =8, B=4, c=6, e=4, f=2, g=2, h=4, i=2, l=2, -=2, n=2, o=2, r=2, s=2, T=2, t=2, u=2, y=2} Process finished with exit code 0
Let me know if you have any question running this.
The post In Java How to get distinct characters with their count in a String? appeared first on Crunchify.
0 Commentaires