Java statistics The number of occurrences of each character in a story

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.