java技术博客
jsp博客
BlogJava
首页
新随笔
联系
聚合
管理
数据加载中……
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;
}
}
posted on 2008-11-07 16:08
郭兴华
阅读(4597)
评论(0)
编辑
收藏
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
<
2008年11月
>
日
一
二
三
四
五
六
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
6
统计
随笔 - 84
文章 - 1
评论 - 2
引用 - 0
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(3)
给我留言
查看公开留言
查看私人留言
随笔分类
java每日练习代码
(rss)
TESTARRAY(6)
(rss)
事件模型与事件处理
(rss)
随笔档案
2009年1月 (2)
2008年11月 (14)
2008年10月 (68)
文章档案
2008年10月 (1)
搜索
最新评论
1. re: jsp读取*.TXT
请问 retstr是什么数据类型?String?好像不行哦
--jsp
2. re: StudentTest1.java
看不懂你的意思,代码没有缩进,看着很不习惯那。
--杨爱友
阅读排行榜
1. java中的treemap(4597)
2. JDBC连接SQLSERVER(1820)
3. 判断一个一个路径是否是目录(1084)
4. jsp读取*.TXT(763)
5. java代理模式(726)
评论排行榜
1. StudentTest1.java(1)
2. jsp读取*.TXT(1)
3. java1.5注解(二)(0)
4. java1.5注解(一)(0)
5. jsp中使用类(0)