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
郭興華
閱讀(4603)
評論(0)
編輯
收藏
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
管理
Powered by:
BlogJava
Copyright © 郭興華
<
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(4603)
2.?JDBC連接SQLSERVER(1822)
3.?判斷一個一個路徑是否是目錄(1085)
4.?jsp讀取*.TXT(764)
5.?java代理模式(727)
評論排行榜
1.?StudentTest1.java(1)
2.?jsp讀取*.TXT(1)
3.?java1.5注解(二)(0)
4.?java1.5注解(一)(0)
5.?jsp中使用類(0)
主站蜘蛛池模板:
深州市
|
德令哈市
|
黑河市
|
灵宝市
|
秦皇岛市
|
绥化市
|
军事
|
二连浩特市
|
安龙县
|
屯门区
|
南丰县
|
双桥区
|
绥德县
|
旅游
|
玛沁县
|
洛隆县
|
平凉市
|
宜宾县
|
都江堰市
|
徐闻县
|
丰顺县
|
泉州市
|
高陵县
|
内江市
|
通城县
|
青田县
|
综艺
|
松溪县
|
乌鲁木齐县
|
康马县
|
白山市
|
马边
|
麻城市
|
阳泉市
|
义马市
|
鹤山市
|
昌平区
|
宁波市
|
富宁县
|
定边县
|
花莲市
|