zoukankan      html  css  js  c++  java
  • 作业

    package pinlv;
    import java.io.BufferedReader;
    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.TreeMap;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    public class Pinlv {
        public static void main(String[] args) throws Exception {
            BufferedReader reader = new BufferedReader(new FileReader("D:\wenzhang.txt"));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            reader.close();
            Pattern expression = Pattern.compile("[a-zA-Z]+");
            String string = buffer.toString();
            Matcher matcher = expression.matcher(string);//
            Map<String, Integer> map = new TreeMap<String, Integer>();
            String word = "";
            int times = 0;
            while (matcher.find()) {
                word = matcher.group();
                if (map.containsKey(word)) {
                    times = map.get(word);
                    map.put(word, times + 1);
                } else {
                    map.put(word, 1);
                }
            }
            List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
            Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>() {
                public int compare(Map.Entry<String, Integer> left,
                        Map.Entry<String, Integer> right) {
                    return (left.getValue()).compareTo(right.getValue());
                }
            };
            Collections.sort(list, comparator);
            int last = list.size() - 1;
            for (int i = last; i > last - 6; i--) {
                String key = list.get(i).getKey();
                Integer value = list.get(i).getValue();
                System.out.println(key + " :" + value);
            }
        }
    }

    在上机的前半个多小时里,我终于弄明白了怎么读取txt文件,在接下来的编程中使我的效率大大地提高,然后就是主要卡在了不知该怎么比较单词出现的频率

  • 相关阅读:
    netcore3.0使用Session
    docker redis使用
    协变和逆变
    HashTable、Dictionary、ConcurrentDictionary三者区别
    设计模式-行为型-解释器模式
    设计模式-行为型-备忘录模式
    jq实现批量全选与反选
    call()和apply()的用法及区别,看这个例子就明白了
    谈谈对闭包的理解
    export,import和export default的区别
  • 原文地址:https://www.cnblogs.com/baixiaoli/p/9775068.html
Copyright © 2011-2022 走看看