Chinaunix首页 | 论坛 | 博客
  • 博客访问: 107695
  • 博文数量: 41
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-23 12:37
文章分类

全部博文(41)

文章存档

2015年(1)

2014年(28)

2013年(12)

我的朋友

分类: Java

2014-08-31 12:43:18

从类似如下的文本文件中读取出所有的姓名,并打印出重复的姓名和重复的次数,并按重复次数排序,文本内容如下:
1,张三,28
2,李四,35
3,张三,28
4,王五,35
5,张三,28
6,李四,35
7,赵六,28
8,田七,35

点击(此处)折叠或打开

  1. package com.wp;


  2. import java.io.*;
  3. import java.util.*;

  4. public class GetNameCount {

  5.     /**
  6.      * @param args
  7.      */
  8.     public static void main(String[] args) throws IOException{
  9.         // TODO Auto-generated method stub
  10.         
  11.         //两种读文件的方式
  12.         BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("d:\\java\\infor.txt")));
  13.         //InputStream ips = GetNameCount.class.getResourceAsStream("infor.txt");
  14.         //BufferedReader br = new BufferedReader(new InputStreamReader(ips));
  15.         
  16.         String line=null;
  17.         
  18.         Map<String,Integer> results=new HashMap<String,Integer>();
  19.         
  20.         try{
  21.             while((line=br.readLine())!=null)
  22.             {
  23.              dealLine(line,results);
  24.             }
  25.             sortResults(results);
  26.             
  27.         }catch(IOException e){             
  28.             e.printStackTrace();
  29.         }
  30.     }
  31.     
  32.     public static void dealLine(String line,Map<String,Integer> map)
  33.     {
  34.      if(!"".equals(line.trim()))//出去字符处的前导空格,防止不必要的空格导致错误
  35.      {
  36.           String [] results =line.split(",");
  37.           if(results.length == 3)
  38.           {
  39.               String name = results[1];
  40.               Integer value =(Integer)map.get(name);
  41.     
  42.               if(value ==null)
  43.                   value = 0;
  44.              
  45.               map.put(name,value + 1);
  46.           }
  47.      }
  48.     }

  49.     static class User
  50.     {
  51.      public String name;
  52.      public Integer value;
  53.     
  54.      public User(String name,Integer value){
  55.          this.name = name;
  56.          this.value = value;
  57.     }
  58.     @Override
  59.         public boolean equals(Object obj) {
  60.     // TODO Auto-generated methodstub
  61.     //下面的代码没有执行,说明往treeset中增加数据时,不会使用到equals方法。
  62.         boolean result =super.equals(obj);
  63.         System.out.println(result);
  64.         return result;
  65.         }
  66.     }
  67.     
  68.     
  69.     private static void sortResults(Map<String,Integer> results) {
  70.         // TODO Auto-generated method stub
  71.         @SuppressWarnings("unchecked")
  72.         TreeSet<User> sortedResults =new TreeSet<User>(new Comparator(){
  73.             
  74.         public int compare(Object o1, Object o2) {
  75.         // TODOAuto-generated method stub
  76.             User user1 = (User)o1;
  77.             User user2 = (User)o2;
  78.         /*如果compareTo返回结果0,则认为两个对象相等,新的对象不会增加到集合中去
  79.         * 所以,不能直接用下面的代码,否则,那些个数相同的其他姓名就打印不出来。
  80.         * */
  81.         //returnuser1.value-user2.value;
  82.         //returnuser1.value
  83.             if(user1.value<user2.value){
  84.                 return -1;
  85.             }else if(user1.value>user2.value){
  86.                 return 1;
  87.             }else{
  88.                 return user1.name.compareTo(user2.name);
  89.             }
  90.         }
  91.     } );
  92.         
  93.         Iterator iterator =results.keySet().iterator();
  94.         
  95.         while(iterator.hasNext())
  96.         {
  97.             String name = (String)iterator.next();
  98.             Integer value =(Integer)results.get(name);
  99.         
  100.             if(value >= 1)
  101.             {
  102.                 sortedResults.add(new User(name,value));
  103.             }
  104.         }
  105.             printResults(sortedResults);
  106.         }
  107.     
  108.         private static void printResults(TreeSet<User> sortedResults)
  109.         {
  110.             Iterator iterator = sortedResults.iterator();
  111.             while(iterator.hasNext())
  112.             {
  113.                 User user = (User)iterator.next();
  114.                 System.out.println(user.name +":" + user.value);
  115.             }
  116.         }
  117.     
  118. }

阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~