22.2

 1 import java.io.File;
 2 import java.io.FileNotFoundException;
 3 import java.util.HashMap;
 4 import java.util.Map;
 5 import java.util.Scanner;
 6 import java.util.Set;
 7 import java.util.*;
 8 
 9 public class S22_2 {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         Map<String,Integer> myMap = new TreeMap<String,Integer>();
14         File myFile = new File("txt.txt");
15         try {
16             Scanner input = new Scanner(myFile);
17             while(input.hasNext()){
18                 String str = input.next().toLowerCase();
19                 if(myMap.get(str) == null){
20                     myMap.put(str, 1);
21                 } else {
22                     int value = myMap.get(str).intValue();
23                     value++;
24                     myMap.put(str,value);
25                 }
26             }
27         } catch (FileNotFoundException e) {
28             // TODO Auto-generated catch block
29             e.printStackTrace();
30         }
31         Set<Map.Entry<String, Integer>> entrySet = myMap.entrySet();    
32         
33         for(Map.Entry<String, Integer> entry:entrySet)
34             System.out.println(entry.getKey() + "	" +
35         entry.getValue());            
36     }
37 
38 }
View Code
原文地址:https://www.cnblogs.com/wanjiang/p/6052633.html