TreeMap按value值进行排序
2016-06-27 09:51
501 查看
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class CountAccurrenceOfWord {
public static void main(String[] args) {
String text = "Have a good day. Have a good class. Have a good visit. Have fun";
Map<String, Integer> hashMap = new HashMap<String, Integer>();
String[] words = text.split("[ .!?]");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 1) {
if (hashMap.get(words[i]) != null) {
int value = hashMap.get(words[i]).intValue();
value++;
hashMap.put(words[i], value);
} else
hashMap.put(words[i], 1);
}
}
Map<String, Integer> treeMap = new TreeMap<String, Integer>(hashMap);
System.out
.println("Dispaly words and their count in ascending order of the word");
System.out.println(treeMap);
List arrayList = new ArrayList(hashMap.entrySet());
Collections.sort(arrayList, new Comparator(){
public int compare(Object o1, Object o2) {
Map.Entry obj1 = (Map.Entry) o1;
Map.Entry obj2 = (Map.Entry) o2;
return ((Integer) obj2.getValue()).compareTo((Integer)obj1.getValue());
}
});
System.out.println(arrayList);
}
}
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
public class CountAccurrenceOfWord {
public static void main(String[] args) {
String text = "Have a good day. Have a good class. Have a good visit. Have fun";
Map<String, Integer> hashMap = new HashMap<String, Integer>();
String[] words = text.split("[ .!?]");
for (int i = 0; i < words.length; i++) {
if (words[i].length() > 1) {
if (hashMap.get(words[i]) != null) {
int value = hashMap.get(words[i]).intValue();
value++;
hashMap.put(words[i], value);
} else
hashMap.put(words[i], 1);
}
}
Map<String, Integer> treeMap = new TreeMap<String, Integer>(hashMap);
System.out
.println("Dispaly words and their count in ascending order of the word");
System.out.println(treeMap);
List arrayList = new ArrayList(hashMap.entrySet());
Collections.sort(arrayList, new Comparator(){
public int compare(Object o1, Object o2) {
Map.Entry obj1 = (Map.Entry) o1;
Map.Entry obj2 = (Map.Entry) o2;
return ((Integer) obj2.getValue()).compareTo((Integer)obj1.getValue());
}
});
System.out.println(arrayList);
}
}
相关文章推荐
- (转载)新手如何正确理解GitHub中“PR(pull request)”中的意思
- iOS中的导航栏UINavigationBar与工具栏UIToolBar要点解析
- 【Arduino官方教程第一辑】示例程序 6-3 Memsic2125加速度计
- 如何在PowerBuilder与DLL之间传递参数
- WWDC-UIKit 中协议与值类型编程实战
- Vue.js——60分钟快速入门
- 全球变暖基础知识小测试程序(Global Warming Facts Quiz)
- 【转】如何在IOS中使用3D UI - CALayer的透视投影
- 【转 iOS 8 Auto Layout界面自动布局系列2-使用Xcode的Interface Builder添加布局约束
- Soap UI 5.2.1 使用手册
- Default Values Table (C# Reference)
- heap with priority queues
- soapui笔记12:PropertyTransfer
- soapui笔记11:Propertis
- UITextField
- Non-Mathematical Feature Engineering techniques for Data Science
- EasyUI模糊查询
- Druid数据库连接池及内置监控的配置和使用
- 334. Increasing Triplet Subsequence
- 232. Implement Queue using Stacks