Demo of sorted storage in Java

Source: Internet
Author: User

Letter.java
 PackageYzhou.map;/** *  * @authorOcean **/ Public classletter{PrivateString name; Private intcount;  PublicLetter () {//TODO auto-generated Constructor stub    }     PublicLetter (String name) {Super();  This. Name =name; }         PublicLetter (String name,intcount) {        Super();  This. Name =name;  This. Count =count; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetCount () {returncount; }     Public voidSetCount (intcount) {         This. Count =count; }        }

Mapdemo02.java

 PackageYzhou.map;ImportJava.util.HashMap;ImportJava.util.Iterator;ImportJava.util.Map;ImportJava.util.Set;/*** Sorting Storage: 1:n * Count the number of occurrences of a word * this is a cat and that's a mice and where is the food? *@author* * * * ideas: * *. split String * * pickup storage. See the number of occurrences of a word as required **/ Public classmapdemo02{ Public Static voidMain (string[] args) {//1. Splitting a stringString[] arr = ("This was a cat and that's a mice and where is the food?"). Split (""); //2. Sorting storagemap<string, letter> map =NewHashmap<string,letter>();  for(String key:arr) {//first check if a bag is present//if (!map.containskey (key)) {//Map.put (Key, new Letter (key));//            }//            //Get the bag//Letter value = Map.get (key);//Value.setcount (Value.getcount () +1);Letter Value=Map.get (key); if(NULL==value) {Value=NewLetter ();            Map.put (key, value); } value.setcount (Value.getcount ()+1); }                        //3. See the number of occurrences of each word         for(String key:map.keySet ()) {Letter Value=Map.get (key); SYSTEM.OUT.PRINTLN (Key+ "-+" +Value.getcount ()); }                    }}

Demo of sorted storage in Java

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.