主题:[转帖]Java容器类List和Set的分析
txdnet
[专家分:0] 发布于 2008-06-21 15:21:00
Java2容器类类库的用途是"保存对象",它分为两类:
Collection一组独立的元素,通常这些元素都服从某种规则.List必须保持元素特定的顺序,而Set不能有重复元素.
Map一组成对的"键值对"对象,即其元素是成对的对象,最典型的应用就是数据字典,并且还有其它广泛的应用.另外,Map可以返回其所有键组成的Set和其所有值组成的Collection,或其键值对组成的Set,并且还可以像数组一样扩展多维Map,只要让Map中键值对的每个"值"是一个Map即可.
1.迭代器
迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为"轻量级"对象,因为创建它的代价小.
Java中的Iterator功能比较简单,并且只能单向移动:
(1)使用方法iterator()要求容器返回一个Iterator.第一次调用Iterator的next()方法时,它返回序列的第一个元素.
(2) 使用next()获得序列中的下一个元素.
(3) 使用hasNext()检查序列中是否还有元素.
(4) 使用remove()将迭代器新返回的元素删除.
Iterator是Java迭代器最简单的实现,为List设计的ListIterator具有更多的功能,它可以从两个方向遍历List,也可以从List中插入和删除元素.
2.List的功能方法
List(interface):次序是List最重要的特点;它确保维护元素特定的顺序.List为Collection添加了许多方法,使得能够向List中间插入与移除元素(只推荐LinkedList使用).一个List可以生成ListIterator,使用它可以从两个方向遍历List,也可以从List中间插入和删除元素.
ArrayList:由数组实现的List.它允许对元素进行快速随机访问,但是向List中间插入与移除元素的速度很慢.ListIterator只应该用来由后向前遍历ArrayList,而不是用来插入和删除元素,因为这比LinkedList开销要大很多.
LinkedList:对顺序访问进行了优化,向List中间插入与删除得开销不大,随机访问则相对较慢(可用ArrayList代替).它具有方法addFirst(),addLast(),getFirst(),getLast(),removeFirst(),removeLast(),这些方法(没有在任何接口或基类中定义过)使得LinkedList可以当作堆栈,队列和双向队列使用.
3.Set的功能方法
Set(interface):存入Set的每个元素必须是唯一的,因为Set不保存重复元素.加入Set的t必须定义equals()方法以确保对象的唯一性.Set与Collection有完全一样的接口.Set接口不保证维护元素的次序.
HashSet: 为快速查找而设计的Set.存入HashSet的对象必须定义hashCode().
TreeSet: 保持次序的Set,底层为树结构.使用它可以从Set中提取有序的序列.
LinkedHashSet:具有HashSet的查询速度,且内部使用链表维护元素的顺序(插入的次序).于是在使用迭代器遍历Set时,结果会按元素插入的次序显示.
HashSet采用散列函数对元素进行排序,这是专门为快速查询而设计的;TreeSet采用红黑树的数据结构进行排序元素;LinkedHashSet内部使用散列以加快查询速度,同时使用链表维护元素的次序,使得看起来元素是以插入的顺序保存的.需要注意的是,生成自己的类时,Set需要维护元素的存储顺序,因此要实现Comble接口并定义compareTo()方法.
[url=http://www.txdnet.cn/ShowListEssay-2-1-Txdnet.jsp]http://www.txdnet.cn/ShowListEssay-2-1-Txdnet.jsp[/url]
沙发
nana001 [专家分:10] 发布于 2008-06-27 12:55:00
[list]
[*][url=http://www.51cnnet.com/ebook/171-beyond-java]Beyond Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/394-ajax-on-java]Ajax on Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/531-java-garage]JAVA GARAGE free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/639-java-and-soap]Java and SOAP free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/716-hardcore-java]Hardcore Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/831-java-cryptography]Java Cryptography free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/843-java-and-xslt]Java and XSLT free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/860-java-rmi]Java RMI free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/877-java-nio]Java NIO free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/890-java-cookbook]Java Cookbook free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/914-java-foundations]Java Foundations free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1493-bluetooth-for-java]Bluetooth for Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/87-rails-for-java-developers]Rails for Java Developers free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/192-jython-for-java-programmers]Jython for Java Programmers free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/528-java-reflection-in-action]Java Reflection in Action free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/530-fundamental-networking-in-java]Fundamental Networking in Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/616-java-thread-programming]Java Thread Programming free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/630-java-enterprise-in-a-nutshell]Java Enterprise in a Nutshell free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/739-effective-enterprise-java]Effective Enterprise Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/753-java-generics-and-collections]Java Generics and Collections free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/787-java-concurrency-in-practice]Java Concurrency in Practice free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/808-developing-java-beans]Developing Java Beans free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/855-java-pocket-guide]Java Pocket Guide free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/869-java-management-extensions]Java Management Extensions free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/881-java-web-services]Java Web Services free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/898-java-3d-programming]Java 3D Programming free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/960-pro-ajax-and-java]Pro Ajax and Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1054-developing-games-in-java]Developing Games in Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1091-java-programming-on-linux]Java Programming on Linux free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1163-c-for-java-programmers]C# For Java Programmers free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1276-java-data-objects]Java Data Objects free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1279-java-message-service]Java Message Service free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1287-java-performance-tuning]Java Performance Tuning free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1309-java-2d-graphics]Java 2D Graphics free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1315-java-servlet-programming]Java Servlet Programming free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1794-wicked-cool-java]Wicked Cool Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1813-beginning-cryptography-with-java]Beginning Cryptography With Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/721-pro-java-me-mmapi-mobile-media-api-for-java-micro-edition]Pro Java ME MMAPI: Mobile Media API for Java Micro Edition free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1012-java-tm-platform-performance-strategies-and-tactics-the-java-tm-series]Jav(TM) Platform Performance: Strategies and Tactics (The Java(TM) Series) free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1014-java-number-cruncher-the-java-programmer-s-guide-to-numerical-computing]Java Number Cruncher: The Java Programmer's Guide to Numerical Computing free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/1563-java-2-micro-edition-java-in-small-things]Java 2 Micro Edition Java in Small Things free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/37-java-extreme-programming-cookbook]Java Extreme Programming Cookbook free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/71-ground-up-java]Ground-Up Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/183-java-threads-third-edition]Java Threads, Third Edition free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/194-better-faster-lighter-java]Better, Faster, Lighter Java free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/251-java-web-services-in-a-nutshell]Java Web Services in a Nutshell free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/278-java-i-o-2nd-edition]Java I/O, 2nd Edition free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/288-java-servlet-and-jsp-cookbook]Java Servlet And JSP Cookbook free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/292-java-cookbook-2nd-edition]Java Cookbook, 2nd Edition free ebook download[/url]
[*][url=http://www.51cnnet.com/ebook/295-java-in-a-nutshell-5th-edition]Java In A Nutshell, 5th Edition free ebook download[/url]
[/list]