Java 8 - Count Duplicate Characters in a String

In this short article, we will write a Java program to count duplicate characters in a given String.
We will use Java 8 lambda expression and stream API to write this program.
Learn Java programming at https://www.javaguides.net/p/java-tutorial-learn-java-programming.html.
Learn Java 8 at https://www.javaguides.net/p/java-8.html.

Java 8 - Count Duplicate Characters from a String

package com.java.tutorials.programs;

import java.util.Map;

import java.util.stream.Collectors;

/**
 * Java 8 - Count Duplicate Characters from a String
 * 
 * @author RAMESH
 *
 */
public class CountDuplicateCharsJava8 {

    public static void main(String[] args) {

        // given input string
        String input = "JavaJavaEE";

        // convert string into stream
        Map < Character, Long > result = input
            .chars().mapToObj(c - > (char) c)
            .collect(Collectors.groupingBy(c - > c, Collectors.counting()));

        result.forEach((k, v) - > {
            if (v > 1) {
                System.out.println(k + " : " + v);
            }
        });
    }
}
Output:
a : 4
E : 2
v : 2
J : 2
Let's rewrite the above program without using Java 8 features.

Java program that counts duplicate characters from a given string (without Java 8)

package com.java.tutorials.programs;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class CountDuplicateChars {

    public static void main(String[] args) {

        // given input string
        String input = "JavaJavaEE";

        // create a HashMap
        Map < Character, Integer > countDuplicateMap = new HashMap < > ();

        // Converts this string to a new character array.
        char[] charArray = input.toCharArray();

        for (char c: charArray) {

            if (countDuplicateMap.containsKey(c)) {

                // get value by key and increment it's value by a
                countDuplicateMap.put(c, countDuplicateMap.get(c) + 1);
            } else {
                countDuplicateMap.put(c, 1);
            }
        }


        for (Entry < Character, Integer > entry: countDuplicateMap.entrySet()) {

            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + " : " + entry.getValue());
            }
        }
    }
}
Output:
a : 4
E : 2
v : 2
J : 2

Related Java programs

  1. Java Program to Count Number of Duplicate Words in String
  2. Java Program to Count Number of Words in Given String
  3. Java Program to Count the Number of Occurrences of Substring in a String
  4. Java Program to Count the Occurrences of Each Character in String
  5. Java Program to Merge two String Arrays  
  6. Java Program to Remove Duplicate Words from String 
  7. Java Program to Reverse a String(5 ways)
  8. Java Program to Reverse Each Word of a String    
  9. Java Program to Swap Two Strings      
  10. How to Check if the String Contains only Digits         
  11. How to Check if the String Contains only Letters  
  12. How to Check If the String Contains Only Letters or Digits 
  13. Java Program to Check if Input String is Palindrome 
  14. Java Program to Find all Permutations of String
  15. How to Remove or Trim All White Spaces from a String in Java
  16. How to Remove Leading and Trailing White Space From a String in Java
  17. Java Program to Count Duplicate Characters in a String
  18. Remove Character from String in Java (Java 8)
  19. Java Program to Count Vowels and Consonants in a String (Java 8)
  20. 4 Ways to Find First Non-Repeated Character in String in Java
  21. Java Program to Remove Duplicate Elements in an Array
  22. Java Program to Find Largest Element in an Array
  23. Java Program to Reverse an Array Without Using Another Array
  24. Java Program to Check the Equality of Two Arrays
  25. Java Program to Check Armstrong Number
  26. Java program to check prime number
  27. Java Program to Swap Two Numbers
  28. Java Program to Find Factorial of a Number
  29. Java Program to Reverse a Number
  30. Java Program to Swap Two Strings Without Using Third Variable 

Comments