Java EnumSet

Java EnumSet

枚举或俗称enum 的目的是在编程语言中代表一组命名的常数。例如,一副扑克牌中的4种花色可以是4个名为Club、Diamond、Heart和Spade的枚举器,属于一个名为Suit的枚举类型。

EnumSet 是Set接口的专门实现之一,用于枚举类型。EnumSet的几个重要特征如下。

它扩展了AbstractSet类并实现了Java中的Set接口。

EnumSet类是Java集合框架的一个成员,并且是不同步的。

它是一个高性能的集合实现,比HashSet快很多。

EnumSet中的所有元素都必须来自一个枚举类型,这个枚举类型是在创建集合的时候明确或隐含地指定的。

它不允许 空对象 ,如果我们这样做,会抛出NullPointerException。

它使用一个 故障安全的 迭代器,所以如果在迭代过程中集合被修改,它不会抛出ConcurrentModificationException 。

EnumSet的层次结构如下。

java.lang.Object

↳ java.util.AbstractCollection

↳ java.util.AbstractSet

↳ java.util.EnumSet

这里, E 是存储的元素的类型。

语法: 声明

public abstract class EnumSet>

这里, E 指定了元素。E必须扩展Enum,这就强制要求元素必须是指定的枚举类型。

使用EnumSet的好处

由于它使用 RegularEnumSet 和 JumboEnumSet 实现 , EnumSet中的所有方法都是使用位数运算实现的。

EnumSet比HashSet快,因为我们不需要计算任何hashCode来找到正确的桶。

计算在恒定的时间内执行,所需的空间也非常小。

EnumSet的方法

方法

执行的动作

allOf(Class elementType)

创建一个包含指定元素类型中所有元素的枚举集。

clone()

返回这个集合的一个副本。

complementOf(EnumSet s)

创建一个与指定枚举集的元素类型相同的枚举集,最初包含该类型的所有元素,但不包含在指定的集合中。

copyOf(Collection c)

创建一个从指定集合初始化的枚举集。

copyOf(EnumSet s)

创建一个与指定的枚举集具有相同元素类型的枚举集,最初包含相同的元素(如果有的话)。

noneOf(Class elementType)

创建一个具有指定元素类型的空枚举集。

of(E e)

创建一个最初包含指定元素的枚举集。

of(E e1, E e2)

创建一个最初包含指定元素的枚举集。

of(E first, E… rest)

创建一个最初包含指定元素的枚举集。

of(E e1, E e2, E e3)

创建一个最初包含指定元素的枚举集。

of(E e1, E e2, E e3, E e4)

创建一个最初包含指定元素的枚举集。

of(E e1, E e2, E e3, E e4, E e5)

创建一个最初包含指定元素的枚举集。

range(E from, E to)

创建一个枚举集,最初包含由两个指定端点定义的范围内的所有元素。

实现

// Java Program to Illustrate Working

// of EnumSet and its functions

// Importing required classes

import java.util.EnumSet;

// Enum

enum Gfg { CODE, LEARN, CONTRIBUTE, QUIZ, MCQ };

// Main class

// EnumSetExample

public class GFG {

// Main driver method

public static void main(String[] args) {

// Creating a set

EnumSet set1, set2, set3, set4;

// Adding elements

set1 = EnumSet.of(Gfg.QUIZ, Gfg.CONTRIBUTE,

Gfg.LEARN, Gfg.CODE);

set2 = EnumSet.complementOf(set1);

set3 = EnumSet.allOf(Gfg.class);

set4 = EnumSet.range(Gfg.CODE, Gfg.CONTRIBUTE);

// Printing corresponding elements in Sets

System.out.println("Set 1: " + set1);

System.out.println("Set 2: " + set2);

System.out.println("Set 3: " + set3);

System.out.println("Set 4: " + set4);

}

}

输出

Set 1: [CODE, LEARN, CONTRIBUTE, QUIZ]

Set 2: [MCQ]

Set 3: [CODE, LEARN, CONTRIBUTE, QUIZ, MCQ]

Set 4: [CODE, LEARN, CONTRIBUTE]

操作1: 创建一个EnumSet对象

由于EnumSet是一个抽象类,我们不能直接创建它的实例。它有许多静态工厂方法,可以让我们创建一个实例。JDK提供了 两种不同的EnumSet的实现

RegularEnumSet

JumboEnumSet

注意: 这些都是封装私有的,并由一个比特向量支持。

RegularEnumSet 使用一个long对象来存储EnumSet的元素。long元素的每一个比特都代表一个Enum值。由于long的大小是64位,它可以存储多达64个不同的元素。

JumboEnumSet 使用一个long元素的数组来存储EnumSet的元素。与RegularEnumSet的唯一区别是JumboEnumSet使用一个长数组来存储位向量,从而允许超过64个值。

工厂方法根据元素的数量创建一个实例,EnumSet不提供任何公共构造函数,实例是使用静态工厂方法创建的,如下所示。

allOf(size)

noneOf(size)

range(e1, e2)

of()

示例

if (universe.length <= 64)

return new RegularEnumSet<>(elementType, universe);

else

return new JumboEnumSet<>(elementType, universe);

例子

// Java Program to illustrate Creation an EnumSet

// Importing required classes

import java.util.*;

// Main class

// CreateEnumSet

class GFG {

// Enum

enum Game { CRICKET, HOCKEY, TENNIS }

// Main driver method

public static void main(String[] args)

{

// Creating an EnumSet using allOf()

EnumSet games = EnumSet.allOf(Game.class);

// Printing EnumSet elements to the console

System.out.println("EnumSet: " + games);

}

}

输出

EnumSet: [CRICKET, HOCKEY, TENNIS]

操作 2: 添加元素

我们可以使用add()和addAll()方法向EnumSet添加元素。

例子

// Java program to illustrate Addition of Elements

// to an EnumSet

// Importing required classes

import java.util.EnumSet;

// Main class

class AddElementsToEnumSet {

// Enum

enum Game { CRICKET, HOCKEY, TENNIS }

// Main driver method

public static void main(String[] args)

{

// Creating an EnumSet

// using allOf()

EnumSet games1 = EnumSet.allOf(Game.class);

// Creating an EnumSet

// using noneOf()

EnumSet games2 = EnumSet.noneOf(Game.class);

// Using add() method

games2.add(Game.HOCKEY);

// Printing the elements to the console

System.out.println("EnumSet Using add(): "

+ games2);

// Using addAll() method

games2.addAll(games1);

// Printing the elements to the console

System.out.println("EnumSet Using addAll(): "

+ games2);

}

}

输出

EnumSet Using add(): [HOCKEY]

EnumSet Using addAll(): [CRICKET, HOCKEY, TENNIS]

操作 3: 访问元素

我们可以通过使用iterator()方法 来访问EnumSet中的元素。

例子

// Java program to Access Elements of EnumSet

// Importing required classes

import java.util.EnumSet;

import java.util.Iterator;

// Main class

// AccessingElementsOfEnumSet

class GFG {

// Enum

enum Game { CRICKET, HOCKEY, TENNIS }

// MAin driver method

public static void main(String[] args)

{

// Creating an EnumSet using allOf()

EnumSet games = EnumSet.allOf(Game.class);

// Creating an iterator on games

Iterator iterate = games.iterator();

// Display message

System.out.print("EnumSet: ");

while (iterate.hasNext()) {

// Iterating and printing elements to

// the console using next() method

System.out.print(iterate.next());

System.out.print(", ");

}

}

}

输出

EnumSet: CRICKET, HOCKEY, TENNIS,

操作 4: 删除元素

我们可以使用remove()和removeAll()方法移除元素。

例子

// Java program to Remove Elements

// from a EnumSet

// Importing required classes

import java.util.EnumSet;

// Main class

// RemovingElementsOfEnumSet

class GFG {

// Enum

enum Game { CRICKET, HOCKEY, TENNIS }

// Main driver method

public static void main(String[] args)

{

// Creating EnumSet using allOf()

EnumSet games = EnumSet.allOf(Game.class);

// Printing the EnumSet

System.out.println("EnumSet: " + games);

// Using remove()

boolean value1 = games.remove(Game.CRICKET);

// Printing elements to the console

System.out.println("Is CRICKET removed? " + value1);

// Using removeAll() and storing the boolean result

boolean value2 = games.removeAll(games);

// Printing elements to the console

System.out.println("Are all elements removed? "

+ value2);

}

}

输出

EnumSet: [CRICKET, HOCKEY, TENNIS]

Is CRICKET removed? true

Are all elements removed? true

下面定义了一些其他的类或接口的方法,在某种程度上帮助我们更好地理解AbstractSet类,如下所示。

java.util.AbstractSet类中声明的方法

方法

描述

equals(Object o)

将指定的对象与这个集合进行比较,看是否相等。

hashCode()

返回这个集合的哈希代码值。

removeAll(Collection c) | 从这个集合中移除所有包含在指定集合中的元素(可选操作)。

### java.util.Collection接口中声明的方法

方法 | 描述

—|—

parallelStream() | 返回一个以该集合为源的可能的并行流。

removeIf(Predicate filter) | 删除这个集合中满足给定谓词的所有元素。

stream() | 返回一个以这个集合为源的顺序流。

toArray(IntFunction generator) | 返回一个包含此集合中所有元素的数组,使用提供的生成器函数来分配返回的数组。

### java.lang.Iterable接口中声明的方法

方法 | 描述

—|—

forEach(Consumer action) | 对Iterable中的每个元素执行给定的操作,直到所有的元素都被处理完,或者该操作抛出一个异常。

### java.util.Set接口中声明的方法

方法 | 描述

—|—

add(E e) | 如果指定的元素还没有出现,就把它添加到这个集合中(可选操作)。

addAll(Collection c) | 如果指定的元素还没有出现,就把它添加到这个集合中(可选操作)。

clear() | 移除这个集合中的所有元素(可选操作)。

contains(Object o) | 如果这个集合包含指定的元素,返回true。

containsAll(Collection c)

如果这个集合包含指定集合的所有元素,则返回true。

isEmpty()

如果这个集合不包含任何元素,则返回真。

迭代器()

返回这个集合中的元素的一个迭代器。

remove(Object o)

如果指定的元素存在,从这个集合中删除(可选操作)。

retainAll(Collection c) | 只保留这个集合中包含在指定集合中的元素(可选操作)。

size() | 返回这个集合中元素的数量(它的cardinality)。

spliterator() | 在这个集合中的元素上创建一个Spliterator。

toArray() | 返回一个包含这个集合中所有元素的数组。

toArray(T[] a) | 返回一个包含这个集合中所有元素的数组;返回的数组的运行时类型是指定数组的类型。

### java.util.AbstractCollection类中声明的方法

方法 | 描述

—|—

add(E e) | 确保这个集合包含指定的元素(可选操作)。

addAll(Collection c) | 将指定集合中的所有元素添加到这个集合中(可选操作)。

clear() | 从这个集合中移除所有的元素(可选操作)。

contains(Object o) | 如果这个集合包含指定的元素,返回true。

containsAll(Collection c)

如果这个集合包含了指定集合中的所有元素,返回true。

isEmpty()

如果这个集合不包含任何元素,则返回真。

iterator()

返回这个集合中所包含的元素的迭代器。

remove(Object o)

从这个集合中删除一个指定元素的单个实例,如果它存在的话(可选操作)。

retainAll(Collection c)

只保留这个集合中包含在指定集合中的元素(可选操作)。

toArray()

返回一个包含此集合中所有元素的数组。

toArray(T[] a)

返回一个包含此集合中所有元素的数组;返回的数组的运行时类型是指定数组的类型。

toString()

返回这个集合的字符串表示。

[an error occurred while processing the directive]
Copyright © 2088 迷你世界杯_竞猜世界杯 - xhfzmy.com All Rights Reserved.
友情链接