`

java中的treemap

 
阅读更多

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

importjava.util.TreeMap;
importjava.util.Map;
importjava.util.Iterator;
importjava.util.Set;

publicclassTreeMapTest
{
publicstaticvoidmain(String[]args)
{
TreeMapset
=newTreeMap();
TreeMapTesttest
=newTreeMapTest();
//生成三个学生实例
Studenttom=newStudent("Tom","20020410");
Studentjack
=newStudent("Jack","20020411");
Studentsmith
=newStudent("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");
Studentrose
=newStudent("Rose","20020413");
System.out.println(
"覆盖已经存在的关键字时返回值:"+set.put("two",rose)+"/n");

test.getValue(set);
}


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

}

}


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

classStudent
{
privateStringstrName="";//学生姓名
privateStringstrNumber="";//学号
privateStringstrSex="";//性别
privateStringstrBirthday="";//出生年月
privateStringstrSpeciality="";//专业
privateStringstrAddress="";//地址

publicStudent(Stringname,Stringnumber)
{
strName
=name;
strNumber
=number;
}


publicStringgetStudentName()
{
returnstrName;
}


publicStringgetStudentNumber()
{
returnstrNumber;
}


publicvoidsetStudentSex(Stringsex)
{
strSex
=sex;
}


publicStringgetStudentSex()
{
returnstrSex;
}


publicStringgetStudentBirthday()
{
returnstrBirthday;
}


publicvoidsetStudentBirthday(Stringbirthday)
{
strBirthday
=birthday;
}


publicStringgetStudentSpeciality()
{
returnstrSpeciality;
}


publicvoidsetStudentSpeciality(Stringspeciality)
{
strSpeciality
=speciality;
}


publicStringgetStudentAddress()
{
returnstrAddress;
}


publicvoidsetStudentAddress(Stringaddress)
{
strAddress
=address;
}


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

}






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

importjava.util.TreeMap;
importjava.util.Map;
importjava.util.Iterator;
importjava.util.Set;

publicclassTreeMapTest2
{
publicstaticvoidmain(String[]args)
{
TreeMapset
=newTreeMap(newReverseSort());
TreeMapTest2test
=newTreeMapTest2();
//生成三个学生实例
Studenttom=newStudent("Tom","20020410");
Studentjack
=newStudent("Jack","20020411");
Studentsmith
=newStudent("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");
Studentrose
=newStudent("Rose","20020413");
System.out.println(
"覆盖已经存在的关键字时返回值:"+set.put("two",rose)+"/n");

test.getValue(set);
}


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

}

}


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

classStudent
{
privateStringstrName="";//学生姓名
privateStringstrNumber="";//学号
privateStringstrSex="";//性别
privateStringstrBirthday="";//出生年月
privateStringstrSpeciality="";//专业
privateStringstrAddress="";//地址

publicStudent(Stringname,Stringnumber)
{
strName
=name;
strNumber
=number;
}


publicStringgetStudentName()
{
returnstrName;
}


publicStringgetStudentNumber()
{
returnstrNumber;
}


publicvoidsetStudentSex(Stringsex)
{
strSex
=sex;
}


publicStringgetStudentSex()
{
returnstrSex;
}


publicStringgetStudentBirthday()
{
returnstrBirthday;
}


publicvoidsetStudentBirthday(Stringbirthday)
{
strBirthday
=birthday;
}


publicStringgetStudentSpeciality()
{
returnstrSpeciality;
}


publicvoidsetStudentSpeciality(Stringspeciality)
{
strSpeciality
=speciality;
}


publicStringgetStudentAddress()
{
returnstrAddress;
}


publicvoidsetStudentAddress(Stringaddress)
{
strAddress
=address;
}


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

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics