Java案例使用集合方法實現(xiàn)統(tǒng)計任意字符串中字符出現(xiàn)的次數
更新時間:2022年04月01日 14:30:02 作者:再美不及姑娘你
這篇文章主要介紹了Java案例使用集合方法實現(xiàn)統(tǒng)計任意字符串中字符出現(xiàn)的次數,下面我們將用兩種方法實現(xiàn),需要的小伙伴可以參考一下文章具體內容
需求:鍵盤錄入一個字符串,統(tǒng)計其中各個字符出現(xiàn)的順序
分析:
- 1.使用
Scanner類獲取一個字符串 - 2.創(chuàng)建
HashMap集合,如果追求統(tǒng)計字符的美觀性,可以使用TreeMap - 3.遍歷字符串得到每一個字符并將其作為TreeMap的鍵
- 4.通過鍵去集合中找相對應的值,看返回值是什么?
返回null:說明該字符在集合中不存在,就將該字符為鍵,次數1為值進行存儲
返回的不是null:說明該字符在集合中存在,就再次將該字符作為鍵,次數+1為值進行存儲
- 5.遍歷集合
public class TreeMapDemo {
? public static void main(String[] args) {
? ? ? //創(chuàng)建TreeMap集合對象
? ? ? TreeMap<Character,Integer>tm=new TreeMap<Character,Integer>();
? ? ? //創(chuàng)建Scanner對象
?
? ? ? Scanner sc=new Scanner(System.in);
? ? ? System.out.println("請輸入一個字符串");
? ? ? String s=sc.nextLine();
? ? ? //遍歷字符串
? ? ? for (int i=0;i<s.length();i++){
? ? ? ? ? //獲取每一個字符
? ? ? ? ? char key =s.charAt(i);
? ? ? ? ? //拿到的每一個字符到集合中找相對應的值
? ? ? ? ? Integer value=tm.get(key);
? ? ? ? ? //判斷返回值
? ? ? ? ? if (value==null){
? ? ? ? ? ? ? tm.put(key,1);
? ? ? ? ? }else {
? ? ? ? ? ? ? value++;
? ? ? ? ? ? ? tm.put(key,value);
? ? ? ? ? }
? ? ? }
? ? ? //遍歷集合并拼接
? ? ? StringBuilder sb=new StringBuilder();
? ? ? Set<Character> keySet=tm.keySet();
? ? ? for (char key :keySet){
? ? ? ? ? Integer value=tm.get(key);
? ? ? ? ? sb.append(key).append("(").append(value).append(")");
?
? ? ? }
? ? ? String result = sb.toString();
? ? ? System.out.println(result);
? }
}
?補充:
import java.util.HashMap;
import java.util.Scanner;
import java.util.Set;
public class MapTest {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//鍵盤錄入字符串
System.out.println("請輸入一串字符");
//創(chuàng)建字符串對象,存儲鍵盤輸入的值
String line = scanner.nextLine();
//創(chuàng)建HashMap集合,鍵是character,值是Inter
HashMap<Character, Integer> hm = new HashMap<Character, Integer>();
//循環(huán)遍歷字符串,得到每個字符
for (int i = 0; i < line.length(); i++) {
//通過charAt方法,將字符串的索引值,作為鍵添加到HashMap集合中
char key = line.charAt(i);
//使用HashMap集合的get方法,對集合中的鍵(字符)進行判斷
//創(chuàng)建Integer對象存儲值
Integer value = hm.get(key);
//返回值為空,將字符作為鍵,1作為存儲值
if (value == null) {
hm.put(key, 1);
} else {
//返回值不為空,說明此字符在集合中出現(xiàn)過,將字符作為鍵,值加一
// 重新存儲鍵(字符)和對應的值
value++;
hm.put(key, value);
}
}
//創(chuàng)建StringBuilder字符串序列
StringBuilder sb = new StringBuilder();
//創(chuàng)建set集合對象,通過keyset方法獲取鍵
Set<Character> keyset = hm.keySet();
//增強for循環(huán)遍歷,獲取鍵所對應的值
for (Character key : keyset) {
//使用get方法獲取到鍵所對應的值
Integer value = hm.get(key);
//對序列進行拼接,使用append方法
sb.append(key).append("(").append(value).append(")");
}
// 通過tostring方法返回StringBuilder此順序中的數據的字符串,創(chuàng)建string對象接受此字符串
String result = sb.toString();
//打印結果
System.out.println(result);
}
}到此這篇關于Java案例使用集合方法實現(xiàn)統(tǒng)計任意字符串中字符出現(xiàn)的次數的文章就介紹到這了,更多相關Java集合統(tǒng)字符出現(xiàn)次數內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關文章
Springcould多模塊搭建Eureka服務器端口過程詳解
這篇文章主要介紹了Springcould多模塊搭建Eureka服務器端口過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-11-11
Java?DelayQueue實現(xiàn)任務延時示例講解
DelayQueue是一個無界的BlockingQueue的實現(xiàn)類,用于放置實現(xiàn)了Delayed接口的對象,其中的對象只能在其到期時才能從隊列中取走。本文就來利用DelayQueue實現(xiàn)延時任務,感興趣的可以了解一下2022-09-09
使用Java將DOCX文檔解析為Markdown文檔的代碼實現(xiàn)
在現(xiàn)代文檔處理中,Markdown(MD)因其簡潔的語法和良好的可讀性,逐漸成為開發(fā)者、技術寫作者和內容創(chuàng)作者的首選格式,然而,許多文檔仍然以Microsoft Word的DOCX格式保存,本文將介紹如何使用Java和相關庫將DOCX文檔解析為Markdown文檔,需要的朋友可以參考下2025-04-04

