
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java program to count the occurrence of each character in a string using Hashmap
To count the occurrence of each character in a string using Hashmap, the Java code is as follows −
Example
import java.io.*; import java.util.*; public class Demo{ static void count_characters(String input_str){ HashMap<Character, Integer> my_map = new HashMap<Character, Integer>(); char[] str_array = input_str.toCharArray(); for (char c : str_array){ if (my_map.containsKey(c)){ my_map.put(c, my_map.get(c) + 1); }else{ my_map.put(c, 1); } } for (Map.Entry entry : my_map.entrySet()){ System.out.println(entry.getKey() + " " + entry.getValue()); } } public static void main(String[] args){ String my_str = "Joe Erien "; System.out.println("The occurence of every character in the string is "); count_characters(my_str); } }
Output
The occurence of every character in the string is 2 r 1 e 2 E 1 i 1 J 1 n 1 o 1
A class named Demo contains the function named ‘count_characters’. Here, a hashmap is created that will store the character and its count. This function iterates through the string and checks for the count of every character. In the main function, the string is defined, and the function is called on this string and relevant message is displayed on the console.
- Related Articles
- Java program to check occurrence of each character in String
- Python program to find occurrence to each character in given string
- Java program to count the occurrences of each character
- Java program to check occurrence of each vowel in String
- C Program to find minimum occurrence of character in a string
- C Program to find maximum occurrence of character in a string
- Finding the last occurrence of a character in a String in Java
- C program to replace all occurrence of a character in a string
- C# Program to find number of occurrence of a character in a String
- Java Program to Iterate through each character of the string.
- Java program to check occurence of each character in String
- Java Program to Capitalize the first character of each word in a String
- C# program to count the occurrences of each character
- Write a C Program to count the frequency of each character
- String function to replace nth occurrence of a character in a string JavaScript

Advertisements