java統(tǒng)計(jì)文件中每個(gè)字符出現(xiàn)的個(gè)數(shù)
本文實(shí)例為大家分享了java統(tǒng)計(jì)文件中字符個(gè)數(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)的個(gè)數(shù)
return map.get(c);
}
public Set<Character> getAllChar(){ //獲取文件中字符的Set集合
return map.keySet();
}
public Map<Character,Integer> getMap(){ //獲取字符與其出現(xiàn)個(gè)數(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ì)算一個(gè)字符串中每個(gè)字符出現(xiàn)的次數(shù)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
/**
* 計(jì)算一個(gè)字符串中每個(gè)字符出現(xiàn)的次數(shù)
*
* 思路:
* 通過(guò)toCharArray()拿到一個(gè)字符數(shù)組-->
* 遍歷數(shù)組,將數(shù)組元素作為key,數(shù)值1作為value存入map容器-->
* 如果key重復(fù),通過(guò)getKey()拿到value,計(jì)算value+1后存入
*/
public class Test01 {
public static void main(String[] args) {
System.out.println("請(qǐng)輸入字符串:");
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);//通過(guò)key獲取value的值
map.put(arr_char,old+1);//把字符放入map的key中,value設(shè)置為通過(guò)key獲取value的值+1
}else{//查看字符是否在map的key中存在,如果不存,把字符放入map的key中,value默認(rèn)設(shè)置為1
map.put(arr_char,1);
}
}
return map;
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java面試題沖刺第四天--數(shù)據(jù)庫(kù)
這篇文章主要為大家分享了最有價(jià)值的三道數(shù)據(jù)庫(kù)面試題,涵蓋內(nèi)容全面,包括數(shù)據(jù)結(jié)構(gòu)和算法相關(guān)的題目、經(jīng)典面試編程題等,感興趣的小伙伴們可以參考一下2021-07-07
java反射實(shí)現(xiàn)javabean轉(zhuǎn)json實(shí)例代碼
基于java反射機(jī)制實(shí)現(xiàn)javabean轉(zhuǎn)json字符串實(shí)例,大家參考使用吧2013-12-12
Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼
Shiro是Apache?的一個(gè)強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼學(xué)和會(huì)話管理,Shiro?主要分為兩個(gè)部分就是認(rèn)證和授權(quán)兩部分,這篇文章主要介紹了Springboot+Shiro+Mybatis+mysql實(shí)現(xiàn)權(quán)限安全認(rèn)證的示例代碼,需要的朋友可以參考下2024-07-07
Java時(shí)間轉(zhuǎn)換成unix時(shí)間戳的方法
這篇文章主要為大家詳細(xì)介紹了Java時(shí)間轉(zhuǎn)換成unix時(shí)間戳的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
SpringBoot預(yù)加載與懶加載實(shí)現(xiàn)方法超詳細(xì)講解
Spring一直被詬病啟動(dòng)時(shí)間慢,可Spring/SpringBoot是輕量級(jí)的框架。因?yàn)楫?dāng)Spring項(xiàng)目越來(lái)越大的時(shí)候,在啟動(dòng)時(shí)加載和初始化Bean就會(huì)變得越來(lái)越慢,很多時(shí)候我們?cè)趩?dòng)時(shí)并不需要加載全部的Bean,在調(diào)用時(shí)再加載就行,那這就需要預(yù)加載與懶加載的功能了2022-11-11

