1. 程式人生 > >大日誌檔案中如何統計單詞個數?及map按value排序lambda表示式版

大日誌檔案中如何統計單詞個數?及map按value排序lambda表示式版


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;

}

}