You can refer to the following code, what questions please leave a message ...
ImportJava.io.BufferedReader;ImportJava.io.FileInputStream;Importjava.io.IOException;ImportJava.io.InputStreamReader;Importjava.util.ArrayList;Importjava.util.Arrays;Importjava.util.Collection;Importjava.util.Collections;ImportJava.util.Comparator;ImportJava.util.HashMap;ImportJava.util.HashSet;Importjava.util.List;ImportJava.util.Map;ImportJava.util.Set; Public classCountthetimesofcharsappear {Private Static FinalString FilePath = "\\String.txt"; Public Static voidMain (string[] args) {Long start=System.nanotime (); //Read FileBufferedReader Breader =NULL; StringBuffer Sbuffer=NULL; Try{Breader=NewBufferedReader (NewInputStreamReader (NewFileInputStream (FilePath), "UTF-8")); Sbuffer=NewStringBuffer (); String Line=NULL; while(line = Breader.readline ())! =NULL) {sbuffer.append (line); } } Catch(Exception e) {//Todo:handle ExceptionE.printstacktrace (); }finally { if(Breader! =NULL) { Try{breader.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }}}, String data=NewString (Sbuffer); Data.trim (); System.out.println ("Contents of the file = = =" +data); System.out.println (Data.length ()); //parsing text content does not count the number of occurrences of all anthology or characters//To de-weight characters in a stringString NewData =Removeduplicatechar (data); System.out.println ("Contents of the file = = =" +NewData); System.out.println (Newdata.length ()); List<content> list =NewArraylist<>(); //the number of times each character appears for(inti=0; i< newdata.length (); i++) { intCount = 0; for(intj = 0; J < Data.length (); j + +) { if(Newdata.charat (i) = =Data.charat (j)) {Count++; }} list.add (Newcontent (Newdata.charat (i), count)); } System.out.println (list); System.out.println (List.size ()); //the number of times the number is sorted by characterCollections.sort (list,NewComparator<content>() {@Override Public intCompare (content O1, content O2) {//TODO auto-generated Method Stub returnO2.getfrequency ()-o1.getfrequency (); } }); SYSTEM.OUT.PRINTLN (list); System.out.println (List.size ()); Long End=System.nanotime (); System.out.println (End-start); } //To de-weight characters in a string /*** Return a String which doesn ' t contain repetitive characters *@paramdata The source String to remove the repetitive characters *@returnString*/ Public Staticstring Removeduplicatechar (string s) {StringBuffer sb=NewStringBuffer (); intI=0; for(i=0; I<s.length (); i++){ CharC=S.charat (i); if(i==S.indexof (c)) {sb.append (c); } } returnsb.tostring (); }}classcontent{Private Charkey; Private intfrequency; PublicContentCharKeyintfrequency) { Super(); This. Key =key; This. Frequency =frequency; } PublicCharacter GetKey () {returnkey; } Public voidSetkey (Charkey) { This. Key =key; } Public intgetfrequency () {returnfrequency; } Public voidSetFrequency (intfrequency) { This. Frequency =frequency; } @Override PublicString toString () {return"Content [key=" + key + ", frequency=" + frequency + "]"; } }
Java statistics The number of occurrences of each character in a story