EnumSet 的真正含义是什么? [英] What does EnumSet really mean?

查看:29
本文介绍了EnumSet 的真正含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例:

import java.util.EnumSet;
import java.util.Iterator;

public class SizeSet {

    public static void main(String[] args) {
        EnumSet largeSize = EnumSet.of(Size.XL,Size.XXL,Size.XXXL);
        for(Iterator it = largeSize.iterator();it.hasNext();){
            Size size = (Size)it.next();
            System.out.println(size);
        }
    }
}


enum Size {
  S, M, L, XL, XXL, XXXL;

}

在这段代码中,我可以理解 Enum 创建了一个 Enum 类型的大小.

In this code I can understand that the Enum creates an Enum type of Sizes.

我的问题是:largeSize 是 EnumSet 类型的对象吗?它的真正含义是什么?我真的很想更好地理解它.

My question is: is largeSize an object of EnumSet type? What does it really mean? I really want to understand it better.

推荐答案

对于任何变量,其类型都可以在其声明中找到:

As for any variable, its type is found in its declaration:

EnumSet largeSize

所以是的,largeSize(应该命名为 largeSizes 因为它是一个集合)是 EnumSet 类型.它也应该被泛化,因此被声明为

So yes, largeSize (which should be named largeSizes since it's a collection) is of type EnumSet. It should also be generified, and thus be declared as

EnumSet<Size> largeSizes

这意味着 largeSizesEnumSet 类型.EnumSet 是一个 Set,它包含特定枚举类型的枚举实例,比其他 Set 实现(如 HashSetTreeSet 等).要了解 EnumSet 是什么,请阅读它的 API.

What it means, is that largeSizes is of type EnumSet. An EnumSet is a Set which contains enum instance of a specific enum type, in a more efficient way than other Set implementations (like HashSet, TreeSet, etc.). To know what an EnumSet is, read its API.

这篇关于EnumSet 的真正含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆