您的位置:首页 > 产品设计 > UI/UE

大日志文件中如何统计单词个数?及map按value排序lambda表达式版

2017-05-19 21:13 274 查看


package com.nys.countwords;

import java.io.File;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Collections;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.Map.Entry;

import java.util.Scanner;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class CountPaser {

private Map<String, Integer> countMap=new HashMap<>();

public void parse(String logPath) throws IOException {

Pattern pattern = Pattern.compile("(?i)[a-z]+");
Matcher matcher=null;
Scanner scanner=new Scanner(new File(logPath));
while(scanner.hasNextLine()) {
String line = scanner.nextLine();
matcher = pattern.matcher(line);
while(matcher.find()) {
String word = matcher.group();
if(null==countMap.get(word)) {
countMap.put(word, 1);
}else {
countMap.put(word, countMap.get(word)+1);
}
}
}
scanner.close();
}

public static void main(String[] args) {

CountPaser cp = new CountPaser();
try {
cp.parse("nys.log");
} catch (IOException e) {
e.printStackTrace();
}
Map<String, Integer> map = cp.countMap;

Map sortMap = CountPaser.sortMap(map);
System.out.println(sortMap);

}

/**
* 对map中的数据按照value排序
*/
public static Map sortMap(Map<String, Integer> oldMap) {

ArrayList<Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(oldMap.entrySet());

Collections.sort(list, (x,y)->x.getValue().compareTo(y.getValue()));//java8特性 lambda表达式

LinkedHashMap<String,Integer> newMap = new LinkedHashMap<String,Integer>();

for(int i=0;i<list.size();i++) {
newMap.put(list.get(i).getKey(), list.get(i).getValue());
}

return newMap;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: