java中的treemap

/**
 * 通过这个程序,测试树映像的使用,表目集合的遍历
 */

import  java.util.TreeMap;
import  java.util.Map;
import  java.util.Iterator;
import  java.util.Set;

public   class  TreeMapTest
{
    public static void main(String[] args)
    {
        TreeMap set = new TreeMap();
        TreeMapTest test  = new TreeMapTest();
        //生成三个学生实例
        Student tom = new Student("Tom","20020410");
        Student jack = new Student("Jack","20020411");
        Student smith = new Student("Smith","20020412");
        
        set.put("one", tom);
        set.put("two", jack);
        set.put("three", smith);
        
        System.out.println("现在映像中元素的个数是:" + set.size() + "/n");
    
        test.getValue(set);
        
        System.out.println("/n现在设置重复关键字,覆盖原来的对象。/n");
        Student rose = new Student("Rose","20020413");
        System.out.println("覆盖已经存在的关键字时返回值:" + set.put("two", rose) + "/n");
        
        test.getValue(set);
    }

    
    public void getValue(TreeMap set)
    {
        System.out.println("遍历映像中的关键字/值对分别是:");
        Set entries = set.entrySet();//得到关键字/值对的集合
        Iterator iter = entries.iterator();
        while(iter.hasNext())
        {
            Map.Entry entry = (Map.Entry)iter.next();
            String key = (String)entry.getKey();
            Student value = (Student)entry.getValue();
            System.out.print(key);
            System.out.println("/t/t" + value);
        }

    }

}


/**
 * 我们设计的学生基本类
 */

class  Student
{
    private String strName = "";//学生姓名
    private String strNumber = "";//学号
    private String strSex = "";//性别
    private String strBirthday = "";//出生年月
    private String strSpeciality = "";//专业
    private String strAddress = "";//地址

    public Student(String name, String number)
    {
        strName = name;
        strNumber = number;
    }


    public String getStudentName()
    {
        return strName;
    }


    public String getStudentNumber()
    {
        return strNumber;
    }


    public void setStudentSex(String sex)
    {
        strSex = sex;
    }


    public String getStudentSex()
    {
        return strSex;
    }


    public String getStudentBirthday()
        {
        return strBirthday;
    }


    public void setStudentBirthday(String birthday)
    {
        strBirthday = birthday;
    }


    public String getStudentSpeciality()
    {
        return strSpeciality;
    }


    public void setStudentSpeciality(String speciality)
    {
        strSpeciality = speciality;
    }


    public String getStudentAddress()
    {
        return strAddress;
    }


    public void setStudentAddress(String address)
    {
        strAddress = address;
    }


    public String toString()
    {
        String information = "学生姓名=" + strName + ", 学号=" + strNumber;  
        if( !strSex.equals("") )
            information += ", 性别=" + strSex;
        if( !strBirthday.equals(""))
            information += ", 出生年月=" + strBirthday;
        if( !strSpeciality.equals("") )
            information += ", 专业=" + strSpeciality;
        if( !strAddress.equals("") )
            information += ", 籍贯=" + strAddress;
        return information;
    }

}






/**
 * 通过这个程序,测试树映像的使用,表目集合的遍历
 */

import  java.util.TreeMap;
import  java.util.Map;
import  java.util.Iterator;
import  java.util.Set;

public   class  TreeMapTest2
{
    public static void main(String[] args)
    {
        TreeMap set = new TreeMap(new ReverseSort());
        TreeMapTest2 test  = new TreeMapTest2();
        //生成三个学生实例
        Student tom = new Student("Tom","20020410");
        Student jack = new Student("Jack","20020411");
        Student smith = new Student("Smith","20020412");
        
        set.put("one", tom);
        set.put("two", jack);
        set.put("three", smith);
        
        System.out.println("现在映像中元素的个数是:" + set.size() + "/n");
    
        test.getValue(set);
        
        System.out.println("/n现在设置重复关键字,覆盖原来的对象。/n");
        Student rose = new Student("Rose","20020413");
        System.out.println("覆盖已经存在的关键字时返回值:" + set.put("two", rose) + "/n");
        
        test.getValue(set);
    }

    
    public void getValue(TreeMap set)
    {
        System.out.println("遍历映像中的关键字/值对分别是:");
        Set entries = set.entrySet();//得到关键字/值对的集合
        Iterator iter = entries.iterator();
        while(iter.hasNext())
        {
            Map.Entry entry = (Map.Entry)iter.next();
            String key = (String)entry.getKey();
            Student value = (Student)entry.getValue();
            System.out.print(key);
            System.out.println("/t/t" + value);
        }

    }

}


/**
 * 我们设计的学生基本类
 */

class  Student
{
    private String strName = "";//学生姓名
    private String strNumber = "";//学号
    private String strSex = "";//性别
    private String strBirthday = "";//出生年月
    private String strSpeciality = "";//专业
    private String strAddress = "";//地址

    public Student(String name, String number)
    {
        strName = name;
        strNumber = number;
    }


    public String getStudentName()
    {
        return strName;
    }


    public String getStudentNumber()
    {
        return strNumber;
    }


    public void setStudentSex(String sex)
    {
        strSex = sex;
    }


    public String getStudentSex()
    {
        return strSex;
    }


    public String getStudentBirthday()
        {
        return strBirthday;
    }


    public void setStudentBirthday(String birthday)
    {
        strBirthday = birthday;
    }


    public String getStudentSpeciality()
    {
        return strSpeciality;
    }


    public void setStudentSpeciality(String speciality)
    {
        strSpeciality = speciality;
    }


    public String getStudentAddress()
    {
        return strAddress;
    }


    public void setStudentAddress(String address)
    {
        strAddress = address;
    }


    public String toString()
    {
        String information = "学生姓名=" + strName + ", 学号=" + strNumber;  
        if( !strSex.equals("") )
            information += ", 性别=" + strSex;
        if( !strBirthday.equals(""))
            information += ", 出生年月=" + strBirthday;
        if( !strSpeciality.equals("") )
            information += ", 专业=" + strSpeciality;
        if( !strAddress.equals("") )
            information += ", 籍贯=" + strAddress;
        return information;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值