您的位置:首页 > 编程语言 > Java开发

Java集合(11)——EnumSet源码解析

2018-03-24 14:06 330 查看

类图



官方文档



(1)EnumSet是一个专为枚举类型设计的类,EnumSet中的所有元素都必须是指定枚举类型的枚举值

(2)EnumSet在内部以位向量的形式进行存储,对于一些批量操作,如containsAll and retainAll,如果其参数是EnumSet类型,则该批量操作的执行速度会很快

(3)通过使用iterator迭代器,可以实现集合中元素的存储顺序与插入元素时的顺序一致

(4)在EnumSet集合类型中,不允许插入null元素,如果插入null 元素,会抛出throw NullPointerException异常,但是可以通过调用remove和包含函数判断是否有null元素

(5)像很多其他Collection类一样,EnumSet类不是synchronized的,如果有多个线程访问该类,并且至少有一个线程修改了该类对象的信息,需要加入外部同步锁

成员变量



成员方法



成员方法源码解析

1.
EnumSet(Class<E>elementType, Enum<?>[] universe)
方法

EnumSet(Class<E>elementType, Enum<?>[] universe) {
this.elementType = elementType;
this.universe    = universe;
}


源码解析:

功能:构造函数

2.
public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)
方法

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);
}


源码解析:

功能:创建一个空的EnumSet集合,其中集合中元素的类型被定义好了,类型为:elementType

3.
public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)
方法

public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) {
EnumSet<E> result = noneOf(elementType);
result.addAll();
return result;
}


源码解析:

功能:创建一个包含指定枚举类里所有枚举值的EnumSet集合。

e815

4.
public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)
方法

public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) {
return s.clone();
}


源码解析:

功能:创建一个指定EnumSet具有相同元素类型、相同集合元素的EnumSet集合

5.
public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)
方法

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;
}
}


源码解析:

功能:使用Collection类型的元素创建Enum集合

源码思路:

(1)首先判断集合c是否是EnumSet类型,如果是,直接调用clone方法,将集合c中的元素克隆出来即可

(2)如果不是EnumSet类型,那首先判断集合c是否为空,如果为空,则抛出异常;如果不为空则使用iterator迭代器,将集合c中的元素依次迭代出来,每遍历一个元素就通过add方法添加到result中

6.
public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)
方法

public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) {
EnumSet<E> result = copyOf(s);
result.complement();
return result;
}


源码解析:

功能:创建一个新的EnumSet集合,该集合中包含的枚举值是s所在的类的枚举值,并且不包括s现有的枚举值

源码思路:

(1)首先通过调用copyOf方法,将结果赋值给result

(2)其次调用complement方法来实现该操作,其中complement方法在EnumSet中是抽象方法

7.
public static <E extends Enum<E>> EnumSet<E> of(E e)
方法

public static <E extends Enum<E>> EnumSet<E> of(E e) {
EnumSet<E> result = noneOf(e.getDeclaringClass());
result.add(e);
return result;
}


源码解析:

功能:创建一个EnumSet类型的对象,该对象中的枚举类型应该与e所在的类的枚举类型一致,并且该对象初始元素有e

8.
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)
方法

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;
}


源码解析:

功能:创建一个EnumSet类型的对象,该对象中的枚举类型应该与e所在的类的枚举类型一致,并且该对象初始元素有e1和e2

9.
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)
方法

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;
}


源码解析:

功能:创建一个EnumSet类型的对象,该对象中的枚举类型应该与e所在的类的枚举类型一致,并且该对象初始元素有e1、e2和e3

10.
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4)
方法

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;
}


源码解析:

功能:创建一个EnumSet类型的对象,该对象中的枚举类型应该与e所在的类的枚举类型一致,并且该对象初始元素有e1、e2、e3和e4

11.
public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5)
方法

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;
}


源码解析:

功能:创建一个EnumSet类型的对象,该对象中的枚举类型应该与e所在的类的枚举类型一致,并且该对象初始元素有e1、e2、e3、e4和e5

12.
public static <E extends Enum<E>> EnumSet<E> of(E first, E... rest)
方法

@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;
}


13.
public static <E extends Enum<E>> EnumSet<E> range(E from, E to)
方法

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;
}


源码解析:

功能:创建一个EnumSet集合,集合中包含E类从from到to的所有枚举值

源码思路:

(1)首先判断from和to的值,需要to的值大于from才能正确执行下面语句,否则抛出异常

(2)其次定义一个EnumSet类型的变量result,设定该变量中的枚举类型是from所在的枚举类型

(3)调用addRange方法,将枚举E中的元素放入result中,其中addRange在该EnumSet类中是抽象方法

示例代码

public class set {
public static void main(String[] args) {
//2. public static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType)方法
EnumSet es1 = EnumSet.noneOf(Dat.class);
System.out.println(es1); //输出[]

//3. public static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType)方法
EnumSet es2 = EnumSet.allOf(Dat.class);
System.out.println(es2); //输出:[YEAR, MONTH, DAY]

//4. public static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s)方法
EnumSet es3 = EnumSet.copyOf(es2);
System.out.println(es3); //输出:[YEAR, MONTH, DAY]

//5. public static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c)方法
Set s = new HashSet();
s.add(Session.SPRIGNG);
s.add(Session.AUTUMN);
EnumSet es4 = EnumSet.copyOf(s);
System.out.println(es4);//输出:[SPRIGNG, AUTUMN]

//6. public static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s)方法
EnumSet es5 = EnumSet.complementOf(es1);
System.out.println(es5); //输出:[YEAR, MONTH, DAY]

//7. public static <E extends Enum<E>> EnumSet<E> of(E e)方法
EnumSet s1 = EnumSet.of(Session.SPRIGNG);
System.out.println(s1);  //输出:[SPRING]

//8. public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2)方法
EnumSet s2 = EnumSet.of(Session.SPRIGNG, Session.SUMMER);
System.out.println(s2); //输出:[SPRING,SUMMER]

//9.public static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3)方法
EnumSet s3 = EnumSet.of(Session.SPRIGNG, Session.SUMMER,Session.AUTUMN);
System.out.println(s3); //输出:[SPRING,SUMMER,AUTUMN]

//10.public static <E extends Enum<E>> EnumSet<E> range(E from, E to)方法
EnumSet s4 = EnumSet.range(Session.SUMMER, Session.WINTER);
System.out.println(s4); //输出:[SUMMER, AUTUMN, WINTER]

}

enum Session {
SPRIGNG,
SUMMER,
AUTUMN,
WINTER
}

enum Dat {
YEAR,
MONTH,
DAY
}
}


注意这里面public static

Set s = new HashSet();
s.add(1);
EnumSet es4 = EnumSet.copyOf(s);
System.out.println(es4);


该代码会产生如下异常:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Enum
at java.util.RegularEnumSet.add(RegularEnumSet.java:18)
at java.util.EnumSet.copyOf(EnumSet.java:154)
at problems2018_3_23.set.main(set.java:27)


感谢

参考:https://blog.csdn.net/wxc880924/article/details/52624887
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: