import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Yingwentxt {
public static void main(String[] args) {
String letter = null;
Map<String, Integer> num = new TreeMap<String, Integer>();
try {
File f = new File("d:/1/111.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while ((letter = br.readLine()) != null) {
letter.toLowerCase();
String reg1 = "\\b";
String reg2 = "\\w+";
String[] str = letter.split(reg1);
for (String s : str) {
if (s.matches(reg2)) {
// 判断集合中是否已经存在该单词,如果存在则个数加一,否则将单词添加到 //集合中,且个数置为1
if (!num.containsKey(s)) {
num.put(s, 1);
} else {
num.put(s, num.get(s) + 1);
}
}
}
}
} catch (Exception e) {
System.out.println(e);
}
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(num.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
// 升序排序
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ":" + mapping.getValue());
}
}
}
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
public class Yingwentxt {
public static void main(String[] args) {
String letter = null;
Map<String, Integer> num = new TreeMap<String, Integer>();
try {
File f = new File("d:/1/111.txt");
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
while ((letter = br.readLine()) != null) {
letter.toLowerCase();
String reg1 = "\\b";
String reg2 = "\\w+";
String[] str = letter.split(reg1);
for (String s : str) {
if (s.matches(reg2)) {
// 判断集合中是否已经存在该单词,如果存在则个数加一,否则将单词添加到 //集合中,且个数置为1
if (!num.containsKey(s)) {
num.put(s, 1);
} else {
num.put(s, num.get(s) + 1);
}
}
}
}
} catch (Exception e) {
System.out.println(e);
}
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(num.entrySet());
// 然后通过比较器来实现排序
Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
// 升序排序
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
for (Map.Entry<String, Integer> mapping : list) {
System.out.println(mapping.getKey() + ":" + mapping.getValue());
}
}
}