EnumSet

xiaoxiao2021-03-01  49

EnumSet是一个与枚举类型一起使用的专用Set实现。枚举set中所有元素都必须来自单个枚举类型(即必须是同类型,且该类型是Enum的子类)。枚举类型在创建set时显式或隐式地指定。枚举set在内部表示为位向量。此表示形式非常紧凑且高效。此类的空间和时间性能应该很好, 足以用作传统上基于int的“位标志”的替换形式,具有高品质、类型安全的优势。 如果指定的collection也是一个枚举set,则批量操作(如containsAll和retainAll)也应运行得非常快。 由iterator方法返回的迭代器按其自然顺序遍历这些元素(该顺序是声明枚举常量的顺序)。 返回的迭代器是弱一致的:它从不抛出ConcurrentModificationException,也不一定显示在迭代进行时发生的任何set修改的效果。 不允许使用null元素。试图插入null元素将抛出NullPointerException。但是, 试图测试是否出现null元素或移除null元素将不会抛出异常。 像大多数collection一样,EnumSet是不同步的。如果多个线程同时访问一个枚举set, 并且至少有一个线程修改该set,则此枚举set在外部应该是同步的。这通常是通过对自然封装该枚举set的对象执行同步操作来完成的。 如果不存在这样的对象,则应该使用Collections.synchronizedSet(java.util.Set)方法来“包装”该set。 最好在创建时完成这一操作,以防止意外的非同步访问:Set<MyEnum>s=Collections.synchronizedSet(EnumSet.noneOf(Foo.class)); 实现注意事项:所有基本操作都在固定时间内执行。虽然并不保证,但它们很可能比其HashSet副本更快。 如果参数是另一个EnumSet实例,则诸如addAll()和AbstractSet.removeAll(java.util.Collection)之类的批量操作 也会在固定时间内执行。<wbr style="line-height:25px">注意1</wbr><wbr style="line-height:25px">:不允许使用null元素。试图插入null元素将抛出NullPointerException。但是,<br style="line-height:25px"> 试图测试是否出现null元素或移除null元素将不会抛出异常。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意2</wbr></span><wbr style="line-height:25px">:EnumSet是不同步的。不是线程安全的。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意3</wbr></span><wbr style="line-height:25px">:EnumSet的本质就为枚举类型定制的一个Set,且枚举set中所有元素都必须来自单个枚举类型。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意4</wbr></span><wbr style="line-height:25px">:关于EnumSet的存储,文档中提到是这样的。“<span style="color:#0000ff; line-height:25px">枚举set在内部表示为位向量。</span>”<br style="line-height:25px"> 我想它应该是用一个bit为来表示的于之对应的枚举变量是否在集合中。<br style="line-height:25px"> 比如:0x1001<br style="line-height:25px"> 假如约定从低位开始,就表示第0个,和第三个枚举类型变量在EnumSet中。<br style="line-height:25px"> 这样的话空间和时间性能也就应该很好。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意5</wbr></span><wbr style="line-height:25px">:至于Enum的枚举常量的位置(序数)可以用Enum的ordinal()方法得到。<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意6</wbr></span><wbr style="line-height:25px">:在jdk内部可以一个数组的形式一个枚举的枚举常量。<br style="line-height:25px"> 下面是来自来JDK中RegularEnumSet.java的一个示例<br style="line-height:25px"><span style="color:#0000ff; line-height:25px">privatestatic<EextendsEnum<E>>E[]getUniverse(Class<E>elementType){<br style="line-height:25px"> returnSharedSecrets.getJavaLangAccess()<br style="line-height:25px"> .getEnumConstantsShared(elementType);<br style="line-height:25px"> }</span><br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意7</wbr></span><wbr style="line-height:25px">:元素属于哪种枚举类型必须在创建set时显式或隐式地指定.<br style="line-height:25px"><span style="line-height:25px"><wbr style="line-height:25px">注意8</wbr></span><wbr style="line-height:25px">:关于枚举类型的更多知识可参考《枚举类型》 <div style="line-height:25px"> <span style="color:#ff6600; line-height:25px">Enumset</span><span style="color:#000080; line-height:25px">是个虚类,我们只能通过它提供的静态方法来返回</span><span style="color:#ff6600; line-height:25px">Enumset</span><span style="color:#000080; line-height:25px">的实现类的实例。</span> </div> <div style="line-height:25px"> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">返回EnumSet的两种不同的实现</span><span style="color:#000080; line-height:25px">:如果EnumSet大小小于64,</span> </div> <div style="line-height:25px; color:rgb(0,0,128)">就返回RegularEnumSet实例(当然它继承自EnumSet),这个EnumSet实际上至用了一个long来存储这个EnumSet。</div> <div style="line-height:25px; color:rgb(0,0,128)">如果 EnumSet大小大于等于64,则返回JumboEnumSet实例,它使用一个long[]来存储。这样做的好处很明显:大多数情况下返回的RegularEnumSet效率比JumboEnumSet高很多。</div> <div style="line-height:25px"> <div style="line-height:25px"> <span style="line-height:25px">公共方法</span><br style="line-height:25px"><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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#allOf(java.lang.Class<E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">allOf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Class.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Class</a><E> elementType)</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"> Creates an enum set filled with all the enum elements of the specified<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">elementType</code>.</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"><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#clone()" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">clone</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"> Creates a new enum set with the same elements as those contained in this enum set.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#complementOf(java.util.EnumSet<E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">complementOf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E> s)</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"> Creates an enum set.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#copyOf(java.util.EnumSet<E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">copyOf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E> s)</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"> Creates an enum set.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#copyOf(java.util.Collection<E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">copyOf</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><E> c)</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"> Creates an enum set.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#noneOf(java.lang.Class<E>)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">noneOf</a></span>(<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Class.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Class</a><E> elementType)</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"> Creates an empty enum set.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E, E, E, E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E e1, E e2, E e3, E e4)</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"> Creates a new enum set, containing only the specified elements.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E, E...)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E start, E... others)</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"> Creates a new enum set, containing only the specified elements.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E e)</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"> Creates a new enum set, containing only the specified element.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E, E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E e1, E e2)</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"> Creates a new enum set, containing only the specified elements.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E, E, E, E, E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E e1, E e2, E e3, E e4, E e5)</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"> Creates a new enum set, containing only the specified elements.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#of(E, E, E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">of</a></span>(E e1, E e2, E e3)</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"> Creates a new enum set, containing only the specified elements.</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">static <Eextends<a rel="nofollow" href="http://developer.android.com/reference/java/lang/Enum.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">Enum</a><E>><a rel="nofollow" href="http://developer.android.com/reference/java/util/EnumSet.html" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">EnumSet</a><E></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/EnumSet.html#range(E, E)" style="color:rgb(0,102,153); line-height:21px; text-decoration:none">range</a></span>(E start, E end)</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"> Creates an enum set containing all the elements within the range defined by<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">start</code>and<code style="line-height:1em; color:rgb(0,112,0); font-family:monospace">end</code>(inclusive).</div> </td> </tr> </tbody></table> </div> </div> </div> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> 实例1 importjava.util.Comparator; importjava.util.EnumSet; importjava.util.Random; importjava.util.concurrent.SynchronousQueue; importjava.util.concurrent.TimeUnit; publicclass Test{ /** *@paramargs */ publicstaticvoid main(String[]args){ System.out.println("EnumSet.noneOf"); EnumSet<Student>set=EnumSet.noneOf(Student.class); set.add(Student.HARRY); set.add(Student.ROBBIE); set.add(Student.ROBIN); for (Studentp:set) System.out.println(p); set.clear(); System.out.println("EnumSet.allOf"); set=EnumSet.allOf(Student.class); for (Studentp:set) System.out.println(p); set.clear(); System.out.println("EnumSet.Ofone"); set=EnumSet.of(Student.ROBIN); for (Studentp:set) System.out.println(p); System.out.println("EnumSet.Oftwo"); set=EnumSet.of(Student.ROBIN,Student.HARRY); for (Studentp:set) System.out.println(p); } } enum Student { ROBIN("robin"), HARRY("harry",40), ROBBIE("robbie"); Stringname; intage; private Student(Stringname) { this(name,0); } private Student(Stringname,intage) { this.name=name; this.age=age; } public StringtoString() { returnname; } }
转载请注明原文地址: https://www.6miu.com/read-4549963.html

最新回复(0)