Java基本概念:集合类 List/Set/Map... 的区别和联系
作者:seiren 日期:2007-04-30 23:53:35
Collection:List、Set
Map:HashMap、HashTable
如何在它们之间选择
一、Array , Arrays
Java所有“存储及随机访问一连串对象”的做法,array是最有效率的一种。
1、
效率高,但容量固定且无法动态改变。
array还有一个缺点是,无法判断其中实际存有多少元素,length只是告诉我们array的容量。
2、Java中有一个Arrays类,专门用来操作array。
arrays中拥有一组static函数,
equals():比较两个array是否相等。array拥有相同元素个数,且所有对应元素两两相等。
fill():将值填入array中。
sort():用来对array进行排序。
binarySearch():在排好序的array中寻找元素。
System.arraycopy():array的复制。
二、Collection , Map
若撰写程序时不知道究竟需要多少对象,需要在空间不足时自动扩增容量,则需要使用容器类库,array不适用。
1、Collection 和 Map 的区别
容器内每个为之所存储的元素个数不同。
Collection类型者,每个位置只有一个元素。
Map类型者,持有 key-value pair,像个小型数据库。
2、各自旗下的子类关系
Collection
--List:将以特定次序存储元素。所以取出来的顺序可能和放入顺序不同。
--ArrayList / LinkedList / Vector
--Set : 不能含有重复的元素
--HashSet / TreeSet
Map
--HashMap
--HashTable
--TreeMap
3、其他特征
* List,Set,Map将持有对象一律视为Object型别。
* Collection、List、Set、Map都是接口,不能实例化。
继承自它们的 ArrayList, Vector, HashTable, HashMap是具象class,这些才可被实例化。
* vector容器确切知道它所持有的对象隶属什么型别。vector不进行边界检查。
三、Collections
Collections是针对集合类的一个帮助类。提供了一系列静态方法实现对各种集合的搜索、排序、线程完全化等操作。
相当于对Array进行类似操作的类——Arrays。
如,Collections.max(Collection coll); 取coll中最大的元素。
Collections.sort(List list); 对list中元素排序
四、如何选择?
1、容器类和Array的区别、择取
* 容器类仅能持有对象引用(指向对象的指针),而不是将对象信息copy一份至数列某位置。
* 一旦将对象置入容器内,便损失了该对象的型别信息。
2、
* 在各种Lists中,最好的做法是以ArrayList作为缺省选择。当插入、删除频繁时,使用LinkedList();
Vector总是比ArrayList慢,所以要尽量避免使用。
* 在各种Sets中,HashSet通常优于HashTree(插入、查找)。只有当需要产生一个经过排序的序列,才用TreeSet。
HashTree存在的唯一理由:能够维护其内元素的排序状态。
* 在各种Maps中
HashMap用于快速查找。
* 当元素个数固定,用Array,因为Array效率是最高的。
结论:最常用的是ArrayList,HashSet,HashMap,Array。
注意:
1、Collection没有get()方法来取得某个元素。只能通过iterator()遍历元素。
2、Set和Collection拥有一模一样的接口。
3、List,可以通过get()方法来一次取出一个元素。使用数字来选择一堆对象中的一个,get(0)...。(add/get)
4、一般使用ArrayList。用LinkedList构造堆栈stack、队列queue。
5、Map用 put(k,v) / get(k),还可以使用containsKey()/containsValue()来检查其中是否含有某个key/value。
HashMap会利用对象的hashCode来快速找到key。
* hashing
哈希码就是将对象的信息经过一些转变形成一个独一无二的int值,这个值存储在一个array中。
我们都知道所有存储结构中,array查找速度是最快的。所以,可以加速查找。
发生碰撞时,让array指向多个values。即,数组每个位置上又生成一个梿表。
6、Map中元素,可以将key序列、value序列单独抽取出来。
使用keySet()抽取key序列,将map中的所有keys生成一个Set。
使用values()抽取value序列,将map中的所有values生成一个Collection。
为什么一个生成Set,一个生成Collection?那是因为,key总是独一无二的,value允许重复。
Map:HashMap、HashTable
如何在它们之间选择
一、Array , Arrays
Java所有“存储及随机访问一连串对象”的做法,array是最有效率的一种。
1、
效率高,但容量固定且无法动态改变。
array还有一个缺点是,无法判断其中实际存有多少元素,length只是告诉我们array的容量。
2、Java中有一个Arrays类,专门用来操作array。
arrays中拥有一组static函数,
equals():比较两个array是否相等。array拥有相同元素个数,且所有对应元素两两相等。
fill():将值填入array中。
sort():用来对array进行排序。
binarySearch():在排好序的array中寻找元素。
System.arraycopy():array的复制。
二、Collection , Map
若撰写程序时不知道究竟需要多少对象,需要在空间不足时自动扩增容量,则需要使用容器类库,array不适用。
1、Collection 和 Map 的区别
容器内每个为之所存储的元素个数不同。
Collection类型者,每个位置只有一个元素。
Map类型者,持有 key-value pair,像个小型数据库。
2、各自旗下的子类关系
Collection
--List:将以特定次序存储元素。所以取出来的顺序可能和放入顺序不同。
--ArrayList / LinkedList / Vector
--Set : 不能含有重复的元素
--HashSet / TreeSet
Map
--HashMap
--HashTable
--TreeMap
3、其他特征
* List,Set,Map将持有对象一律视为Object型别。
* Collection、List、Set、Map都是接口,不能实例化。
继承自它们的 ArrayList, Vector, HashTable, HashMap是具象class,这些才可被实例化。
* vector容器确切知道它所持有的对象隶属什么型别。vector不进行边界检查。
三、Collections
Collections是针对集合类的一个帮助类。提供了一系列静态方法实现对各种集合的搜索、排序、线程完全化等操作。
相当于对Array进行类似操作的类——Arrays。
如,Collections.max(Collection coll); 取coll中最大的元素。
Collections.sort(List list); 对list中元素排序
四、如何选择?
1、容器类和Array的区别、择取
* 容器类仅能持有对象引用(指向对象的指针),而不是将对象信息copy一份至数列某位置。
* 一旦将对象置入容器内,便损失了该对象的型别信息。
2、
* 在各种Lists中,最好的做法是以ArrayList作为缺省选择。当插入、删除频繁时,使用LinkedList();
Vector总是比ArrayList慢,所以要尽量避免使用。
* 在各种Sets中,HashSet通常优于HashTree(插入、查找)。只有当需要产生一个经过排序的序列,才用TreeSet。
HashTree存在的唯一理由:能够维护其内元素的排序状态。
* 在各种Maps中
HashMap用于快速查找。
* 当元素个数固定,用Array,因为Array效率是最高的。
结论:最常用的是ArrayList,HashSet,HashMap,Array。
注意:
1、Collection没有get()方法来取得某个元素。只能通过iterator()遍历元素。
2、Set和Collection拥有一模一样的接口。
3、List,可以通过get()方法来一次取出一个元素。使用数字来选择一堆对象中的一个,get(0)...。(add/get)
4、一般使用ArrayList。用LinkedList构造堆栈stack、队列queue。
5、Map用 put(k,v) / get(k),还可以使用containsKey()/containsValue()来检查其中是否含有某个key/value。
HashMap会利用对象的hashCode来快速找到key。
* hashing
哈希码就是将对象的信息经过一些转变形成一个独一无二的int值,这个值存储在一个array中。
我们都知道所有存储结构中,array查找速度是最快的。所以,可以加速查找。
发生碰撞时,让array指向多个values。即,数组每个位置上又生成一个梿表。
6、Map中元素,可以将key序列、value序列单独抽取出来。
使用keySet()抽取key序列,将map中的所有keys生成一个Set。
使用values()抽取value序列,将map中的所有values生成一个Collection。
为什么一个生成Set,一个生成Collection?那是因为,key总是独一无二的,value允许重复。
平均得分
(0 次评分)
评论: 9 | 查看次数: 2269
- 共有 9 条评论
蜈蚣被蛇咬了,为防毒液扩散必须截肢!蜈蚣想:幸亏偶腿多~!!大夫安慰道:兄弟,想开点,你以后就是蚯蚓了。手机窃听器 .窃听器 .手机监听器 .监听器 .手机窃听器 .窃听器 .手机监听器.监听器.手机窃听器 .窃听器 .手机窃听器 .窃听器 .手机监听器 .监听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .手机窃听器 .窃听器
监听器 手机监听器 监听器 手机监听器 监听器 手机监听器 监听器 手机监听器 监听器 手机监听器 监听器 手机监听器 窃听器 手机窃听器 窃听器 手机窃听器 窃听器 手机窃听器 窃听器 手机窃听器 窃听器 手机窃听器 窃听器 手机窃听器 福彩3d
It was cold in Arathi. des po wow The weather was neve. free online games r agreeable to. free online war games Captain Abel Marsh. gold für wow He was born to the免费网络游戏 nation Stromgarde. gold in wow and had lived amidst the. gold wow rolling hills and rocks of his homeland for nearly forty years. gold wow Yet no matter how. mp3 player long he lived he最新网游 would never get. online games used to the frigid . play war games air that permeated the. po wow countryside during the winter. world of warcraft gold Marsh clutched his cloak closer to his chest in a bid for additional warmth. world of wow He sometimes fancied the idea of spiriting off to some. wow europe land with a warmer more favorable climate. wow geld But any plans he hatched . wow geld to do just that never came to any fruition. wow gold 1000 When he was younger he might have traveled south. wow gold 5000 to the nation of Stormwind, but that was also. wow gold billig a failed plan. wow gold günstig Just as well he thought. wow gold verkaufen better to be freezing. wow level service up north. wow leveling service than a slave to the orcs. wow mein gold de Marsh stepped out of his tent. wow po that overlooked a. wow powerlevel small military camp.
For ten thousand years. wow gold the immortal night elves. 8GB MP3 PLAYER cultivated a druidic society . apple ipod within the shadowed网络游戏 recesses of. buy cheap wow gold Ashenvale Forest. canon digital camera Then the catastrophic. cheap world of warcraft gold invasion of the Burning Legion. digital camera shattered the tranquility of. digital cameras their ancient. dvd player civilization. eve isk Led by the Arch网游Druid Malfurion. ipod Stormrage and the Priestess. ipod nano Tyrande Whisperwind. ipod shuffle the mighty night elves rose. ipod touch to challenge the demonic onslaught. ipods Aided by the newly. mp3 arrived orcs and humans. mp3 player the night elves succeeded. mp3 players in halting the Legion's. mp4 advance and. portable dvd players defeating its master. world of warcraft buy gold the demonlord Archimonde. wow Though victorious. wow gold the night elves were forced. wow leveling to sacrifice their. wow powerleveling cherished immortality. zubehoer mp3 player and watch their beloved forests burn.
In the aftermath of the horrific conflict. wow gold Malfurion and Tyrande helped their. 2GB MP3 PLAYER people rebuild游戏 their shattered villages. wow gold Slowly the night elves began. 4GB MP3 PLAYER to adjust to their mortal existence. wow gold Such an adjustment. buy wow gold was far from easy. wow gold and there were many night elves. buying gold world of warcraft who could not adjust to the prospects of aging. wow gold disease, and fraility. cell phones Seeking to regain魔兽 their immortality. cheap cell phones a number of wayward druids. cheap wow gold conspired to plant a special. cheap wow gold tree that would reestablish. cheap wow gold a link between their spirits. cheapest wow gold and the eternal world. eve isk When Malfurion heard. mp3 players about this plan. phones cell he warned that nature. portable mp3 player would never bless such a selfish act. portable mp3 players Shortly thereafter. sell wow gold Malfurion's spirit was. world of warcraft gold somehow lost within the depths. wow of the Emerald Dream. wow gold Though his fellow druids. wow gold kaufen attempted to find his wandering spirit. wow gold kaufen only his body remained sleeping within his Barrow Den.
When she awakened Sayvil was already hard at work. cheap wow power leveling directing her other assistants. cheapest wow gold rather than healing the. gold wow soldiers herself. HDRO gold her own strength exhausted. level wow from the night before. lord of rings online gold Alireana.最新免费网络游戏good you are awake. lotro gold Come here a moment. lotro gold I have a task for you. or wow Your gifts arewowgold with healing minds. power level rather than bodies as mine is. wowgold I have told you the story. power level of the things done to Niyara. serveur wow I would like you to find her and Messira. wow level and see what you can do for her. wow leveling I leave her care in your hands. wow lvl you will know what. wow lvl is best for her. wowgold when the time comes. wow lvl 60 They currently live on Theramore Island. wow lvl 70 withing the Dustwallow Marsh. wow or Alireana nodded once. wow power leveling I will do my best for her. wow powerlevel If I can find her. wow powerlevel She turned to leave. wow powerleveling turning back to smile at Sayvil. I will not fail you. she walked out of the. makeshift hospital heading.
手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器│窃听器 .手机窃听器 .窃听器 .手机监听器 .监听器 .手机窃听器 .窃听器 .手机监听器.监听器.手机窃听器 .窃听器 .手机监听器 .监听器 .手机窃听器 .窃听器
彼此彼此。。。。。。。。。。。。。。
妈的~~狂补基础中~~。。。
- 共有 9 条评论
发表评论
订阅
上一篇
|

文章来自:
标签: 