Java集合容器简介

本文深入探讨Java集合框架的各种容器和接口,包括List、Set、Map等核心组件的特性和使用场景,以及线程安全和并发容器的设计原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java集合容器主要有以下几类<wbr>:</wbr>
1,<wbr>内置容器</wbr><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:数组</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(153,51,0); line-height:25px">2,<wbr>list容器</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">Vetor</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">,Stack</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">ArrayList,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">LinkedList</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">CopyOnWriteArrayList(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">AttributeList(1.5)</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">,RoleList(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">RoleUnresolvedList(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">ConcurrentLinkedQueue(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">ArrayBlockingQueue(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">LinkedBlockingQueue(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">PriorityQueue(1.5</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">),</span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">PriorityBlockingQueue(1.5)</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">SynchronousQueue(1.5)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(153,51,0); line-height:25px">3,<wbr>set容器</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">HashSet(1.2)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">LinkedHashSet(1.4)</span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">,TreeSet(1.2)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px"></span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">CopyOnWriteArraySet(1.5)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">EnumSet(1.5)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">JobStateReasons</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(153,51,0); line-height:25px">4,<wbr>map容器</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">Hashtable</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">HashMap(1.2)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">TreeMap(1.2)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">LinkedHashMap(1.4)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">WeakHashMap(1.2)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,<br> I</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">dentityHashMap(1.4</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">),</span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">ConcurrentMap(1.5)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,128,0); line-height:25px">concurrentHashMap(1.5)</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px">注意</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">:</span><span style="font-family:Arial; font-size:14px; color:rgb(153,204,0); line-height:25px">Vector</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(128,128,0); line-height:25px">Stack</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,</span><span style="font-family:Arial; font-size:14px; color:rgb(51,153,102); line-height:25px">Hashtable</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">是Java1.2前的容器。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(0,51,102); line-height:25px">虽然在</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Java2</span><span style="font-family:Arial; font-size:14px; color:rgb(0,51,102); line-height:25px">之前<wbr>,Java是<wbr>没有<wbr>完整的集合框架的。它只有一些简单的可以自扩展的容器类。<br> 但是在</wbr></wbr></wbr></span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Java2</span><span style="font-family:Arial; font-size:14px; color:rgb(0,51,102); line-height:25px">后他们还是被融入到了集合框架的,不过只是历史遗留而已。它们和1.2前应该还是有些变化的,虽然本质没什么变化。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(255,0,255); line-height:25px">Set</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">接口继承于</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">Collection,</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">但不允许重复,使用自己内部的一个排列机制。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(255,0,255); line-height:25px">List</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">接口继承</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Collection</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,允许重复,以元素安插的次序来放置元素,不会重新排列。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(255,0,255); line-height:25px">Map</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">接口是一组成对的键-值对象,即所持有的是</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,255); line-height:25px">key-valuepairs</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">。Map中不能有重复的key。拥有自己的内部排列机制。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px">一、Java1.2之前的容器类库</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">其实在Java2之前,Java是没有完整的集合框架的。它只有一些简单的可以自扩展的容器类,比如Vector,Stack,Hashtable等。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Java1容器类库设计的一个重大失误是竟然没有对容器进行排序的工具。比如你想让Vector容器中的对象按字典顺序进行排序,你就要自己实现。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr>1.1、Vector<br></wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> </span><span style="font-family:Arial; font-size:14px; color:rgb(255,0,255); line-height:25px">java.util.Vector</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">中包含的元素可以通过一个整型的索引值取得,它的大小可以在添加或移除元素时自动增加或缩小。Vector的操作很简单,通过addElement()加入一个对象,用elementAt()取出它,还可以查询当前所保存的对象的个数size();</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">另外还有一个Enumeration类提供了连续操作Vector中元素的方法,这可以通过Vector中的elements()方法来获取一个Enumeration类的对象,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">可以用一个While循环来遍历其中的元素。用hasMoreElements()检查其中是否还有更多的元素。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">用nextElement()获得下一个元素。Enumeration的用意在于使你能完全不用理会你要遍历的容器的基础结构,只关注你的遍历方法,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">这也就使得遍历方法的重用成为可能。由于这种思想的强大功能,所以在Java2中被保留下来,不过具体实现,方法名和内部算法都改变了,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">这就是Java2中的Iterator以及ListIterator类。然而Enumeration的功能却十分有限,比如只能朝一个方向进行,只能读取而不能更改等。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">更多内容请参考《</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792011109113020943/" style="color:rgb(207,121,28); text-decoration:none; line-height:25px">Vector</a></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">》</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr>1.2、Stack<br></wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(255,0,255); line-height:25px">java.util.Stack</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">最常用的操作便是压入和弹出,最后压入的元素最先被弹出。它遵循后进先出(LIFO)原则。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> 在Java中Stack的的用法也很简单,有push()压入一个元素,用pop()弹出一个元素。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"> 更多内容请参考《</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920111093211393/" style="color:rgb(207,121,28); text-decoration:none; line-height:25px">Stack容器</a></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">》</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr>1.3、Hashtable<br></wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Hashtable与Java2中的Map类似,可以看成一种关联或映射数组,可以将两个毫无关系的对象相关联。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">它的基本目标是实现两个对象之间进行关联。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">更多内容请参考《</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920111093659648/" style="color:rgb(207,121,28); text-decoration:none; line-height:25px">Hashtable</a></span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">》</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr>二、Java2中的容器类库<br></wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">自Java1.2之后Java版本统称为Java2,Java2中的容器类库才可以说是一种真正意义上的集合框架的实现。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">基本完全重新设计,但是又对Java1中的一些容器类库在新的设计上进行了保留,这主要是为了向下兼容的目的,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">当用Java2开发程序时,应尽量避免使用它们,Java2的集合框架已经完全可以满足你的需求。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在Java1中容器类库是同步化的,而Java2中的容器类库都是非同步化,这可能是对执行效率进行考虑的结果。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Java2中的集合框架提供了一套设计优良的接口和类,使程序员操作成批的数据或对象元素极为方便。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">这些接口和类有很多对抽象数据类型操作的API,而这是我们常用的且在数据结构中熟知的。例如Maps,Sets,Lists,Arrays等。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">并且Java用面向对象的设计对这些数据结构和算法进行了封装,这就极大的减化了程序员编程时的负担。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">程序员也可以以这个集合框架为基础,定义更高级别的数据抽象,比如栈、队列和线程安全的集合等,从而满足自己的需要。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Java2的集合框架,抽其核心,主要有三类:</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">List</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">(包括List,Queue,BlockingQueue)、</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Set</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">和</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Map</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">。</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">List</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">和</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Set</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">继承了</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">Collection</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">,而Map则独成一体</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px"></span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">初看上去可能会对Map独成一体感到不解,它为什么不也继承Collection呢?但是这种设计是合理的。<br> 一个Map提供了通过Key对Map中存储的Value进行访问,也就是说它操作的都是成对的对象元素,比如put()和get()方法,<br> 而这是一个Set或List所不就具备的。当然在需要时,你可以由</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">keySet()</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">方法或</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">values()</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">方法从一个Map中得到键的Set集或值的Collection集。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">集合框架中还有两个很实用的公用类:</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Collections</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">和</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Arrays</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">。</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">Collections</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">提供了对一个</span><span style="font-family:Arial; font-size:14px; color:rgb(255,153,0); line-height:25px">Collection</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">容器进行诸如排序、复制、查找和填充等一些非常有用的方法,</span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Arrays</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">则是对一个数组进行类似的操作<wbr>。</wbr></span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><wbr>2.1、Collection</wbr></span><wbr style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">Collection接口提供了一组操作成批对象的方法。(它只是个接口)</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">它提供了基本操作如添加、删除。它也支持查询操作如是否为空isEmpty()方法等。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">为了支持对Collection进行独立操作,Java的集合框架给出了一个</span><span style="font-family:Arial; font-size:14px; color:rgb(255,102,0); line-height:25px">Iterator</span><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">,它使得你可以泛型操作一个Collection,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">而不需知道这个Collection的具体实现类型是什么。它的功能与Java1中的Enumeration类似,只是更易掌握和使用,功能也更强大。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在建立集合框架时,Sun的开发团队考虑到需要提供一些灵活的接口,用来操作成批的元素,又为了设计的简便,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">就把那些对集合进行可选操作的方法与基本方法放到了一起。因为一个接口的实现者必须提供对接口中定义的所有方法的实现,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">这就需要一种途径让调用者知道它正在调用的可选方法当前不支持。最后开发团队选择使用一种信号,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">也即抛出一种不支持操作例外(UnsupportedOperationException),如果你在使用一个Collection中遇到一个上述的例外,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">那就意味着你的操作失败,比如你对一个只读Collection添加一个元素时,你就会得到一个不支持操作例外。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">在你实现一个集合接口时,你可以很容易的在你不想让用户使用的方法中抛出UnsupportOperationException来告诉使用者这个方法当前没有实现,</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:26px">UnsupportOperationException是RuntimeException的一个扩展。</span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px"><wbr>另外</wbr></span><span style="font-family:Arial; font-size:14px; color:rgb(153,51,0); line-height:25px">Java2</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">的容器类库还有一种</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,255); line-height:25px">Failfast</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">的机制。比如你正在用一个Iterator遍历一个容器中的对象,<br> 这时另外一个线程或进程对那个容器进行了修改,那么再用next()方法时可能会有灾难性的后果,<br> 而这是你不愿看到的,这时就会引发一个</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,255); line-height:25px">ConcurrentModificationException</span><span style="font-family:Arial; font-size:14px; color:rgb(0,0,128); line-height:25px">例外<wbr>。<br><wbr>这就是fail-fast<wbr>。</wbr></wbr></wbr></span><br style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"><div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 声明的接口</div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <span style="line-height:normal; font-family:arial,sans-serif; font-size:13px"></span> <table id="pubmethods" style="line-height:21px; margin:0px 0px 1em 1em; padding:0px; border-width:0px; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="margin:0px; padding:0px; border-width:0px"> <tr style="margin:0px; padding:0px; border-width:0px"> <th colspan="12" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:rgb(222,232,241)"> Public Methods</th> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#add(E)" style="color:rgb(0,102,153); text-decoration:none">add</a></span>(E object)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Attempts to add<code style="line-height:1em; color:rgb(0,112,0)">object</code>to the contents of this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>(optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 确保容器能持有你传给它的那个参数。如果没有把它加进去,就返回false。(“可选”)</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#addAll(java.util.Collection&lt;?%20extends%20E&gt;)" style="color:rgb(0,102,153); text-decoration:none">addAll</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); text-decoration:none">Collection</a>&lt;?extendsE&gt; collection)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Attempts to add all of the objects contained in<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>to the contents of this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>(optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 加入参数Collection所含的所有元素。只要加了元素,就返回true。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract void</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#clear()" style="color:rgb(0,102,153); text-decoration:none">clear</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Removes all elements from this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>, leaving it empty (optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 清除容器所保存的所有元素。(“可选”)</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#contains(java.lang.Object)" style="color:rgb(0,102,153); text-decoration:none">contains</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>object)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Tests whether this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>contains the specified object.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 如果容器持有参数Object,就返回true。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#containsAll(java.util.Collection&lt;?&gt;)" style="color:rgb(0,102,153); text-decoration:none">containsAll</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); text-decoration:none">Collection</a>&lt;?&gt; collection)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Tests whether this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>contains all objects contained in the specified<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 如果容器持有参数Collection所含的全部元素,就返回true。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#equals(java.lang.Object)" style="color:rgb(0,102,153); text-decoration:none">equals</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>object)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Compares the argument to the receiver, and returns true if they represent the<em>same</em>object using a class specific comparison.</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract int</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#hashCode()" style="color:rgb(0,102,153); text-decoration:none">hashCode</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns an integer hash code for the receiver.</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#isEmpty()" style="color:rgb(0,102,153); text-decoration:none">isEmpty</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns if this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>contains no elements.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 如果容器里面没有保存任何元素,就返回true。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract<a rel="nofollow" href="http://developer.android.com/reference/java/util/Iterator.html" style="color:rgb(0,102,153); text-decoration:none">Iterator</a>&lt;E&gt;</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#iterator()" style="color:rgb(0,102,153); text-decoration:none">iterator</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns an instance of<code style="line-height:1em; color:rgb(0,112,0)"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Iterator.html" style="color:rgb(0,102,153); text-decoration:none; line-height:21px">Iterator</a></code>that may be used to access the objects contained by this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 返回一个可以在容器的各元素之间移动的Iterator。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#remove(java.lang.Object)" style="color:rgb(0,102,153); text-decoration:none">remove</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>object)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Removes one instance of the specified object from this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>if one is contained (optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 删除容器里面某个元素。删过东西,就返回true。(“可选”)</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#removeAll(java.util.Collection&lt;?&gt;)" style="color:rgb(0,102,153); text-decoration:none">removeAll</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); text-decoration:none">Collection</a>&lt;?&gt; collection)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Removes all occurrences in this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>of each object in the specified<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>(optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 删除容器里面所有参数Collection所包含的元素。只要删过东西,就返回true。(“可选”)</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#retainAll(java.util.Collection&lt;?&gt;)" style="color:rgb(0,102,153); text-decoration:none">retainAll</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html" style="color:rgb(0,102,153); text-decoration:none">Collection</a>&lt;?&gt; collection)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Removes all objects from this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>that are not also found in the<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>passed (optional).</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 删除容器中参数Collection所制定之外的所有元素(集合论中“交集”的概念)。如果发生过变化,则返回true。(“可选”)</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract int</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#size()" style="color:rgb(0,102,153); text-decoration:none">size</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns a count of how many objects this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>contains.</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract &lt;T&gt; T[]</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#toArray(T%5B%5D)" style="color:rgb(0,102,153); text-decoration:none">toArray</a></span>(T[] array)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns an array containing all elements contained in this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> <div style="margin:0px; padding:3px 1em 0px; border-width:0px">If the specified array is large enough to hold the elements, the specified array is used,</div> <div style="margin:0px; padding:3px 1em 0px; border-width:0px">otherwise an array of the same type is created. If the specified array is used and is larger than this Collection,</div> <div style="margin:0px; padding:3px 1em 0px; border-width:0px">the array element following the Collection elements is set to null.</div> <div style="margin:0px; padding:3px 1em 0px; border-width:0px">If the implementation has ordered elements it will return the element array in the same order as an iterator would return them.</div> <div style="margin:0px; padding:3px 1em 0px; border-width:0px">toArray(new Object[0]) behaves exactly the same way as toArray() does.</div> </div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object[]</a></nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/Collection.html#toArray()" style="color:rgb(0,102,153); text-decoration:none">toArray</a></span>()</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Returns a new array containing all elements contained in this<code style="line-height:1em; color:rgb(0,112,0)">Collection</code>.</div> </td> </tr> </tbody></table> <br> 注意,这里没有能进行随机访问的get()方法。这是因为Collection有个子接口Set。而Set有它自己的内部顺序(因此随即访问事毫无意义的)。<br> 所以如果你要检查Collection的元素,你就必须使用迭代器。<br> 如下:<br><span style="color:rgb(51,102,255)">Stringkey="key";<br> Stringvalue="Hello";<br> Stringvalue2="Hi";<br></span><span style="color:rgb(0,0,255)">HashMap&lt;String,String&gt;map=newHashMap();</span><br><span style="color:rgb(51,102,255)">map.put(key,value);</span><br><span style="color:rgb(51,102,255)">map.put(key+2,value2);</span><br><span style="color:rgb(51,102,255)">Collection&lt;String&gt;c=map.values();</span><br><span style="color:rgb(51,102,255)"></span><span style="color:rgb(153,51,0)">for</span><span style="color:rgb(51,102,255)">(Stringstr:c)<br> {<br> System.out.println(str);<br> }</span><br><wbr>注意1<wbr>:Collection直接子接口有List&lt;E&gt;,Queue&lt;E&gt;,BlockingQueue&lt;E&gt;,Set&lt;E&gt;,SortedSet&lt;E&gt;,BeanContext,BeanContextServices。<br> 2.2、List<br><span style="color:rgb(255,0,255)">java.util.List</span>接口对Collection进行了简单的扩充<br> 它的具体实现类常用的有<span style="color:rgb(153,204,0)">ArrayList</span>和<span style="color:rgb(128,128,0)">LinkedList</span>。你可以将任何东西放到一个List容器中,并在需要时从中取出。<br><wbr><span style="color:rgb(153,204,0)">ArrayList</span><span style="color:rgb(0,0,128)">从其命名中可以看出它是一种类似数组的形式进行存储,因此它的随机访问速度极快,<br> 而</span><span style="color:rgb(128,128,0)">LinkedList</span><span style="color:rgb(0,0,128)">的内部实现是链表,它适合于在链表中间需要频繁进行插入和删除操作。<br> 在具体应用时可以根据需要自由选择<wbr>。</wbr></span><br><wbr><span style="color:rgb(0,0,128)">前面说的</span><span style="color:rgb(153,51,0)">Enumeration</span><span style="color:rgb(0,0,128)">和</span><wbr><span style="color:rgb(153,51,0)">Iterator</span><span style="color:rgb(0,0,128)">只能对容器进行向前遍历,而</span><span style="color:rgb(255,0,255)">ListIterator</span><span style="color:rgb(0,0,128)">则继承了</span><span style="color:rgb(255,153,0)">Iterator</span><span style="color:rgb(0,0,128)">的思想,并提供了对List进行</span><span style="color:rgb(0,0,255)">双向遍历</span><span style="color:rgb(0,0,128)">的方法<wbr>。</wbr></span><br><span style="color:rgb(255,0,255)">List</span>继承了来自Collection的iterator()来获取这个序列的Iterator<br><span style="color:rgb(255,0,255)">List</span>提供了以两个方法获取这个序列的ListIterator,<br><span style="color:rgb(0,0,255)"></span><span style="color:rgb(153,51,0)">abstract</span><span style="color:rgb(0,0,255)">ListIterator&lt;E&gt;listIterator()<br></span><span style="color:rgb(153,51,0)">abstract</span><span style="color:rgb(0,0,255)">ListIterator&lt;E&gt;listIterator(intlocation)</span></wbr></wbr></wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 关于<span style="color:rgb(153,51,0)">ListIterator</span>的更多内容请参照《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201110951126755/" style="color:rgb(207,121,28); text-decoration:none">ListIterator</a></strong>》</div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <wbr>2.2.1、ArrayList<br><wbr><span style="color:rgb(255,0,255)">java.util.<wbr>ArrayList</wbr></span>是一个用数组实现的List。能进行快速的随机访问,但是往列表中间插入和删除元素的时候比较慢。<wbr><br> 更多内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920111093562177/" style="color:rgb(207,121,28); text-decoration:none">ArrayList</a></strong>》</wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <wbr>2.2.2、LinkedList<br><wbr><span style="color:rgb(255,0,255)">java.util.LinkedList</span>(链表)-是双向链表,每个节点都有两个指针指向上一节点和下一节点。<br> 对顺序访问进行了优化。在List中间插入和删除元素的代价也不高。<br> 随机访问的速度相对较慢。此外它还有addFirst(),addLast(),getFirst(),getLast(),removeFirst()和removeLast()等方法.<br> 你能把它当成栈(stack),队列(queue)或双向队列(deque)来用。<br> 关于<span style="color:rgb(153,51,0)">LinkedList</span>的更多内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920111094934463/" style="color:rgb(207,121,28); text-decoration:none">LinkedList</a></strong>》</wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <wbr>2.2.3、CopyOnWriteArrayList(1.5)<wbr><br><wbr>CopyOnWriteArrayList是ArrayList的一个线程安全的变体<wbr>,<br> 其中所有可变操作(添加、设置,等等)都是通过对基础数组进行一次新的复制来实现的。<br> 这一般需要很大的开销,但是当遍历操作的数量大大超过可变操作的数量时,这种方法可能比其他替代方法更有效。<br> 在不能或不想进行同步遍历,但又需要从并发线程中排除冲突时,它也很有用。<br> “快照”风格的迭代器方法在创建迭代器时使用了对数组状态的引用。此数组在迭代器的生存期内绝不会更改,<br> 因此不可能发生冲突,并且迭代器保证不会抛出ConcurrentModificationException。<br> 自创建迭代器以后,迭代器就不会反映列表的添加、移除或者更改。不支持迭代器上更改元素的操作(移除、设置和添加)。<br> 这些方法将抛出UnsupportedOperationException。<br><wbr>2.2.4、AttributeList(1.5)<br><wbr>AttributeList表示Attribute对象的列表.它继承于ArrayList.为了在ArrayList中插入Attribute对象,而重写超类ArrayList中的相应方法。<br> 它为了确保AttributeList中所包含的对象只是Attribute对象,这是必需的。<br> 这可避免在检索AttributeList中的元素时出现异常。<br><wbr>2.2.5、RoleList(1.5)<br><wbr>RoleList表示角色(Role对象)的列表.它继承于ArrayList.它为了在ArrayList中插入Role对象,而重写超类ArrayList中的相应方法。<br> RoleUnresolvedList(1.5)<br> RoleUnresolvedList表示RoleUnresolved对象的列表.它继承于ArrayList.<br> 它为了在ArrayList中插入RoleUnresolved对象,而重写超类ArrayList中的相应方法。<br><wbr>2.2.6、ConcurrentLinkedQueue(1.5)<br><wbr>一个基于链接节点的、无界的、线程安全的队列。<br> 此队列按照FIFO(先进先出)原则对元素进行排序。队列的头部是队列中时间最长的元素。<br> 队列的尾部是队列中时间最短的元素。新的元素插入到队列的尾部,队列检索操作从队列头部获得元素。<br> 当许多线程共享访问一个公共collection时,ConcurrentLinkedQueue是一个恰当的选择。此队列不允许null元素。<br> 此实现采用了有效的“无等待(wait-free)”算法,该算法基于MagedM.Michael和MichaelL.Scott撰写的<br> 《Simple,Fast,andPracticalNon-BlockingandBlockingConcurrentQueueAlgorithms》中描述的算法。<br> 需要小心的是,与大多数collection不同<wbr>,size方法不是一个固定时间的操作<wbr>。<br> 由于这些队列的异步特性,确定当前元素的数量需要遍历这些元素。<br> 此类及其迭代器实现了Collection和Iterator接口的所有可选方法。<br><wbr>注意1<wbr>:它的<wbr>线程安全不是通过锁来实现的<wbr>。它是通过<wbr>非阻塞算法<wbr>来实现的。<br> 关于非阻塞算法请参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201062941745769/" style="color:rgb(207,121,28); text-decoration:none">Java非阻塞算法简介</a></strong>》,《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920106295154405/" style="color:rgb(207,121,28); text-decoration:none">ConcurrentLinkedQueue原理(上)</a></strong>》和《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201062951055462/" style="color:rgb(207,121,28); text-decoration:none">ConcurrentLinkedQueue原理(下)</a></strong></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 》<br><wbr>注意2<wbr>:此队列不允许null元素<br> 关于ConcurrentLinkedQueue的更加详细参阅《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920106295029175/" style="color:rgb(207,121,28); text-decoration:none">ConcurrentLinkedQueue</a></strong>》<br><wbr>2.2.7、BlockingQueue(1.5)<br><wbr><span style="color:rgb(255,0,255)">BlockingQueue</span>接口是在Queue基础上增加了两个操作,<br> 两个操作是:检索元素时等待队列变为非空,以及存储元素时等待空间变得可用。<br> (试图向已满队列中放入元素会导致放入操作受阻塞,直到BlockingQueue里有新的空间才会被唤醒继续操作。<br> 试图从空队列中检索元素将导致类似阻塞,直到BlocingkQueue进了新货才会被唤醒。)<br> BlockingQueue的实现类有ArrayBlockingQueue,LinkedBlockingQueue,DelayQueue,PriorityBlockingQueue,SynchronousQueue。<br> 关于BlockingQueue及其实现类的详细信息请<wbr>参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107415915820/" style="color:rgb(207,121,28); text-decoration:none">BlockingQueue</a></strong>》<wbr><br><wbr>2.2.8、PriorityQueue<br><wbr><span style="color:rgb(255,0,255)">PriorityQueue</span>是个基于优先级堆的极大优先级队列。<br> 具体参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201110714024297/" style="color:rgb(207,121,28); text-decoration:none">PriorityBlockingQueue</a></strong>》<br><wbr>2.3、Set<br><span style="color:rgb(0,51,102)"></span><span style="color:rgb(255,0,255)">java.util.Set</span><span style="color:rgb(0,51,102)">接口也是Collection的一种扩展,它的接口都是来自Collection或Iterable,它自己并没新增加接口.<br> Set是一个不包含重复元素的collection。更正式地说,set不包含满足e1.equals(e2)的元素对e1和e2<wbr>,</wbr></span><br> 并且最多包含一个null元素。正如其名称所暗示的,此接口模仿了数学上的set抽象。<br> 在所有构造方法以及add、equals和hashCode方法的协定上,Set接口还加入了其他规定,<br> 这些规定超出了从Collection接口所继承的内容。出于方便考虑,<br> 它还包括了其他继承方法的声明(这些声明的规范已经专门针对Set接口进行了修改,但是没有包含任何其他的规定)。<br> 对这些构造方法的其他规定是(不要奇怪),所有构造方法必须创建一个不包含重复元素的set(正如上面所定义的)。<br> 如果将可变对象用作set元素,那么必须极其小心。如果对象是set中某个元素,<br> 以一种影响equals比较的方式改变对象的值,那么set的行为就是不确定的。<br> 此项禁止的一个特殊情况是不允许某个set包含其自身作为元素。<br> 某些set实现对其所包含的元素有所限制。例如,某些实现禁止null元素,而某些则对其元素的类型所有限制。<br> 试图添加不合格的元素会抛出未经检查的异常,通常是NullPointerException或ClassCastException。<br> 试图查询不合格的元素是否存在可能会抛出异常,也可能简单地返回false;某些实现会采用前一种行为,<br> 而某些则采用后者。概括地说,试图对不合格元素执行操作时,如果完成该操作后不会导致在set中插入不合格的元素,<br> 则该操作可能抛出一个异常,也可能成功,这取决于实现的选择。此接口的规范中将这样的异常标记为“可选”。<br><wbr>注意1<wbr>:Set是一个不包含重复元素的collection。更正式地说,set不包含满足e1.equals(e2)的元素对e1和e2。<br><wbr>注意2<wbr>:与List不同的是,在Set中的对象元素不能重复,也就是说你不能把同样的东西两次放入同一个Set容器中。<br><wbr>2.3.1、HashSet<br><span style="color:rgb(255,0,255)">java.util.HashSet</span>类实现了Set接口,它由哈希表(实际上是一个HashMap实例)支持。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 更多内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201110963730265/" style="color:rgb(207,121,28); text-decoration:none">HashSet</a></strong>》</div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <wbr>2.3.2、SortedSet及TreeSet(1.5):<wbr><br><span style="color:rgb(255,0,255)">SortedSet</span>是个接口,它里面的中的元素一定是有序的.TreeSet是SortedSet的唯一实现。TreeSet是一个有序的Set,<br> 它用了一种叫红黑树的数据结构【red-blacktreedatastructure】来为元素排序。<br> 这样你就能从Set里面提取一个有序序列了。<br> TreeSet将放入其中的元素按序存放,这就要求你放入其中的对象是可排序的,<br> 这就用到了集合框架提供的另外两个实用类Comparable和Comparator。一个类是可排序的,它就应该实现Comparable接口。<br> 有时多个类具有相同的排序算法,那就不需要在每分别重复定义相同的排序算法,只要实现Comparator接口即可。<br><wbr>更多详细信息参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107523524105/" style="color:rgb(207,121,28); text-decoration:none">SortedSet和TreeSet</a></strong>》</wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 2.3.3、LinkedHashSet(JDK1.4):<wbr><br><span style="color:rgb(255,0,255)">LinkedHashSet</span>是一个在内部使用链表的Set,既有HashSet的查询速度,又能保存元素被加进去的顺序(插入顺序)。<br> 用Iterator遍历Set的时候,它是按插入顺序进行访问的。<br> 此实现与HashSet的不同之外在于,后者维护着一个运行于所有条目的双重链接列表。<br> 更多详细参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107542927169/" style="color:rgb(207,121,28); text-decoration:none">LinkedHashSet</a></strong>》<br> 小结:<br><wbr><span style="color:rgb(255,102,0)">HashSet</span><span style="color:rgb(0,0,128)">保存对象的顺序是和</span><span style="color:rgb(0,0,255)">TreeSet</span><span style="color:rgb(0,0,128)">和</span><span style="color:rgb(0,0,255)">LinkedHashSe</span><span style="color:rgb(0,0,128)">t不一样的。这是因为它们是用不同的方法来存储和查找元素的<wbr>。</wbr></span><br><span style="color:rgb(153,204,0)">TreeSet</span>用了一种叫<wbr>红黑树的数据结构<wbr>【red-blacktreedatastructure】来为元素排序,<br> 而<span style="color:rgb(128,128,0)">HashSet</span>则用了“<wbr>专为快速查找而设计<wbr>”的散列函数。<span style="color:rgb(51,153,102)">LinkedHashSet</span>在内部用散列来提高查询速度,<br> 它只是用链表来保存元素的插入顺序的。<br><wbr>2.3.4、CopyOnWriteArraySet<wbr><br><span style="color:rgb(255,0,255)">CopyOnWriteArraySet</span>是对其所有操作使用CopyOnWriteArrayList的Set。<br> 因此,它共享以下相同的基本属性:<br><span style="color:rgb(0,0,128)">*它最适合于set大小通常保持很小、只读操作远多于可变操作以及需要在遍历期间防止线程间冲突的应用程序。<br> *它是线程安全的。<br> *因为通常需要复制整个基础数组,所以可变操作(添加、设置、移除,等等)的开销巨大。<br> *迭代器不支持可变移除操作。<br> *使用迭代器进行遍历的速度很快,并且不会与其他线程发生冲突。在构造迭代器时,迭代器依赖于不变的数组快照。</span><br> 示例用法。<br> 以下代码使用了一个写时复制(copy-on-write)的set,以维护在状态更新时执行某项操作的一组Handler对象。<br><span style="color:rgb(51,102,255)"></span><span style="color:rgb(153,51,0)">class</span><span style="color:rgb(51,102,255)"></span><span style="color:rgb(255,102,0)">Handler</span><span style="color:rgb(51,102,255)">{voidhandle();...}<br></span><span style="color:rgb(153,51,0)">class</span><span style="color:rgb(255,102,0)">X</span><span style="color:rgb(51,102,255)">{<br></span><span style="color:rgb(153,51,0)">privatefinal</span><span style="color:rgb(51,102,255)">CopyOnWriteArraySet&lt;Handler&gt;handlers=newCopyOnWriteArraySet&lt;Handler&gt;();<br></span><span style="color:rgb(153,51,0)">publicvoid</span><span style="color:rgb(51,102,255)">addHandler(Handlerh){handlers.add(h);}<br> privatelonginternalState;<br> privatesynchronizedvoidchangeState(){internalState=...;}<br></span><span style="color:rgb(153,51,0)">publicvoid</span><span style="color:rgb(51,102,255)">update(){<br> changeState();<br> for(Handlerhandler:handlers)<br> handler.handle();<br> }<br> }</span><br><wbr>注意1<wbr>:CopyOnWriteArraySet是对其所有操作使用CopyOnWriteArrayList的Set。<br> 它共享了CopyOnWriteArrayList的基本属性.<br><wbr>注意2<wbr>:它是线程安全的。<br><wbr>注意3<wbr>:感觉CopyOnWriteArraySet和CopyOnWriteArrayList最大的区别就是Set不允许重复的元素。<br> 可能这正是opyOnWriteArraySet存在的意义。<br> 2.3.5、EnumSet(1.5)<br> EnumSet的本质就为枚举类型定制的一个Set,且枚举set中所有元素都必须来自单个枚举类型。<br> 枚举set中所有元素在内部表示为位向量.此表示形式非常紧凑且高效。此类的空间和时间性能应该很好.<br> 详细见《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107611652533/" style="color:rgb(207,121,28); text-decoration:none">EnumSet</a></strong>》<br> 2.3.6、JobStateReasons<br> JobStateReasons类是打印属性类,它是一个枚举值集合,提供了有关作业当前状态的额外信息,即扩充作业的JobState属性值的信息。<br> 注意1:此类在android并没有。所以没有对它的研究。请忽略。<br><wbr>2.4、Map(1.2)<wbr><br> publicinterfaceMap&lt;K,V&gt;<br><span style="color:rgb(0,51,102)">将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。<br> 此接口代替Dictionary类,后者完全是一个抽象类,而不是一个接口。</span><br> Map接口提供三种collection视图,允许以键集、值集合或键-值映射关系集的形式查看某个映射的内容。<br> 映射的顺序定义为迭代器在映射的collection视图中返回其元素的顺序。<br> 某些映射实现可明确保证其顺序,如TreeMap类;某些映射实现则不保证顺序,如HashMap类。<br> 注:将可变对象用作映射键时必须格外小心。当对象是映射中某个键时,如果做了会影响equals比较的修改,<br> 则映射的行为就不是特定的。此项禁止的一个特殊情况是不允许某个映射包含其自身作为键。<br> 虽然允许某个映射包含其自身作为值,但建议要格外小心:在这样的映射上将无法再定义equals和hashCode方法。<br> 所有通用的映射实现类应该提供两个“标准的”构造方法:<br><span style="color:rgb(0,0,128)">一个void(无参数)构造方法,用于创建空映射,<br> 另一个是带有Map类型单参数的构造方法,用于创建一个与其参数具有相同键-值映射关系的新映射。</span><br> 实际上,后一个构造方法允许用户复制任意映射,生成所需类的一个等价映射。尽管无法强制执行该建议(因为接口不能包含构造方法),<br> 但是JDK中所有通用的映射实现都遵从它。<br> 该接口中包含的“破坏性”方法可修改其所操作的映射,如果此映射不支持该操作,则指定这些方法抛出UnsupportedOperationException。<br> 如果是这样,那么在调用对该映射无效时,这些方法可能,但并不一定抛出UnsupportedOperationException。例如,<br> 如果某个不可修改的映射(其映射关系是“重叠”的)为空,则对该映射调用putAll(Map)方法时,可能但并不一定抛出异常。<br> 某些映射实现对可能包含的键和值有所限制。例如,某些实现禁止空键和空值,而某些实现则对其键的类型有限制。<br> 尝试插入不合格的键或值会抛出一个未经检查的异常,<br> 通常是NullPointerException或ClassCastException。<br> 试图查询是否存在不合格的键或值可能抛出异常,或者只是简单地返回false;<br> 某些实现将表现出前一种行为,而某些实现则表现后一种。较为常见的是,<br> 试图对不合格的键或值执行操作且该操作的完成不会导致不合格的元素被插入映射中时,将可能抛出一个异常,<br> 也可能操作成功,这取决于实现本身。这样的异常在此接口的规范中标记为“可选”。<br> 此接口是JavaCollectionsFramework的成员。<br> CollectionsFramework接口中的很多方法是根据equals方法定义的。<br> 例如,contains(Objectkey)方法的规范声明:<br> “当且仅当此映射对于键k包含以下映射关系时才返回true:(key==null?k==null:key.equals(k))”。<br> 不应将此规范解释为它暗指调用具有非空参数key的Map.containsKey会导致对任意的键k调用key.equals(k)。<br> 可随意对各种实现执行优化,只要避免调用equals即可,<br> 例如,通过首先比较两个键的哈希码(Object.hashCode()规范保证哈希码不相等的两个对象不会相等)。<br> 较为常见的是,各种CollectionsFramework接口的实现可随意利用基础Object方法的指定行为,<br> 而不管实现程序认为它是否合适。<br> Map的功能:<br> 所以从某种意义上讲,<wbr><span style="color:rgb(0,0,128)">它是将数字和对象关联起来<wbr>。</wbr></span><br> 它就是用另一个对象(键)来查找对象!这是一种至关重要的编程技巧。<br> 这一概念在Java中表现为Map。put(Objectkey,Objectvalue)方法会往Map里面加一个值,<br> 并且把这个值同键(你查找时所用的对象)联系起来。给出键之后,get(Objectkey)就会返回与之相关的值。<br> 你也可以用containsKey()和containsValue()测试Map是否包含有某个键或值。<br> Java标准类库里有好几种Map:HashMap,TreeMap,LinkedHashMap,WeakHashMap,以及IdentityHashMap。它们都实现了Map的基本接口,<br><wbr>注意1<wbr>:Map的本质就是将键映射到值(对象),这里的键也是个对象(本质上是使用它的hashCode())。<br><br><wbr>2.4.1、HashMap(1.2)</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <span style="color:rgb(255,0,255)">HashMap</span>是基于哈希表的Map接口的实现。此实现提供所有可选的映射操作,并允许使用null值和null键。<br> (除了不同步和允许使用null之外,HashMap类与Hashtable大致相同。)此类不保证映射的顺序,特别是它不保证该顺序恒久不变。<br> 此实现不是同步的。不是线程安全的。<br> 更多请阅读<wbr>《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107624957373/" style="color:rgb(207,121,28); text-decoration:none">HashMap</a></strong>》。<wbr><br><wbr>2.4.2、SortedMap及TreeMap(1.2)<wbr><br><span style="color:rgb(255,0,255)">SortedMap</span>保证按照键的升序排列的映射。TreeMap是SortedMap接口的基于红黑树的实现。<br> 详细见<wbr>《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107642243291/" style="color:rgb(207,121,28); text-decoration:none">SortedMap和TreeMap</a></strong>》<wbr><br><wbr>2.4.3、LinkedHashMap(1.4)<wbr><br><span style="color:rgb(255,0,255)">LinkedHashMap</span>是Map接口的哈希表和链接列表实现,具有可预知的迭代顺序。<br> 此实现与HashMap的不同之处在于,后者维护着一个运行于所有条目的双重链接列表。<br> 此链接列表定义了迭代顺序,该迭代顺序通常就是将键插入到映射中的顺序(插入顺序)。<br> 它的映射很适合构建LRU缓存。<br> 具体参见<wbr>《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010794155535/" style="color:rgb(207,121,28); text-decoration:none">LinkedHashMap</a></strong>》<br> 2.4.4、WeakHashMap(1.2)<wbr><br><span style="color:rgb(255,0,255)">WeakHashMap</span>是一个weakkey的Map,是为某些特殊问题而设计的。它能让Map释放其所持有的对象。<br> 如果某个对象除了在Map当中充当键之外,在其他地方都没有其reference的话,那它将被当作垃圾回收。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> 详细内容请参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/1710405792010794452203/" style="color:rgb(207,121,28); text-decoration:none">WeakHashMap</a></strong>》</div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <wbr>2.4.5、IdentityHashMap(JDK1.4)<br><wbr><span style="color:rgb(255,0,255)">IdentityHashMap</span>类利用哈希表实现Map接口,比较键(和值)时使用引用相等性代替对象相等性。<br> 换句话说,在IdentityHashMap中,当且仅当(k1==k2)时,才认为两个键k1和k2相等(在正常Map实现(如HashMap)中,<br> 当且仅当满足下列条件时才认为两个键k1和k2相等:(k1==null?k2==null:e1.equals(e2)))。<br> 2.4.6、ConcurrentMap(1.5)<br><span style="color:rgb(255,0,255)">ConcurrentMap</span>是接口。它提供了putIfAbsent、remove、replace方法的Map。<br> 定义的接口有:</wbr></wbr> </div> <div style="color:rgb(51,51,51); font-family:Arial; font-size:14px; line-height:25px"> <span style="line-height:normal; font-family:arial,sans-serif; font-size:13px"></span> <table id="pubmethods" style="line-height:21px; margin:0px 0px 1em 1em; padding:0px; border-width:0px; font-size:0.9em; border-collapse:collapse; empty-cells:show; width:933px"><tbody style="margin:0px; padding:0px; border-width:0px"> <tr style="margin:0px; padding:0px; border-width:0px"> <th colspan="12" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:rgb(222,232,241)"> Public Methods</th> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract V</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentMap.html#putIfAbsent(K,%20V)" style="color:rgb(0,102,153); text-decoration:none">putIfAbsent</a></span>(K key, V value)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> If the specified key is not already associated with a value, associate it with the given value.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 如果指定键已经不再与某个值相关联,则将它与给定值关联。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentMap.html#remove(java.lang.Object,%20java.lang.Object)" style="color:rgb(0,102,153); text-decoration:none">remove</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>key,<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Object.html" style="color:rgb(0,102,153); text-decoration:none">Object</a>value)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Removes the entry for a key only if currently mapped to a given value.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 只有存在给定键到给定对象的条目映射时,才移除该键的条目。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px; background-color:rgb(246,246,246)"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract boolean</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentMap.html#replace(K,%20V,%20V)" style="color:rgb(0,102,153); text-decoration:none">replace</a></span>(K key, V oldValue, V newValue)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Replaces the entry for a key only if currently mapped to a given value.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 只有存在给定键到给定对象的条目映射时,才替换该键的条目。</div> </td> </tr> <tr style="margin:0px; padding:0px; border-width:0px"> <td style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); text-align:right; vertical-align:top; background-color:inherit"> <nobr>abstract V</nobr> </td> <td width="100%" style="margin:0px; padding:6px 12px; border:1px solid rgb(204,204,204); vertical-align:top; background-color:inherit"> <nobr><span style="margin-right:2px"><a rel="nofollow" href="http://developer.android.com/reference/java/util/concurrent/ConcurrentMap.html#replace(K,%20V)" style="color:rgb(0,102,153); text-decoration:none">replace</a></span>(K key, V value)</nobr><div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> Replaces the entry for a key only if currently mapped to some value.</div> <div style="line-height:25px; margin:0px; padding:3px 1em 0px; border-width:0px"> 只有存在给定键到给定对象的条目映射时,才替换该键的条目。</div> </td> </tr> </tbody></table> <br> 注意1:ConcurrentMap只有一个实现concurrentHashMap。<br><wbr>2.4.7、concurrentHashMap(1.5)<br><wbr><wbr><span style="color:rgb(255,0,255)">concurrentHashMap</span>是线程安全的,但检索操作不必锁定,不会受阻塞<wbr>,<br> 因此,可能与更新操作交迭(包括put和remove)。ConcurrentHashMap允许多个修改操作并发进行,其关键在于使用了锁分离技术。它使用了多个锁来控制对hash表的不同部分进行的修改。<br> 通过可选的concurrencyLevel构造方法参数(默认值为16)来设置更新操作之间的并发数。<br> 更多参考《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/171040579201071041610915/" style="color:rgb(207,121,28); text-decoration:none">ConcurrentHashMap</a></strong>》和《<strong><a title="阅读全文" target="_blank" href="http://hubingforever.blog.163.com/blog/static/17104057920107104948143/" style="color:rgb(207,121,28); text-decoration:none">ConcurrentHashMap实现细节</a></strong>》<br><wbr>2.4.8、EnumMap(1.5)<wbr><br><wbr><span style="color:rgb(255,0,255)">EnumMap</span>是与枚举类型键一起使用的专用Map实现。枚举映射中所有键都必须来自单个枚举类型,该枚举类型在创建映射时显式或隐式地指定<wbr>。<br> 枚举映射在内部表示为数组。此表示形式非常紧凑且高效。得到元素也很快。<br> 它内部元素是有序的。迭代进行时返回的映射的顺序就是其枚举类型键在枚举声明中的位置顺序。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
### Java 集合容器的使用原理 #### 什么是Java集合框架? Java集合框架是一个用于表示和操作数据组的标准库,它提供了多种接口和类来存储、检索以及管理一组对象。这些容器可以动态调整大小并提供灵活的数据结构支持[^1]。 #### 主要分类 Java集合主要分为两大类别:Collection 和 Map 接口下的子集。 - **Collection** 是单列数据的顶级父接口,其下又细分为 List, Set 及 Queue。 - **List**: 存储有序可重复元素,典型代表有 ArrayList 和 LinkedList。 ```java List<String> list = new ArrayList<>(); list.add("Element"); System.out.println(list.get(0)); // 输出 "Element" ``` - **Set**: 不允许存储重复元素,常用 HashSet 或 TreeSet 实现去重功能。 ```java Set<Integer> set = new HashSet<>(); set.add(1); set.add(2); boolean isAdded = set.add(1); // 返回 false 表明未成功添加因为已存在该元素 ``` - **Queue**: 提供先进先出(FIFO)队列行为,PriorityQueue 支持优先级排序。 ```java Queue<Character> queue = new PriorityQueue<>(); queue.offer('a'); char firstChar = queue.poll(); // 移除并返回 'a' ``` - **Map** 则用来保存键值对映射关系,其中 HashMap 和 ConcurrentHashMap 是两种重要变体[^2]。 - **HashMap**: 非线程安全的基础哈希表实现,在多线程环境下可能导致数据不一致问题。 ```java Map<String, Integer> map = new HashMap<>(); map.put("key", 100); int value = map.get("key"); // 获取对应 key 的 value 值 ``` - **ConcurrentHashMap**: 线程安全版本适合高并发场景应用。JDK7采用分段锁机制提升效率;而到了JDK8则改用了CAS算法配合红黑树优化内部结构以进一步增强性能表现。 #### 性能考量最佳实践 当选用具体类型的集合时需考虑实际应用场景的要求如访问速度、内存占用量等因素从而做出合理决策。例如对于频繁读取但很少修改的操作建议使用不可变或者只读视图形式呈现给外部调用者降低潜在风险同时提高运行效能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值