统计文件中姓名及出现次数的小例子

来源:互联网 发布:日本杂志模特知乎 编辑:程序博客网 时间:2024/04/26 18:53
/** *从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序:1,张三,282,李四,353,张三,284,王五,355,张三,286,李四,357,赵六,288,田七,35 * @author 大蘑菇 * */public class CountName {     /**      * ①读取文件到字符串      * ②按照换行分割成字符串数组      * ③遍历字符串数组,取出姓名      * ④将姓名和出现的次数存入map中;      * ⑤利用 treeset整理结果;为此需要封装User,其中包含了name,以及count;需要在User 中实现comparable接口或者构造 treesetmap时传入comparator;      *      * @throws Exception      */          static class User /*implements Comparable<User>*/ {          String name;           int count ;           public User(String name, int count) {               super();               this.name = name;               this.count = count;          }           @Override           public String toString() {               return "User [name=" + name + ", count=" + count + "]" ;          }                     /**           * 方法一。使用comparable接口来进行排序;           *///        @Override//        public int compareTo(User o) {//            if(this.count>o.count)return -1;//            if(this.count<o.count)return 1;//            //            return this.name.compareTo(o.name);//            //             return this.count>o.count?1:(this.count<o.count?-1:this.name.compareTo(o.name));//            //        }               }          public static void main(String[] args) throws Exception {           //读取文件;          File file= new File("d:\\test\\name.txt" );          FileReader fr=new FileReader(file);           int fileSize=(int) file.length();           char[] buff=new char[fileSize];                     //用来存放名字和对应的次数;          Map<String, Integer> map= new HashMap<String, Integer>();                     //安装次数由小到大排序后的结果;传入比较器,使用comparator来比较;这样元素再加入的时候就会调用比较方法,按照默认升序排列          Set<User> sortedResult= new TreeSet<User>(new Comparator<User>() {               @Override               public int compare(User o1, User o2) {//                 if(o1.count>o2.count)return -1;//默认是从小到大排列//                 if(o1.count<o2.count)return 1;//                 return o1.name.compareTo(o2.name);                    //更简洁的写法                    return o1.count >o2.count ? 1:(o1.count<o2.count ?-1:o1.name .compareTo(o2.name));              }          });                     int len=fr.read(buff);//得到读取的字符个数;          String result= new String(buff,0,len);                    String[] strings = result.split( "\\s+");           //将姓名提取出来;并将姓名和出现的次数放入到集合中;           for (String string : strings) {              string= new String(string.getBytes(),"utf-8" );//1,张三,28(解决乱码)              System. out.println(string);                            String[] strings2 = string.split( ",");              String name=strings2[1];             //存入姓名以及对应的次数              Integer count=map.get(name);               if(count==null ){                   map.put(name, 1);              } else{                   map.put(name, count+1);              }          }           //遍历map,得到所有的entry集合,封装成user,加入到set集合中;          Set<Map.Entry<String, Integer>> entrySet = map.entrySet();          Iterator<Entry<String, Integer>> iterator = entrySet.iterator();                     while(iterator.hasNext()){              Entry<String, Integer> entry = iterator.next();               System. out.println(entry.getKey()+":" +entry.getValue()+"次");              User user= new User(entry.getKey(), entry.getValue());              sortedResult.add(user); //在加入数据时,自动安装 compareto定义的进行排序;                        }           //遍历整理好的set,从小到大升序排列          Iterator<User> it = sortedResult.iterator();           while(it.hasNext()){              User user=it.next();              System. out.println(user);          }     }          }

0 0