Kwic--java count the number of words and output in sequence

Source: Internet
Author: User
Tags readfile

2016-07-02 (Essay Writing time)

Wrote for a long time to avoid the future use of the program ....

is a count of the number of words, and according to the number from large to small output. Enter the file name OK

The words are sorted according to the first letter,,, inside the map, etc.,, note the rewrite comparison function, because we are to sort by the value, rather than the general sort by key,,, we want to output from the number of more to less,,, the number is repeated

if (Base.get (a) >=base.get (b)) {
return-1; Note Do not return 0 will remove duplicates
} has played a key role.

Because we want to sort by the number of numbers, so remember to rewrite the comparison function, why can't let the number do keys, because the key can not be repeated, so here the word is the key, OK.

See Code, write the chaos, but achieve the specific requirements,,,

Str.split ("\\s+"); Here the record delimiter

Regular expressions,
\\d represents 0-9 of the number,
\\s denotes whitespace, carriage return, newline, and so on.
\\w denotes a word character (numeric letter underline)
The + sign denotes one or more meanings

1  PackageCalnum;2 3 ImportJava.io.BufferedReader;4 Importjava.io.FileNotFoundException;5 ImportJava.io.FileReader;6 Importjava.io.IOException;7 ImportJava.nio.Buffer;8 ImportJava.nio.ShortBuffer;9 Importjava.util.ArrayList;Ten Importjava.util.Collections; One ImportJava.util.Comparator; A ImportJava.util.HashMap; - ImportJava.util.Iterator; - Importjava.util.List; the ImportJava.util.Map; - ImportJava.util.Map.Entry; - ImportJava.util.Scanner; - ImportJava.util.TreeMap; +  - Importjavax.swing.Spring; + Importjavax.swing.text.html.parser.Entity; A  at  Public classcalwordnum{ -  -      PublicString ReadFile ()throwsIOException { -String all =NULL; -         //System.out.print ("##########################"); -Scanner cin =NewScanner (system.in); inSystem.out.print ("Plz input file name:"); -String filename =Cin.next (); toFileReader Read =Newfilereader (filename); +BufferedReader re =NewBufferedReader (read); -StringBuffer strbuf =NewStringBuffer (); the  * String SS; $          while((ss = Re.readline ())! =NULL) {Panax Notoginseng Strbuf.append (ss); -         //read the full text the         } +  A         returnstrbuf.tostring (); the  +     } -  $     // $      Publicstring[] Input (String str) { -string[] arr = str.split ("\\s+"); -  the         returnarr; -     }Wuyi  the      Public voidPutmap (list<string>ls) { -String temp, Theword =NULL; Wu         intFlage = 0; -Theword = Ls.get (0); Aboutmap<string, integer> map =NewHashmap<string, integer>(); $Iterator it =ls.iterator (); -          while(It.hasnext ()) { -             //System.out.println ("SSS" +it.next (). toString ()); -temp =It.next (). toString (); A  +             if(Temp.equals (Theword)) { theflage++; -                 Continue; $}Else { the map.put (Theword, flage); theFlage = 1; theTheword =temp; the  -             } in         } the  the map.put (Theword, flage); About         //overrides the comparison method, TreeMap sorts by value. Parameter required for two map comparison functions theValuecomparator BVC =Newvaluecomparator (map);  theMap<string,integer> MAP2 =NewTreemap<string,integer>(BVC);  the Map2.putall (map); +Iterator Mapit =Map2.entryset (). iterator (); -          while(Mapit.hasnext ()) { theEntry TR =(Entry) Mapit.next ();Bayi              theString num =(String) Tr.getkey (); the             intSA =(Integer) tr.getvalue (); -System.out.println ("This word:" +num + "have:" + sa + "x"); -         } the  the     } the  the     // / -      Public Static voidMain (string[] args)throwsIOException { the         //TODO auto-generated Method Stub thelist<string> ls =NewArraylist<string>(); the 94Calwordnum CA =Newcalwordnum (); the  theString Allword =ca.readfile (); thestring[] arr =Ca.input (Allword);98          for(inti = 0; i < arr.length; i++) { About  - Ls.add (Arr[i].tolowercase ());101         }102 103 collections.sort (LS);104 display (LS); the ca.putmap (LS);106 107     }108 109 @Override the      Public Booleanequals (Object obj) {111         //TODO auto-generated Method Stub the         return Super. Equals (obj);113     } the  the     Private Static voidDisplay (list<string>ls) { the         //TODO auto-generated Method Stub117 118          for(String s:ls)119 System.out.println (s); -     }    121     122 }123 //124 classValuecomparatorImplementsComparator<string> {   the       126Map<string, integer>Base; 127      PublicValuecomparator (map<string, integer>base) {   -          This. Base =Base; 129     }   the   131          the      Public intCompare (String A, string b) {133         if(Base.get (a) >=Base.get (b)) {  134             return-1;//note Do not return 0 will remove duplicates135         } 136         137         Else {  138             return1; 139         }  $     }  141}

No, after running the input file name on the line, if your delimiter is not//s+ can be satisfied, the trouble to modify their own. The result is a word, the number of words, a slip,,, can run, no errors. But there may be places to consider steps. Hope to criticize

Kwic--java count the number of words and output in sequence

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.