n Tasks mastering the statistics of Java series The number of words appearing in an article

Source: Internet
Author: User

Question: Count the number of occurrences of a word in a story

Ideas:

(1) The article (a string store) is stored in a string (word) array after splitting (split) by a space.

(2) Define a map,key is a string type, save the word; value is a numeric type that holds the number of occurrences of the word.

(3) traversing (1) The resulting string array, for each word, the study of the map key in the occurrence of the word, if not, add an element in map, key is the word, value is 1 (first occurrence);

If the word is found in the key of the map, the corresponding value (the number of occurrences of the word) is found by key, and the value is added 1 and saved back to the map.

(4) Traverse the map obtained in (3), output key (word) and corresponding value (number of times).

The demo code is as follows:

Import Java.util.hashmap;import Java.util.iterator;public class has {//count word occurrences public static String statlist (Strin   G str) {StringBuffer SB = new StringBuffer (); Hashmap<string,integer> has = new hashmap<string,integer> ();   Open a hash table string[] slist = Str.split (""); for (int i = 0; i < slist.length; i++) {if (!has.containskey (Slist[i])) {//If there is no such word has.put (SL         Ist[i], 1);                 } else {//if any, add 1 Integer ncounts = Has.get (Slist[i]);        Has.put (slist[i],ncounts+1);      }}//Traverse map Iterator Iterator = Has.keyset (). Iterator ();             while (Iterator.hasnext ()) {String word = (string) iterator.next ();      Sb.append ("Word:"). Append (Word). Append ("Number of"). Append (Has.get (Word)). Append ("\ n"); } return Sb.tostring ();}  public static void Main (string[] args) {string s = new String ("Your is the mananger of the office supplies company. A colleague have received a letter CompaininG about a order for office furniture.        She has left the letter for you to answer and have written some notes on it. "); System.out.println (Statlist (s));}}

  

n Tasks mastering the statistics of Java series The number of words appearing in an article

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.