当前位置:网站首页>The map function counts the number of occurrences of characters
The map function counts the number of occurrences of characters
2022-07-26 12:17:00 【Wang Xiaoya】
stem :
Gets the number of occurrences of each character on the custom text
Tips : Traverse every character of the text ; The characters and the number of occurrences are saved in Map in ; take Map Write data to file
Included :aabbccddwab ----->a3b3c2d2w1
analysis :
- We can divide the results into several parts : a(5),b(4),c(3),d(2),e(1)
- Each part can be regarded as : Characters and the number of times corresponding to characters
- Data like this , We can go through HashMap Collection to store , Keys are characters , The value is the number of times the character appears
Be careful : key yes character , The type should be Character; value Characters appear frequency , The type should be Integer
Ideas :
- Enter a string on the keyboard
- establish HashMap aggregate , The key is Character, The value is Integer
- Traversal string , Get every character
- Take every character you get as a key to HashMap Find the corresponding value in the set , Look at the return value
If the return value is null: Indicates that the character is in HashMap There is no such thing as , Just use this character as a key ,1 Store as a value
If the return value is not null: Indicates that the character is in HashMap Exists in collection , Add this value to 1, Then re store the character and the corresponding value
- Traverse HashMap aggregate , Get health and value , Splice as required
- Output splicing results

Abominable ! It's a mistake , Null pointer exception NullPointerException
We found the reason , It turned out that there was no else sentence ( I should have missed it when I was writing notes , I thought I wrote , But I didn't :D)
After the change
Complete code :
package com.B.IOStream_14.IODemo;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
//2. Gets the number of occurrences of each character on the custom text
// Tips : Traverse every character of the text ; The characters and the number of occurrences are saved in Map in ; take Map Write data to file
// Included :aabbccddwab ----->a3b3c2d2w1
public class A2Map {
private static Object Character;
public static void main(String[] args) {
// Keyboard input string
Scanner sc = new Scanner(System.in);
System.out.println(" Please enter the string :");
String line = sc.nextLine();
// establish TreeMap aggregate ( Orderly , If it is HashMap It's out of order ), The key is Character, The value is Integer
TreeMap<Character, Integer> map = new TreeMap<Character, Integer>();
// Traversal string
for (int i = 0; i < line.length(); i++) {
char key = line.charAt(i); // Use character as key
// Take every character you get as a key to TreeMap Find the corresponding value in the set , Look at the return value
Integer value = map.get(key);
if (value == null) {
// If the return value is null: Indicates that the character is in TreeMap There is no such thing as , Just use this character as a key ,1 Store as a value
map.put(key, 1);
}else {
// If the return value is not null: Indicates that the character is in TreeMap Exists in collection , Add this value to 1, Then re store the character and the corresponding value
value++;
map.put(key, value);
}
}
// Traverse TreeMap aggregate , And we get the bond and the value , Splice as required
StringBuilder sb = new StringBuilder();
// Set Make sure that the keys are not repeated
Set<Character> KeySet = map.keySet();
// iterator
for (Character key : KeySet) {
Integer value = map.get(key);
sb.append(key).append(value);
}
String result = sb.toString();
// Output
System.out.println(result);
}
}
result :
Please enter the string :
wsgdgagdgdg
a1d3g5s1w1
Process ended , Exit code 0边栏推荐
- Pytorch深度学习快速入门教程 -- 土堆教程笔记(一)
- El form displays two columns per row, with the bottom button centered
- pytest接口自动化测试框架 | 通过标记表达式执行用例
- pytest接口自动化测试框架 | fixture调用fixture
- Implementation of dynamic and static libraries (packaging dynamic and static libraries for others to use)
- transformer一统天下?depth-wise conv有话要说
- 【Mysql约束】
- HTAP是有代价的
- y9000p2022重装win10问题
- Pytest interface automation test framework | pytest configuration file
猜你喜欢

SSJ-21B时间继电器

X.509、PKCS文件格式介绍

Map函数统计字符出现的次数

Real time synchronization and conversion of massive data based on Flink CDC

FPGA入门学习(一) - 第一个FPGA工程

【2243】module_param.m
![[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!](/img/a0/4910970a089cab198875944c7ae88c.png)
[countdown 10 days] Tencent cloud audio and video special is about to meet, and the thousand yuan prize is waiting for you!

海外APP推送(下篇):海外厂商通道集成指南

Audio and video+

详解勒让德变换与共轭函数
随机推荐
Ssj-21b time relay
空洞卷积详解(输入输出大小分析)
Pytest interface automation test framework | execute use cases through markup expressions
[Anhui University] information sharing of postgraduate entrance examination and re examination
Here blog: running a large language model in a production environment - overview of the reasoning framework
Ds-112 time relay
Pytoch deep learning quick start tutorial -- mound tutorial notes (II)
How do children's playgrounds operate?
Digital intelligence transformation, management first | jnpf strives to build a "full life cycle management" platform
el-form 每行显示两列,底部按钮居中
y9000p2022重装win10问题
Jsj-3/ac220v time relay
Introduction to FPGA (II) - one out of two selector
File类的学习过程中出现的问题及解决方法
What is per title encoding?
自定义浏览器默认右击菜单栏
详解勒让德变换与共轭函数
Audio and video technology development weekly | 255
pytest接口自动化测试框架 | fixture调用fixture
pytest接口自动化测试框架 | 通过标记表达式执行用例