• TreeMap排序实例


       现有一篇英文作文,让你从中找出出现频率最高的n个单词

    As can be seen from the chart that money value of annual global electronic transactions id increasing gradually in the seven years. In 1997, the money value of global electronic commerce transactions is US$ 2.6 billion, while the number reaches US$1000 billion, 500 times that of 1997. why is electronic commerce booming nowadays?s=s.toLowerCase() Several factors contribute to this phenomenon. First, the availability of computer is the foremost cause. The rapid development of computer technology enables everybody to have access to computer and Internet. Internet now no more a stranger to common people. Second, the technology of Internet is becoming more and more mature. People, who at first do not trust business transactions on Internet, now become convinced that doing business on Internet is very convenient and effective. Thirdly, electronic commerce is the fastest way so far to make transactions across far distance. It makes it possible to do business at home and it saves time and unnecessary formalities as well. That is why electronic commerce is preferable to the prosperity of world economy and it also give birth to SOHO, a special group of people working at home. The trend towards a promising e-commerce is inevitable. Therefore let's get prepared to embrace this irretrievable trend.

       思路:首先用正则表达式将作文分成多个英语单词,然后再用map映射记录每个单词并计算次数,最后用TreeMap将其按出现的次数进行排序。

      

     1 package text;
     2 import java.util.*;
     3 import java.util.regex.Matcher;
     4 import java.util.regex.Pattern;
     5 public class text7 {
     6 
     7     public static void main(String[] args) {
     8         
     9         String s="As can be seen from the chart that money value of annual global electronic transactions id increasing gradually in the seven years. In 1997, the money value of global electronic commerce transactions is US$ 2.6 billion, while the number reaches US$1000 billion, 500 times that of 1997. why is electronic commerce booming nowadays?s=s.toLowerCase() Several factors contribute to this phenomenon. First, the availability of computer is the foremost cause. The rapid development of computer technology enables everybody to have access to computer and Internet. Internet now no more a stranger to common people. Second, the technology of Internet is becoming more and more mature. People, who at first do not trust business transactions on Internet, now become convinced that doing business on Internet is very convenient and effective. Thirdly, electronic commerce is the fastest way so far to make transactions across far distance. It makes it possible to do business at home and it saves time and unnecessary formalities as well. That is why electronic commerce is preferable to the prosperity of world economy and it also give birth to SOHO, a special group of people working at home. The trend towards a promising e-commerce is inevitable. Therefore let's get prepared to embrace this irretrievable trend.";
    10         Pattern p=Pattern.compile("[a-z|']+");                //正则表达式的比较器
    11         Matcher m=p.matcher(s);                               //要找的文章
    12         Map<String,Integer> map1=new HashMap<String,Integer>();       //定义一个hashmap键中存放单词
    13         while(m.find()){ 
    14             String a=m.group(); 
    15             if(map1.containsKey(a)){                                 //值中存放次数
    16                  map1.put(a,map1.get(a)+1);
    17             }
    18             else{
    19                  map1.put(a,1);
    20             }
    21         }
    22         Map<String,Integer> map2=new TreeMap<String,Integer>(new Mycomparator(map1));   //定义一个treemap并给出排序的比较器
    23         map2.putAll(map1);                                                               //向其中添加元素    
    24         Set<String> set=map2.keySet();
    25         Iterator<String> it=set.iterator();                                              //迭代器输出
    26         while(it.hasNext()){
    27             String str=it.next();
    28             System.out.println(str+":"+map2.get(str));
    29         }
    30         //System.out.println(map2);
    31         
    32     
    33 
    34     }
    35 
    36 }
    37 class Mycomparator implements Comparator<String>{
    38     Map<String,Integer> map;
    39     Mycomparator(Map<String,Integer> map)
    40     {
    41         this.map=map;
    42     }
    43     @Override
    44     public int compare(String o1, String o2) {             //比较函数:首先按次数排序,如果次数相同按单词长度进行排序
    45         
    46         if(o1==null||o2==null)
    47             return 0;
    48         if(map.get(o2)-map.get(o1)!=0)
    49               return map.get(o2)-map.get(o1);
    50         else{
    51             return o1.compareTo(o2);
    52         }
    53             
    54     }
    55     
    56 }
  • 相关阅读:
    MVC模式在Java Web应用程序中的实例分析
    设计模式
    实现xxxxxxx系统六大质量属性战术
    阅读了《大型网站技术架构:核心原理与案例分析》,分析XX系统如何增加相应的功能,提高系统的可用性和易用性
    淘宝系统质量属性分析
    《架构漫谈》读后感
    软件架构师架构设计过程是什么?
    《架构之美》阅读笔记06
    《架构之美》阅读笔记05
    《架构之美》阅读笔记04
  • 原文地址:https://www.cnblogs.com/llsq/p/7439999.html
Copyright © 2020-2023  润新知