Java利用Map實現(xiàn)計算文本中字符個數(shù)
一、題目要求
有一個英文的文本文檔a.txt , 需要讀取該文本文檔并計算文檔中英文字符出現(xiàn)的次數(shù),最后打印輸出
注意:map集合只能放入英文字符,不能夠有空格數(shù)字引號等字符,所以在寫代碼的時候需要額外的進(jìn)行判斷,還需要考慮的一點是英文字符是有大小寫的,但是統(tǒng)計在map里面就不區(qū)分大小寫
二、分析
1、需要先獲取文檔的路徑,并創(chuàng)建HashMap作為存放次數(shù)的容器,key的類型為Character存放字符,value的類型為Integer存放次數(shù)
2、通過文檔輸入流讀取文檔中的內(nèi)容,得到一串字符串
3、遍歷字符串,獲得字符串中每一個字符
4、判斷字符在map集合中是否存在
5、不存在則將其放入
6、存在則將map集合中字符key對應(yīng)的次數(shù)value取出,然后加一,再通過put方法重新放回,起到更新次數(shù)的效果
三、部分代碼展示
1、創(chuàng)建Map列表及文件路徑
FileInputStream fis = null; HashMap<Character, Integer> map = new HashMap<>();
2、獲得文本中的內(nèi)容并返回為字符串
while ((len = fis.read(b))!=-1){ // 獲得文件中的字符串 s = new String(b, 0, len); }
3、遍歷字符串并獲得每個字符再進(jìn)行相關(guān)判斷后放入map集合中
// 遍歷字符串得到每一個字符 for (int i = 0; i < s.length() ; i++) { char c = s.charAt(i); // 判斷是否是字母 if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){ // 將字母轉(zhuǎn)換為小寫 char nc = Character.toLowerCase(c); // 判斷map集合中是否有該鍵 if (map.containsKey(nc)){ Integer num = map.get(nc); num++; map.put(nc,num); }else { map.put(nc,1); } } }
4、遍歷map集合得到數(shù)據(jù)
// 遍歷map集合得到數(shù)據(jù) Set<Character> key = map.keySet(); for (Character c : key) { Integer value = map.get(c); System.out.println("key="+c+"----"+"value="+value); }
四、全部代碼
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.Set; public class totalNum { public static void main(String[] args) throws IOException { // todo 統(tǒng)計字母出現(xiàn)的次數(shù) FileInputStream fis = null; String s = null; HashMap<Character, Integer> map = new HashMap<>(); try { fis = new FileInputStream("E:\\JavaCode\\JavaSE\\Day8-25\\src\\a1\\a.txt"); byte[] b = new byte[1024]; int len = 0; while ((len = fis.read(b))!=-1){ // 獲得文件中的字符串 s = new String(b, 0, len); } // 遍歷字符串得到每一個字符 for (int i = 0; i < s.length() ; i++) { char c = s.charAt(i); // 判斷是否是字母 if ( ((c>='a')&&(c<='z')) || ((c>='A')&&(c<='Z'))){ // 將字母轉(zhuǎn)換為小寫 char nc = Character.toLowerCase(c); // 判斷map集合中是否有該鍵 if (map.containsKey(nc)){ Integer num = map.get(nc); num++; map.put(nc,num); }else { map.put(nc,1); } } } // 遍歷map集合得到數(shù)據(jù) Set<Character> key = map.keySet(); for (Character c : key) { Integer value = map.get(c); System.out.println("key="+c+"----"+"value="+value); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { fis.close(); } } }
五、結(jié)果截圖(部分)
六、a.txt文本
I am happy to join with you. today in what will go down in history ,as the greatest demonstration for freedom in the history of our nation…
Five score years ago, a great American, in whose symbolic shadow we stand today, signed the Emancipation Proclamation. This momentous decree came as a great beacon light of hope to millions of Negro slaves who had been seared in the flames of withering injustice. It came as a joyous daybreak to end the long night of bad captivity.
到此這篇關(guān)于Java利用Map實現(xiàn)計算文本中字符個數(shù)的文章就介紹到這了,更多相關(guān)Java計算文本字符個數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果的任務(wù)
Callable與Future兩功能是Java?5版本中加入的,這篇文章主要給大家介紹了關(guān)于Java多線程教程之如何利用Future實現(xiàn)攜帶結(jié)果任務(wù)的相關(guān)資料,需要的朋友可以參考下2021-12-12springboot?+mybatis?使用PageHelper實現(xiàn)分頁并帶條件模糊查詢功能
這篇文章主要介紹了springboot?+mybatis?使用PageHelper實現(xiàn)分頁并帶條件模糊查詢功能,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-02-02Java中的javaBean、vo、entity、domain和pojo
這篇文章主要介紹了Java中的javaBean、vo、entity、domain和pojo用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12