当前位置:网站首页>Case - count the number of occurrences of each string in the string
Case - count the number of occurrences of each string in the string
2022-06-13 05:05:00 【Jason_ LH1024】


package com.it.code01;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class HashMapDemo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter a string :");
String line = sc.nextLine();
// establish HashMap aggregate , The key is Character, The value is Integer
// HashMap<Character, Integer> hm = new HashMap<>();
TreeMap<Character, Integer> hm = new TreeMap<>();
// Traversal string , Get every character
for (int i = 0; i < line.length(); i++) {
char key = line.charAt(i);
// Take each string you get as a key to HashMap Find the corresponding value in the set , Look at the return value
Integer value = hm.get(key);
if (value == null) {
// If the return value is null, Indicates that the character is in HashMap There is no such thing as , Just
hm.put(key, 1);
} else {
// If the return value is null, Description the string is in the HashMap There is no such thing as ,
// Just use this character as a key ,1 As a stored value
value++;
hm.put(key, value);
}
}
// Traverse HashMap aggregate , And we get the bond and the value , Splice as required
StringBuilder sb = new StringBuilder();
Set<Character> keySet = hm.keySet();
for (Character key : keySet) {
Integer value = hm.get(key);
sb.append(key).append("(").append(value).append(")");
}
String result = sb.toString();
// Output results
System.out.println(result);
}
}


边栏推荐
- [multi thread programming] the future interface obtains thread execution result data
- The games that you've tasted
- 135. distribute candy
- Modification and analysis of libcoap source code by Hongmeng device discovery module
- Simple sr: Best Buddy Gans for highly detailed image super resolution Paper Analysis
- C language exercise 1
- Clause 30: be familiar with the failure of perfect forwarding
- C language learning log 1.22
- Pycharm错误解决:Process finished with exit code -1073741819 (0xC0000005)
- Listiterator list iterator
猜你喜欢

How to lay copper in AD (aluminum designer)

System file interface open

CMB written test graphical reasoning

Simple greedy strategy

PostgreSQL Guide: inside exploration (Chapter 10 basic backup and point in time recovery) - Notes

Advanced C language - Section 1 - data storage

Section 6 - pointers

Dup2 use

The games that you've tasted

Sort (internal sort) + external sort
随机推荐
C language learning log 10.4
Regular expressions in QT
Brick story
RT thread console device initialization
Clause 47: please use traits classes to represent type information
QT using layout manager is invalid or abnormal
【多线程】线程池核心类-ThreadPoolExecutor
Std:: Map empty example
Std:: map insert details
RTSP streaming using easydarwin+ffmpeg
关于匿名内部类
Search DFS and BFS
Several methods of identifying equivalent circuit of circuit drawing
Std:: Map initialization
Bm1z002fj-evk-001 startup evaluation
On switch() case statement in C language
lookup
Article 49: understanding the behavior of new handler
Section 5 - Operator details
Sort (internal sort) + external sort