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 }
时间: 2024-10-13 05:58:05

TreeMap排序实例的相关文章

js对数组元素大小排序实例代码

js对数组元素大小排序实例代码:在实际应用中,有时候需要对数组中的元素按照大小来进行排序,当然是先排序的方法有多种,比如使用数组对象自带的sort()方法,本章节再来分享一种其他的方式,先看代码实例: var org=[5,4,3,2,1,6,7,9,8,10]; var tempArr=new Array(); for(var i=0;i<org.length;i++) { if(i==0) { tempArr[0]=org[0]; //把第一个元素放到新序列 } else { for(var

js数组元素由小到大排序实例代码

js数组元素由小到大排序实例代码:有时候需要对数组中的数字进行排序,下面是一段将数组中数字由小到大排序的代码实例,希望能够帮到大家.实例代码如下: var arr=[2,55,55,1,75,3,9,35,70,166,432,678,32,98]; var len=arr.length; console.log(arr.join(",")); var newarr=[]; for(var i=0;i<len;i++){ newarr.push(Math.min.apply(nu

9.2.2 hadoop全排序实例详解

1.1.1         全排序 (1)全排序概述 指的是让所有的输出结果都是有序的,最简单的方法就是用一个reduce任务,但是这样处理大型文件时效率极低,失去的并行架构的意义.所以可以采用分组排序的方法来实现全局排序,例如现在要实现按键的全局的排序,可以将键值按照取值范围分为n个分组,<-10℃,-10℃~0℃, 0℃~10℃,>10℃.实现partitioner类,创建4个分区,将温度按照取值范围分类到四个分区中,每个分区进行排序,然后将4个分区结果合并成一个,既是一个全局有序的输出.

qsort 与sort 对结构体排序实例

qsort 与sort 对结构体排序实例 #include<bits/stdc++.h> using namespace std; typedef struct { string book; int num; }Book; //qsort的比较函数 int cmp(const void * a, const void * b) { return (*(Book*)a).num > (*(Book*)b).num ? 1 : 0; } //sort的比较函数 bool cmp_(Book

Yii CGridView 关联表搜索排序实例

在这篇文章中,我准备讲解如何在CGridView中搜索或者排序关联表中的某一行,通过给Yii Blog demo添加一个list页面. 首先,检查你的blog demo里的protected\models\Comment.php,确保Comment模型有一个search的方法,如果没有,就用gii生成一个,我下载到的blog demo里倒是没有. 然后,写代码的时间到了,我们从 CommentController 开始,我们给它加一个 actionList: 1 2 3 4 5 6 7 8 9

TreeSet与TreeMap排序

1.TreeSet原理: /* * TreeSet存储对象的时候, 可以排序, 但是需要指定排序的算法 *  * Integer能排序(有默认顺序), String能排序(有默认顺序), 自定义的类存储的时候出现异常(没有顺序) *  * 如果想把自定义类的对象存入TreeSet进行排序, 那么必须实现Comparable接口 *   在类上implement Comparable  *   重写compareTo()方法 *   在方法内定义比较算法, 根据大小关系, 返回正数负数或零 *  

TreeMap 排序

一.TreeMap TreeMap 默认排序规则:按照key的字典顺序来排序(升序) 当然,也可以自定义排序规则:要实现Comparator接口. 用法简单,先看下下面的demo public class SortDemo { public static void main(String[] args) { System.out.println("---------------- 默认 排序结果-----------------"); createDefaultSortTreeMap(

mysql 实现排名及中文排序实例[分页累加行号]

/*排名相同情况下,优先按姓名排序*/SELECT t.`name`, t.company_name, @rownum:[email protected]+1 as rankNum, t.ss from (    SELECT u.`name`, sci.company_name, @rownum:=0,(u.check_num+u.online_hours) as ss FROM v_user u    LEFT JOIN sys_company_info sci ON u.companyId

TreeMap排序

import java.util.Comparator; import java.util.HashMap; import java.util.Map; import java.util.TreeMap; public class MapSort {            public  TreeMap<String,Double> sort(HashMap<String,Double> map){         ValueComparator bvc =  new ValueC