======================================================
注:本文源代码点此下载
======================================================
1. collection: 一组各自独立对象
list: 以特定次序存储一组元素 [原序]
常用举例: arraylist, linkedlist
set: 元素不得重复 [重排序]
常用举例: hashset
最常用界面: add(element)
get()
iterator()
2. map: key-value paris, 也被称为关联式数组(associative array) [重排序]
常用举例: hashmap
最常用界面: put(key, value)
get()
[容器打印]
由各容器缺省的tostring()提供
set和map都具有内部的排列(ordering)机制
[容器缺点: 元素型别未定]
一旦将元素放入容器,它将丧失它的型别信息,都变成了object
这样从容器中取出元素时首先要转换为原有型别,唯有string例外:
编译器会自动调用tostring()函数
[迭代器 iterators]
迭代器是一个对象,其职责是走访以及选择序列(sequence)中的一连串对象
而且迭代器是“轻量级”对象,产生的代价极小
collention.iterator() 返回一个iterator对象
java.util.iterator [class]
next() 取得序列中下一个元素,第一次调用将返回第一个元素
hasnext() 检查序列中是否还有下一个元素
remove() 移去迭代器最新传回的元素
对于list还有一个更复杂的listiterator
java.util.listiterator [class]
add(), remove(), set(),
hasnext(), next(), nextindex(),
hasprevious(), previous(), previousindex()
旧版迭代器为enumeration
[容器分类 container taxonomy]
[collection机能]
boolean add(object)* 若未能将引数加入,则返回false
boolean addall(collection)* 只要引数collection中有一个元素成功能加入就返回true
void clear()* 移除(remove)容器内所有元素
boolean contains(object) 若容器内含引数所代表的对象,返回true
boolean containsall(collection) 若容器内含引数所含的所有元素,返回true
boolean isempty()
iterator iterator()
boolean remove(object)* 若引数值位于容器中,则移出该元素(或其中之一)。若已发生移除动作,则返回true
boolean removeall(collection)* 移除容器内所有元素。动作发生返回true
boolean retainall(collection)* 只保留引数容器内的元素(交集 intersection)。动作发生返回true
int size() 返回容器中元素个数
object[] toarray() 返回一个array,内含容器内所有元素
object[] toarray(object[] a) 同上,但array的中元素型别和引数a的元素型别相同(你仍需自己改变array的型别)
*optional
[list机能]
list 次序(order)是list最重要的特性;特有的listiterator(见前述)
arraylist* 允许快速随机访问;安插/移除发生在list中央位置时效率极差
linkedlist 最佳顺序循环访问,易于安插/移除;随机访问缓慢,特有机能如下:
addfirst(), addlast(), getfirst(), getlast(), removefirst(), removelast()
易于使用linkedlist实现stack, queue, deque
*缺省最佳选择
[set机能]
set [interface] set具有和collection一模一样的interface
加进set的每一元素必须独一无二——即每个元素必须定义equals()以判断独一性
hashset* 一种把查找时间看得很重要的set, 每个元素必须定义hashcode()
treeset 底层结构为tree的一种有序(ordered)set,可从set中萃取出一个带次序性的序列(ordered sequence)
*缺省最佳选择
sortedset [interface] (treeset是其唯一实现)
comparator comparator() 产生一个"被此set所使用"的comparator,或者返回null表示以"自然方式"排序。
object first() 产生lowest element
object last() 产生highest element
sortedset subset(fromelement, toelement) 产生set子集,范围从fromelement(含)到toelement(不含)
sortedset headset(toelement) 产生set子集,所有元素都小于toelement
sortedset tailset(fromelement) 产生set子集,所有元素都大于或等于fromelement
first last
[ 0 1 2 3 4 5 6 7 8 ]
low high
开发自己的型别(type)时注意:
1. set需要以某种方式维护其元素次序,这意味着你必须实现 comparable interface, 并定义compareto()
2. 不要在compareto()中使用简单明了的比较形式: return i1-i2。
这种写法是常见的错误。因为只有i1和i2都是无符号(unsigned, 但java中无此关键字)的int,这种写法才是正确的。面对java中带正负号的int会出错。原因是带正负号的int,其容量不足以表示两个同型数值相减的结果。如果i是一个足够大的正整数,而j是一个绝对值足够大的负整数,那么i-j的结果将会造成溢出,并返回负值,导致错误。
[map机能]
map [interface] 维护key-value的关联性,使你可以用key来查找value
hashmap* 基于hash table的实现,可用于取代hashtable。
在常量时间内安插元素;可通过构造函数,设定capacity, load factor来调整效能。
treemap 基于red-black tree的实现。
结果以排序形式出现(次序由comparable[interface]或者comparator[class]决定);唯一具有submap()的一个map
*缺省最佳选择
map [interface]
put(object key, object value)
get(object key)
containskey(object key)
containsvalue(object value)
sortedmap [interface] (treemap是其唯一实现)
comparator comparator();
object firstkey();
object lastkey();
sortedmap submap(fromkey, tokey);
sortedmap headmap(tokey);
sortedmap tailmap(fromkey);
[容器库: 公用函数]
java 2 容器库: java.util.collections [class]
(不要和collection混!)
enumeration(collection) 产生一个旧式(java 1.0/1.1)的enumeration
max(collection[, comparator]) 未指定comparator时则使用natural comparison
min(collection[, comparator])
reverse() 逆序
copy(list dest, list src)
fill(list list, object o) (只对list有效)替换掉list中原有的元素, 将o的reference复制到list的每个位置上
ncopies(int n, object o) 返回一个"大小为n, 内容不再变动"的list, 其中所有的reference都指向o
产生只读版本:
unmodifiablecollection(collection c)
unmodifiablelist(list l)
unmodifiableset(set s)
unmodifiablemap(map m)
thinking in java 补充容器库: com.bruceeckel.util.collection2 [class]
fill(collection, generator, int) 使用自动产生器generator向容器内加入指定个数元素
除了在 com.bruceeckel.util.arrays2 中定义的 randxxxgenerator [class]还能继续使用外,
(boolean, byte, char, short, int, long, float, double, string)
还重新为map提供了
randstringpairgenerator [class] 产生指定个数的,随机字符串对(string pairs)
stringpairgenerator [class] 将给定的二维字符串数组(2d string arrays)转为字符串对(string pairs)
预定义产生器对象:
rsp randstringpairgenerator的对象,产生10组string pairs
geography stringpairgenerator的对象
countries stringgenerator的对象
capitals stringgenerator的对象
[java 1.0/1.1 旧式容器]
vector 对应旧式迭代器enumeration的容器,"函数名称又长又不好用的arraylist"
elements() 返回enumeration
addelement()
enumeration [interface]
boolean hasmoreelements()
object nextelement()
hashtable 类似hashmap
stack
bitset
======================================================
在最后,我邀请大家参加新浪APP,就是新浪免费送大家的一个空间,支持PHP+MySql,免费二级域名,免费域名绑定 这个是我邀请的地址,您通过这个链接注册即为我的好友,并获赠云豆500个,价值5元哦!短网址是http://t.cn/SXOiLh我创建的小站每天访客已经达到2000+了,每天挂广告赚50+元哦,呵呵,饭钱不愁了,\(^o^)/