当前位置:网站首页>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);
}
}
边栏推荐
- Section 7 - structures
- Chapter 13 abstraction: address space
- Clause 47: please use traits classes to represent type information
- C language learning log 1.24
- Force buckle 25 A group of K flipped linked lists
- Reductive elimination
- Recursion and recursion
- JS to realize the conversion between string and array and an interview question
- Section 5 - Operator details
- Advanced C - Section 2 - pointers
猜你喜欢
C language learning log 12.5
Ruoyi cloud startup tutorial (hand-held graphics)
System file interface open
Section 7 - structures
Mind mapping series - Database
Avantages de win8.1 et win10
Embedded hardware: electronic components (1) resistance capacitance inductance
External sort
Section 3 - functions
Binary search and binary answer
随机推荐
Clause 30: be familiar with the failure of perfect forwarding
CMB's written test -- data analysis
QT signal is automatically associated with the slot
About mission planning and improving execution
Understanding inode
C language learning log 12.5
【线程/多线程】线程的执行顺序
[leetcode]- binary search
Explain the opencv function cv:: add() in detail, and attach sample code and running results of various cases
Ruoyi cloud startup tutorial (hand-held graphics)
Optocoupler working principle function electric parameter application circuit
MySQL8.0.13安装教程(有图)
Red Treasure Book Reading Notes (continuously updated)
17.6 unique_ Lock details
Force deduction 121 questions
Sort (internal sort) + external sort
Shell built-in string substitution
C language learning log 12.25
详解OpenCV的函数cv::add(),并附各种情况的示例代码和运行结果
System file interface open