大家可以参考下面代码,有什么疑问请留言。。。
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 + "]"; } }
原文地址:https://www.cnblogs.com/revel171226/p/8947527.html
时间: 2024-10-02 20:47:14