亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 開發 > Java > 正文

閱讀EnumSet抽象類源碼

2024-07-13 10:13:24
字體:
來源:轉載
供稿:網友

EnumSet

EnumSet是Java枚舉類型的泛型容器,Java既然有了SortedSet、TreeSet、HashSet等容器,為何還要多一個EnumSet<T>呢?答案肯定是EnumSet有一定的特性,舉個例子,EnumSet的速度很快。其他特性就不一一列舉了,畢竟本文的內容不是介紹EnumSet的特性。

專門為枚舉類設計的集合類,所有元素必須是枚舉類型

EnumSet的集合元素是有序的,內部以位向量的形成存儲,因此占用內存小,效率高

不允許加入null元素

源碼

package java.util;import sun.misc.SharedSecrets;public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E>  implements Cloneable, java.io.Serializable{  /**   * 元素類型   */  final Class<E> elementType;  /**   * 通過數組存儲元素    */  final Enum[] universe;  private static Enum[] ZERO_LENGTH_ENUM_ARRAY = new Enum[0];  EnumSet(Class<E>elementType, Enum[] universe) {    this.elementType = elementType;    this.universe  = universe;  }  /**   * 創造一個空的 enum set 并制定其元素類型   * @param elementType the class object of the element type for this enum   *   set   * @throws NullPointerException if <tt>elementType</tt> is null   */  public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) {    Enum[] universe = getUniverse(elementType);    if (universe == null)      throw new ClassCastException(elementType + " not an enum");    if (universe.length <= 64)      return new RegularEnumSet<>(elementType, universe);    else      return new JumboEnumSet<>(elementType, universe);  }  /**   * 創建一個包含所有在指定元素類型的元素的枚舉set   *   * @param elementType the class object of the element type for this enum   *   set   * @throws NullPointerException if <tt>elementType</tt> is null   */  public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {    EnumSet<E> result = noneOf(elementType);    result.addAll();    return result;  }  /**   * Adds all of the elements from the appropriate enum type to this enum   * set, which is empty prior to the call.   */  abstract void addAll();  /**   * 創建一個枚舉設置相同的元素類型與指定枚舉set   *   * @param s the enum set from which to initialize this enum set   * @throws NullPointerException if <tt>s</tt> is null   */  public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {    return s.clone();  }  /**   * 創建一個枚舉集從指定集合初始化,最初包含相同的元素    * @param c the collection from which to initialize this enum set   * @throws IllegalArgumentException if <tt>c</tt> is not an   *   <tt>EnumSet</tt> instance and contains no elements   * @throws NullPointerException if <tt>c</tt> is null   */  public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) {    if (c instanceof EnumSet) {      return ((EnumSet<E>)c).clone();    } else {      if (c.isEmpty())        throw new IllegalArgumentException("Collection is empty");      Iterator<E> i = c.iterator();      E first = i.next();      EnumSet<E> result = EnumSet.of(first);      while (i.hasNext())        result.add(i.next());      return result;    }  }  /**   * 創建一個枚舉集合,其元素與 s 相同   * @param s the enum set from whose complement to initialize this enum set   * @throws NullPointerException if <tt>s</tt> is null   */  public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {    EnumSet<E> result = copyOf(s);    result.complement();    return result;  }  /**   * 1 個元素枚舉集合   *   * @param e the element that this set is to contain initially   * @throws NullPointerException if <tt>e</tt> is null   * @return an enum set initially containing the specified element   */  public static <E extends Enum<E>> EnumSet<E> of(E e) {    EnumSet<E> result = noneOf(e.getDeclaringClass());    result.add(e);    return result;  }  /**   * 2 個元素枚舉集合   *   * @param e1 an element that this set is to contain initially   * @param e2 another element that this set is to contain initially   * @throws NullPointerException if any parameters are null   * @return an enum set initially containing the specified elements   */  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) {    EnumSet<E> result = noneOf(e1.getDeclaringClass());    result.add(e1);    result.add(e2);    return result;  }  /**   * 3 個元素枚舉集合   *   * @param e1 an element that this set is to contain initially   * @param e2 another element that this set is to contain initially   * @param e3 another element that this set is to contain initially   * @throws NullPointerException if any parameters are null   * @return an enum set initially containing the specified elements   */  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) {    EnumSet<E> result = noneOf(e1.getDeclaringClass());    result.add(e1);    result.add(e2);    result.add(e3);    return result;  }  /**   * 4 個元素枚舉集合   * @param e1 an element that this set is to contain initially   * @param e2 another element that this set is to contain initially   * @param e3 another element that this set is to contain initially   * @param e4 another element that this set is to contain initially   * @throws NullPointerException if any parameters are null   * @return an enum set initially containing the specified elements   */  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) {    EnumSet<E> result = noneOf(e1.getDeclaringClass());    result.add(e1);    result.add(e2);    result.add(e3);    result.add(e4);    return result;  }  /**   * 5 個元素枚舉集合   *   * @param e1 an element that this set is to contain initially   * @param e2 another element that this set is to contain initially   * @param e3 another element that this set is to contain initially   * @param e4 another element that this set is to contain initially   * @param e5 another element that this set is to contain initially   * @throws NullPointerException if any parameters are null   * @return an enum set initially containing the specified elements   */  public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4,                          E e5)  {    EnumSet<E> result = noneOf(e1.getDeclaringClass());    result.add(e1);    result.add(e2);    result.add(e3);    result.add(e4);    result.add(e5);    return result;  }  /**   * n 個元素枚舉集合   *   * @param first an element that the set is to contain initially   * @param rest the remaining elements the set is to contain initially   * @throws NullPointerException if any of the specified elements are null,   *   or if <tt>rest</tt> is null   * @return an enum set initially containing the specified elements   */  @SafeVarargs  public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) {    EnumSet<E> result = noneOf(first.getDeclaringClass());    result.add(first);    for (E e : rest)      result.add(e);    return result;  }  /**   * 區間內元素的 枚舉集合   *   * @param from the first element in the range   * @param to the last element in the range   * @throws NullPointerException if {@code from} or {@code to} are null   * @throws IllegalArgumentException if {@code from.compareTo(to) > 0}   * @return an enum set initially containing all of the elements in the   *     range defined by the two specified endpoints   */  public static <E extends Enum<E>> EnumSet<E> range(E from, E to) {    if (from.compareTo(to) > 0)      throw new IllegalArgumentException(from + " > " + to);    EnumSet<E> result = noneOf(from.getDeclaringClass());    result.addRange(from, to);    return result;  }  /**   * Adds the specified range to this enum set, which is empty prior   * to the call.   */  abstract void addRange(E from, E to);  /**   * Returns a copy of this set.   *   * @return a copy of this set   */  public EnumSet<E> clone() {    try {      return (EnumSet<E>) super.clone();    } catch(CloneNotSupportedException e) {      throw new AssertionError(e);    }  }  /**   * Complements the contents of this enum set.   */  abstract void complement();  /**   * Throws an exception if e is not of the correct type for this enum set.   */  final void typeCheck(E e) {    Class eClass = e.getClass();    if (eClass != elementType && eClass.getSuperclass() != elementType)      throw new ClassCastException(eClass + " != " + elementType);  }  /**   * Returns all of the values comprising E.   * The result is uncloned, cached, and shared by all callers.   */  private static <E extends Enum<E>> E[] getUniverse(Class<E> elementType) {    return SharedSecrets.getJavaLangAccess()                    .getEnumConstantsShared(elementType);  }  /**   * This class is used to serialize all EnumSet instances, regardless of   * implementation type. It captures their "logical contents" and they   * are reconstructed using public static factories. This is necessary   * to ensure that the existence of a particular implementation type is   * an implementation detail.   *   * @serial include   */  private static class SerializationProxy <E extends Enum<E>>    implements java.io.Serializable  {    /**     * The element type of this enum set.     *     * @serial     */    private final Class<E> elementType;    /**     * The elements contained in this enum set.     *     * @serial     */    private final Enum[] elements;    SerializationProxy(EnumSet<E> set) {      elementType = set.elementType;      elements = set.toArray(ZERO_LENGTH_ENUM_ARRAY);    }    private Object readResolve() {      EnumSet<E> result = EnumSet.noneOf(elementType);      for (Enum e : elements)        result.add((E)e);      return result;    }    private static final long serialVersionUID = 362491234563181265L;  }  Object writeReplace() {    return new SerializationProxy<>(this);  }  // readObject method for the serialization proxy pattern  // See Effective Java, Second Ed., Item 78.  private void readObject(java.io.ObjectInputStream stream)    throws java.io.InvalidObjectException {    throw new java.io.InvalidObjectException("Proxy required");  }}

總結

以上就是本文關于閱讀EnumSet抽象類源碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!


注:相關教程知識閱讀請移步到JAVA教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
狠狠躁夜夜躁人人爽天天天天97| 亚洲国产欧美精品| 国产精品国产三级国产aⅴ9色| 欧美日在线观看| 一本色道久久88综合亚洲精品ⅰ| 色噜噜狠狠色综合网图区| 久久精品电影网| 亚洲国产又黄又爽女人高潮的| 九九热精品视频| 最近2019年日本中文免费字幕| 久久精品国产亚洲精品2020| 91在线观看免费观看| 中文亚洲视频在线| 国产日韩av高清| 日韩精品极品毛片系列视频| 精品激情国产视频| 国产成人亚洲综合| 亚洲成人激情在线观看| 午夜精品一区二区三区在线视| 色偷偷888欧美精品久久久| 粗暴蹂躏中文一区二区三区| 国产在线精品自拍| 成人欧美一区二区三区在线| 久久精品美女视频网站| 亚洲视频欧美视频| 久久精品国产亚洲7777| 亚洲女人天堂成人av在线| 深夜精品寂寞黄网站在线观看| 91久久久久久久久久| 日韩av一区二区在线观看| 欧美国产日韩xxxxx| 久久青草福利网站| 国产成人精品国内自产拍免费看| 欧美乱大交xxxxx| 久久久久久久久久婷婷| 欧美日韩国产123| 欧美在线中文字幕| 日韩av在线资源| 国产日韩欧美在线视频观看| 夜色77av精品影院| 国产精品美女免费| 亚洲女同性videos| 日韩麻豆第一页| 欧美日本啪啪无遮挡网站| 国产高清视频一区三区| 欧美亚洲视频一区二区| 亚洲电影免费观看高清完整版在线观看| 欧美性视频在线| 日本精品性网站在线观看| 欧美成人精品一区二区| 精品亚洲一区二区三区四区五区| 日韩精品免费综合视频在线播放| 色妞欧美日韩在线| 91精品久久久久久久久| 欧美电影电视剧在线观看| 国产精品久久久久久久久久免费| 日韩h在线观看| 丝袜一区二区三区| 国产伊人精品在线| 亚洲美腿欧美激情另类| 日韩精品在线播放| 亲爱的老师9免费观看全集电视剧| 日韩精品免费一线在线观看| 亚洲在线视频福利| 欧美日韩亚洲视频一区| 激情亚洲一区二区三区四区| 欧美xxxx做受欧美.88| 亚洲精品久久久久久久久久久久久| 久久久久久久久久久亚洲| 国产精品久久国产精品99gif| 亚洲伊人久久大香线蕉av| 午夜精品一区二区三区在线视| 国产极品精品在线观看| 色av吧综合网| 精品国产电影一区| 美日韩精品视频免费看| 国产成人一区二区三区小说| 成人字幕网zmw| 国产激情999| 国产午夜精品全部视频播放| 国产在线久久久| 亚洲一区亚洲二区亚洲三区| 亚洲国产成人爱av在线播放| 欧美夫妻性生活视频| 亚洲第一福利在线观看| 久久久噜噜噜久久中文字免| 亚洲免费影视第一页| 亚洲电影在线观看| 91极品女神在线| 国产激情综合五月久久| 亚洲电影免费观看高清| 97香蕉超级碰碰久久免费软件| 中文字幕久精品免费视频| 91久久久久久久久| 国产精品吹潮在线观看| 超碰97人人做人人爱少妇| 亚洲毛片在线观看.| 日韩精品免费在线视频| www国产亚洲精品久久网站| 国产日韩欧美在线视频观看| 亚洲欧洲在线视频| 欧美视频在线视频| 欧美日韩精品在线观看| 中文字幕国产精品| 亚洲人成啪啪网站| 亚洲欧洲国产精品| 精品中文字幕视频| 国产97在线播放| 亚洲人成伊人成综合网久久久| 亚洲精品美女久久久久| 亚洲第一精品电影| 亚洲精品中文字| 亚洲精品999| 亚洲综合色激情五月| 日本午夜精品理论片a级appf发布| 亚洲自拍欧美另类| 中文字幕精品视频| 姬川优奈aav一区二区| 欧美日韩国产第一页| 久久成人人人人精品欧| 青青草成人在线| 欧美激情第三页| 欧美激情一区二区久久久| 日本成人免费在线| 久久久成人的性感天堂| 久久久精品999| 免费99精品国产自在在线| 成人免费看吃奶视频网站| 国外日韩电影在线观看| 九九精品视频在线观看| 亚洲综合中文字幕在线| 亚洲精品v天堂中文字幕| 国产精品久久久久9999| 欧美日韩国产区| 精品久久久久国产| 日韩中文字幕免费| 91免费在线视频网站| 欧美性猛交xxxx富婆| 欧美大片在线看免费观看| 国产日韩专区在线| 日韩免费中文字幕| 影音先锋欧美精品| 全亚洲最色的网站在线观看| 亚洲护士老师的毛茸茸最新章节| 亚洲精品福利在线观看| 亚洲欧洲xxxx| 日韩在线观看电影| 亚洲福利视频久久| 一区二区三区国产视频| 日本视频久久久| 国产精品日日做人人爱| 日韩在线一区二区三区免费视频| 国产婷婷色综合av蜜臀av| 国产精品网站大全| 日韩色av导航| 亚洲欧美激情一区| 亚洲第一区在线| 日韩国产欧美精品在线| 久久免费福利视频| 992tv成人免费视频| 上原亚衣av一区二区三区| 日韩精品极品在线观看| 国产一区二区三区在线免费观看| 一区二区三区回区在观看免费视频|