Chinaunix首页 | 论坛 | 博客
  • 博客访问: 485861
  • 博文数量: 111
  • 博客积分: 3146
  • 博客等级: 中校
  • 技术积分: 939
  • 用 户 组: 普通用户
  • 注册时间: 2009-07-07 11:23
个人简介

Nathing

文章分类

全部博文(111)

文章存档

2016年(2)

2015年(1)

2014年(31)

2012年(2)

2011年(9)

2010年(36)

2009年(30)

我的朋友

分类: Java

2010-10-29 23:02:41

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

结果:李四:2 王五:2 张三:3

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeSet;

/**
 * 打印出重复的姓名和重复的次数,并按重复次数排序
 * @author Administrator
 *
 */

public class GetNameTest {

    /**
     * @param args
     * @throws IOException
     */

    public static void main(String[] args)
    {
        Map<String, Integer> results = new HashMap<String, Integer>();
        InputStream is = GetNameTest.class.getResourceAsStream("info.txt");
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        String line = "";
        String name = "";
        Integer value = 0;
        //1.把从文件读取的数据存在Map中,以name为key,name出现的次数为value
        try
        {
            while ((line = br.readLine()) != null)
            {
                String lines[] = line.split(",");
                if (lines.length == 3)
                {
                    name = lines[1];
                    value = results.get(name);
                    if (value == null)
                    {
                        value = 0;
                    }
                    results.put(name, value + 1);
                }
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        System.out.println(results);

        //按一定的比较策略将map中value大于1的数据构建成user对象放入TreeSet中
        TreeSet<User> treeSet = new TreeSet<User>(
            new Comparator<Object>()
            {
                @Override
                public int compare(Object o1, Object o2)
                {
                    User u1 = (User)o1;
                    User u2 = (User)o2;
                    System.out.println("Comparator strategy");
                    if (u1.getValue()>u2.getValue())
                        return 1;
                    else if (u1.getValue()<u2.getValue())
                        return -1;
                    else

                        //不能返回0,否则,那些个数相同的其他姓名就打印不出来
                        return u1.getName().compareTo(u2.getName());
                }
            }
        );
        Iterator<String> iter = results.keySet().iterator();
        while (iter.hasNext())
        {
            name = (String)iter.next();
            if (results.get(name) > 1)
            {
                treeSet.add(new User(name,results.get(name)));
            }
        }
        
        Iterator<User> iterUser = treeSet.iterator();
        while (iterUser.hasNext())
        {
            User u = (User)iterUser.next();
            System.out.println(u.getName() +":" + u.getValue());
        }
    }
}
class User
{
    private String name;
    private Integer value;
    public String getName()
    {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public Integer getValue()
    {
        return value;
    }
    public void setValue(Integer value)
    {
        this.value = value;
    }
    public User(String name, Integer value)
    {
        super();
        this.name = name;
        this.value = value;
    }
    
    /**
     * 此方法没有执行.
     */

    @Override
    public boolean equals(Object obj)
    {
        boolean flag = false;
        User user = (User)obj;
        if (this.value == user.value && this.name.compareTo(user.name) == 0)
            flag = true;
        System.out.println("User equals method!");
        return flag;
    }
}


阅读(1291) | 评论(0) | 转发(0) |
0

上一篇:字符类型统计

下一篇:Java序列化算法透析

给主人留下些什么吧!~~