`
kanwoerzi
  • 浏览: 1642568 次
文章分类
社区版块
存档分类
最新评论

Java集合容器简介

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

相关推荐

Global site tag (gtag.js) - Google Analytics