Java EnumSet
1 什么是Java EnumSet
Java EnumSet类是用于枚举类型的专用Set实现。它继承AbstractSet类并实现Set接口。
2 Java EnumSet的层次结构
下图为EnumSet类的层次结构。
3 Java EnumSet的语法
public abstract class EnumSet<E extends Enum<E>> extends AbstractSet<E> implements Cloneable, Serializable
4 Java EnumSet的方法
5 Java EnumSet的例子
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumSet的例子
*/
import java.util.*;
enum days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class Demo {
public static void main(String[] args) {
Set<days> set = EnumSet.of(days.TUESDAY, days.WEDNESDAY);
//遍历元素
Iterator<days> iter = set.iterator();
while (iter.hasNext())
System.out.println(iter.next());
}
}
输出结果为:
TUESDAY
WEDNESDAY
6 Java EnumSet的例子:allOf()和noneOf()
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* Java EnumSet的例子
*/
import java.util.*;
enum days {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public class Demo {
public static void main(String[] args) {
Set<days> set1 = EnumSet.allOf(days.class);
System.out.println("Week Days:"+set1);
Set<days> set2 = EnumSet.noneOf(days.class);
System.out.println("Week Days:"+set2);
}
}
输出结果为:
Week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
Week Days:[]
热门文章
优秀文章