如何使用枚举值初始化HashSet? [英] How to initiate a HashSet with Enumeration Values?

查看:98
本文介绍了如何使用枚举值初始化HashSet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用具有带有带有enumaration参数的HashSet的方法的库,但我无法调用该方法.我认为这个问题可能是我认为最简单的问题,但是我从两天开始就遇到了这个问题.请遵循库中的方法:

I am working with a library with a method with have a HashSet with enumaration parameter, and i ain't able to call that method. I think that question is probably easiest that i think, but i have this problem from two days. Follow the method in library:

public int searchCard(HashSet<CardSlotTypeEnum> var1, int var2, OnCardInfoListener var3) {
    if(var2 >= 0 && var3 != null) {
        u.a().a(var1, var2, var3);
        return 0;
    } else {
        return -2;
    }
}

遵循Enumaration CardSlotTypeEnum:

Follow the enumaration CardSlotTypeEnum:

public enum CardSlotTypeEnum {
    ICC1,
    ICC2,
    ICC3,
    PSAM1,
    PSAM2,
    PSAM3,
    RF,
    SWIPE,
}

遵循我的代码:

reader = deviceEngine.getCardReader();
OnCardInfoListener info = new OnCardInfoListener() {};
HashSet<CardSlotTypeEnum> teste = new HashSet<CardSlotTypeEnum>();

reader.searchCard(teste,60,info);

我在初始化'teste'变量时遇到问题,这种方式是唯一不给我一些错误信息的方法,但是在这种情况下'teste'没有得到任何补偿.

I'm having problems to initialize the 'teste' variable, this way was the only that don't are giving some message of error to me, but in this case the 'teste' aren't receiving nothing.

如果有人可以帮助我,我将不胜感激.

If someone could help me, I would be grateful.

推荐答案

这个库使用的是 HashSet 而不是 EnumSet ,这有点奇怪,但是如果是这样的话你就坚持下去吧.

It is a little strange that this library is using HashSet instead of EnumSet, but if that's what you're stuck with then so be it.

创建一个包含所有 enum 类中的所有值的 EnumSet 非常容易.然后,您可以从中轻松创建 HashSet :

It's very easy to create an EnumSet that has all values in your enum class. You can then create a HashSet quite easily from that:

EnumSet<CardSlotTypeEnum> enums = EnumSet.allOf(CardSlotTypeEnum.class);
HashSet<CardSlotTypeEnum> teste = new HashSet<>(enums);

编辑

如果只想将特定的枚举常量添加到集合中,则只需手动进行即可:

Edit

If you only want to add specific enum constants to the set, you can simply do that manually:

HashSet<CardSlotTypeEnum> teste = new HashSet<>();
teste.add(CardSlotTypeEnum.RF);
teste.add(CardSlotTypeEnum.SWIPE);

这篇关于如何使用枚举值初始化HashSet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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