欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

打印出某段英文中重复出现的单词

程序员文章站 2022-06-08 11:29:55
...
BufferedReader br = new BufferedReader(new FileReader("d:\\test.txt"));
        String line = "";
        Map<String,Integer> map = new HashMap<String,Integer>();
        while(br.readLine()!=null){
            line = br.readLine();
            String[] strArray = line.split(" ");
            int size = strArray.length;
            //统计各个单词出现的次数
            for(int i=0;i<size;i++){
                if(map.containsKey(strArray[i])){
                    int j = map.get(strArray[i]);
                    j = j + 1;
                    map.put(strArray[i], j);
                    //System.out.println(strArray[i]+":"+j);
                }else{
                    map.put(strArray[i], 1);
                }
                //System.out.println(strArray[i]);
            }
        }
        //若单词重复出现,则打印出该单词和该单词出现的次数
        Set<Entry<String, Integer>> set = map.entrySet();
        Iterator<Entry<String,Integer>> it = set.iterator();
        while(it.hasNext()){
            Entry<String,Integer> entry = it.next();
            if(entry.getValue()>1){
                System.out.println(entry.getKey()+":"+entry.getValue());
            }
        }