Java统计一篇文章中每个字符出现的个数

Posted 潘彬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java统计一篇文章中每个字符出现的个数相关的知识,希望对你有一定的参考价值。

大家可以参考下面代码,有什么疑问请留言。。。

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class CountTheTimesOfCharsAppear {    
    private static final String filePath = "\\String.txt";
    
    public static void main(String[] args) {
        
        Long start = System.nanoTime();
        //读取文件
        BufferedReader bReader = null;
        StringBuffer sBuffer = null;
        
        try {
            
            bReader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"));
            
            sBuffer = new StringBuffer();
            
            String line = null;
            while ((line = bReader.readLine())!=null) {
                
                sBuffer.append(line);
                
            }
            
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }finally {
            
            if (bReader != null) {
                try {
                    bReader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
        
        String data = new String(sBuffer);
        
        data.trim();
        
        System.out.println("文件的内容===》"+data);
        
        System.out.println(data.length());
        
        //解析文本内容不统计所有文集或者字符出现的次数
        
        
        //对字符串中的字符进行去重
        String newData = RemoveDuplicateChar(data);
        
        System.out.println("文件的内容===》"+newData);
        System.out.println(newData.length());
        
        List<content> list = new ArrayList<>();
        //統計每個字符出現的次數
        for(int i=0; i< newData.length();i++) {
            int count = 0;
            for(int j = 0; j < data.length();j++) {
                
                if (newData.charAt(i)== data.charAt(j)) {
                    
                    count++;
                }
            }
            
            list.add(new content(newData.charAt(i),count));
        }
        
        
        System.out.println(list);
        System.out.println(list.size());
        //根據字符出現的次數進行排序
        Collections.sort(list, new Comparator<content>() {

            @Override
            public int compare(content o1, content o2) {
                // TODO Auto-generated method stub
                return o2.getFrequency()-o1.getFrequency();
            }
        });
        
        System.out.println(list);
        System.out.println(list.size());
        
        
        Long end = System.nanoTime();
        
        System.out.println(end-start);
    }
    
    //对字符串中的字符进行去重
    /**
     * Return a String which doesn‘t contain repetitive characters
     * @param data this is the source String to remove the repetitive characters
     * @return String
     */
    public static String RemoveDuplicateChar(String s) {
        
        StringBuffer sb = new StringBuffer();
        int i=0;
        for(i=0; i<s.length();i++){
            char c=s.charAt(i);
            if(i==s.indexOf(c)) {
                 sb.append(c);
            }
        }
        return sb.toString();
    }

}

class content{
    
    private char key;
    private int frequency;
    public content(char key, int frequency) {
        super();
        this.key = key;
        this.frequency = frequency;
    }
    public Character getKey() {
        return key;
    }
    public void setKey(char key) {
        this.key = key;
    }
    public int getFrequency() {
        return frequency;
    }
    public void setFrequency(int frequency) {
        this.frequency = frequency;
    }
    @Override
    public String toString() {
        return "content [key=" + key + ", frequency=" + frequency + "]";
    }
    
    
    
}

 

以上是关于Java统计一篇文章中每个字符出现的个数的主要内容,如果未能解决你的问题,请参考以下文章

JAVA实验--统计文章中单词的个数并排序

Java上机实现统计某一目录下每个文件中出现的字母个数、数字个数、空格个数及行数?

java怎么统计字符串内的标点符号?

怎么统计输入的一个字符串中每个字母出现的次数?

Java获取字符串中字母出现的个数

统计一段文章的单词频率,取出频率最高的5个单词和个数(python)