java統(tǒng)計(jì)文件中每個字符出現(xiàn)的個數(shù)
更新時間:2019年03月22日 10:15:55 作者:給糖吃的小騙子
這篇文章主要為大家詳細(xì)介紹了java統(tǒng)計(jì)文件中每個字符出現(xiàn)的個數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了java統(tǒng)計(jì)文件中字符個數(shù)的具體代碼,供大家參考,具體內(nèi)容如下
package com.zhu.io; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class FileCharacter{ Map<Character,Integer>map=new TreeMap<Character,Integer>(); public FileCharacter(String fileName) throws IOException{ BufferedReader br=new BufferedReader(new FileReader(new File(fileName))); int x; while((x=br.read())>0){ Character key=new Character((char)x); if(map.containsKey(key)){ int count=map.get(key); map.remove(key); map.put(key, ++count); }else{ map.put(key, 1); } } } public int getCount(char c){ //獲取字符在文件中出現(xiàn)的個數(shù) return map.get(c); } public Set<Character> getAllChar(){ //獲取文件中字符的Set集合 return map.keySet(); } public Map<Character,Integer> getMap(){ //獲取字符與其出現(xiàn)個數(shù)組成的Map集合 return map; } public void printInfo(){ //打印信息 Set<Map.Entry<Character, Integer>>set=map.entrySet(); for(Map.Entry<Character, Integer> entry:set){ System.out.println("[ "+entry.getKey()+" ]"+"\t"+"count:"+entry.getValue()); } } public static void main(String[] args) throws IOException { FileCharacter fc=new FileCharacter("e:\\test.txt"); fc.printInfo(); } }
小編另為大家分享一段代碼:計(jì)算一個字符串中每個字符出現(xiàn)的次數(shù)
import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * 計(jì)算一個字符串中每個字符出現(xiàn)的次數(shù) * * 思路: * 通過toCharArray()拿到一個字符數(shù)組--> * 遍歷數(shù)組,將數(shù)組元素作為key,數(shù)值1作為value存入map容器--> * 如果key重復(fù),通過getKey()拿到value,計(jì)算value+1后存入 */ public class Test01 { public static void main(String[] args) { System.out.println("請輸入字符串:"); Scanner sc=new Scanner(System.in); while (sc.hasNextLine()){ String str=sc.nextLine(); Map<Character,Integer> map =count(str); System.out.println(map); } } public static Map<Character,Integer> count(String str){ Map<Character,Integer> map=new HashMap<Character,Integer>(); char[] array_char=str.toCharArray();//把字符串轉(zhuǎn)成字符數(shù)組 for(char arr_char: array_char){//遍歷字符數(shù)組 if(map.containsKey(arr_char)){//查看字符是否在map的key中存在,如果存在 Integer old=map.get(arr_char);//通過key獲取value的值 map.put(arr_char,old+1);//把字符放入map的key中,value設(shè)置為通過key獲取value的值+1 }else{//查看字符是否在map的key中存在,如果不存,把字符放入map的key中,value默認(rèn)設(shè)置為1 map.put(arr_char,1); } } return map; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼
基于java反射機(jī)制實(shí)現(xiàn)javabean轉(zhuǎn)json字符串實(shí)例,大家參考使用吧2013-12-12Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼
Shiro是Apache?的一個強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼學(xué)和會話管理,Shiro?主要分為兩個部分就是認(rèn)證和授權(quán)兩部分,這篇文章主要介紹了Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼,需要的朋友可以參考下2024-07-07SpringBoot預(yù)加載與懶加載實(shí)現(xiàn)方法超詳細(xì)講解
Spring一直被詬病啟動時間慢,可Spring/SpringBoot是輕量級的框架。因?yàn)楫?dāng)Spring項(xiàng)目越來越大的時候,在啟動時加載和初始化Bean就會變得越來越慢,很多時候我們在啟動時并不需要加載全部的Bean,在調(diào)用時再加載就行,那這就需要預(yù)加載與懶加載的功能了2022-11-11