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