GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Java-Interview-Programs/MaxDuplicateWordCount.java at master · luminglei/Java-Interview-Programs Expand file tree/ Copy pathMaxDuplicateWordCount.javaMore file actions 82 lines (72 loc) · 2.3 KB / Copy pathMaxDuplicateWordCount.javaFile metadata and controls82 lines (72 loc) · 2.3 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 package com.sky.pgm; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.StringTokenizer; public class MaxDuplicateWordCount { public Map<String, Integer> getWordCount(String fileName){ FileInputStream fis = null; DataInputStream dis = null; BufferedReader br = null; Map<String, Integer> wordMap = new HashMap<String, Integer>(); try{ fis = new FileInputStream(fileName); dis = new DataInputStream(fis); br = new BufferedReader(new InputStreamReader(dis)); String line = null; while((line=br.readLine())!=null){ StringTokenizer st = new StringTokenizer(line," "); while(st.hasMoreTokens()){ String tmp = st.nextToken().toLowerCase(); if(wordMap.containsKey(tmp)){ wordMap.put(tmp, wordMap.get(tmp)+1); }else { wordMap.put(tmp,1); } } } }catch(FileNotFoundException e){ e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { if(br!= null){ br.close(); } } catch (Exception e2) { } } return wordMap; } public List<Entry<String, Integer>> sortByValue(Map<String, Integer> wordMap){ Set<Entry<String, Integer>> set = wordMap.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); Collections.sort(list, new Comparator<Map.Entry<String, Integer>>(){ public int compare( Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2 ) { return (o2.getValue()).compareTo( o1.getValue() ); } }); return list; } public static void main(String[] args) { MaxDuplicateWordCount mdc = new MaxDuplicateWordCount(); Map<String, Integer> wordMap = mdc.getWordCount("D:\\test.txt"); List<Entry<String, Integer>> list = mdc.sortByValue(wordMap); for (Map.Entry<String, Integer> entry: list) { System.out.println(entry.getKey()+" "+entry.getValue()); } } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |