/**
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
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 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;
}
}
* 通过这个程序,测试树映像的使用,表目集合的遍历
*/
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;
}
}