Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Enum类中存储值的映射集合,以便更好地访问_Java_Hash_Enums_Hashmap - Fatal编程技术网

Java 在Enum类中存储值的映射集合,以便更好地访问

Java 在Enum类中存储值的映射集合,以便更好地访问,java,hash,enums,hashmap,Java,Hash,Enums,Hashmap,我有下一个enum类: public class DataParameter { public enum Types { BIG(-1), SMALL(1), SMAL2(2), SMAL3(5), BIG1(-1), BIG2(1), BIG3(2), BIF4( 5); private final int id; Types(int id) { this.id = id;

我有下一个
enum
类:

public class DataParameter {

    public enum Types {
        BIG(-1), SMALL(1), SMAL2(2), SMAL3(5), BIG1(-1), BIG2(1), BIG3(2), BIF4(
                5);

        private final int id;

        Types(int id) {
            this.id = id;
        }

        public int getValue() {
            return id;
        }

        public static <T> parseType(T type, int id) {
            int intType = Integer.parseInt(String.valueOf(type));

            if (intType == SIZE_XL.getValue()) {
                if (id == -1)
                    return BIG;
                if (id == 1)
                    return BIG2;
                if (id == 2)
                    return BIG3;
                if (id == 5)
                    return BIG4;
            } else if (intType == SIZE_LARGE.getValue()) {
                if (id == 5)
                    return BIG4;
                if (id == 2)
                    return SMALL2;
                if (id == 5)
                    return SMALL3;
            } else if (intType == SIZE_UKNOWN.getValue()) {
                if (id == -1)
                    return BIG;
                if (id == -1)
                    return BIG;
                if (id == -1)
                    return BIG1;
                if (id == 5)
                    return BIG4;
            } else if (intType == SIZE_UKNOWN_2.getValue()) {
                return SMAL3;
            }

            return returnDefault(intType);
        }
    }

    public DataParameter(GameTypes type) {
        mValue = type.getValue();
    }
}
当我得到密钥时,我将从地图中检索元素 我感到困惑,因为在
enum
类中,我遇到了一些未知的问题

HashMap<Integer, HashMap<Integer, Integer> myMap = new HashMap<Integer, HashMap<Integer, Integer>();
myMap.put(SIZE_XL.getValue(),BIG2.getValue(),BIG2)
myMap.put(SIZE_XL.getValue(),BIG3.getValue(),BIG3)
myMap.put(SIZE_XL.getValue(),BIG4.getValue(),BIG4)

HashMap您需要创建一个映射,而不是直接将值放在映射中,并且需要将该映射放在
enum
中的键上。您当前已将值放在不适用于Map的表单中

i、 而不是使用

HashMap<Integer, HashMap<Integer, Integer> myMap = new HashMap<Integer, HashMap<Integer, Integer>();
myMap.put(SIZE_XL.getValue(),BIG2.getValue(),BIG2)

HashMap在您的代码中,您省略了
SIZE\u XL
和类似常量的定义,我建议不要将它们定义为某个常量,而是将它们放在自己的
enum
中,例如:

enum Size {
  XL(2), LARGE(1), UNKNOWN(-1), UNKNOWN_2(-2);

  private final int value;
  Size(int val) {
    value = val;
  }

  public int getValue() {
    return value;
  }
}
在您的解析方法中,您正在根据两个参数查找
类型
,基本上这是两个键,您需要将它们组合起来才能获得值<代码>表格
是中定义的数据结构,具有两个键(一行和一列)和一个值。将感兴趣的组合(如解析函数中定义的)存储在中可以让您快速查找正确的值。代码变为:

public enum Types {
  SMALL(1), SMALL2(2), SMALL3(3),

  BIG(0), BIG1(1), BIG2(2), BIG3(3), BIG4(4);

  static final ImmutableTable<Integer, Integer, Types> TABLE =
    ImmutableTable.<Integer, Integer, Types>builder()
      .put(Size.XL.getValue(), BIG.getValue(), BIG)
      .put(Size.XL.getValue(), BIG2.getValue(), BIG2)
      .put(Size.XL.getValue(), BIG4.getValue(), BIG4)
      .put(Size.LARGE.getValue(), BIG4.getValue(), BIG4)
      .put(Size.LARGE.getValue(), SMALL2.getValue(), SMALL2)
      .put(Size.LARGE.getValue(), SMALL3.getValue(), SMALL3)
      .put(Size.UNKNOWN.getValue(), BIG.getValue(), BIG)
      .put(Size.UNKNOWN.getValue(), BIG1.getValue(), BIG1)
      .put(Size.UNKNOWN.getValue(), BIG4.getValue(), BIG4)
      .build();

  private final int id;

  Types(int id) {
    this.id = id;
  }

  public int getValue() {
    return id;
  }

  public static Collection<Types> getTypesWithSize(Size size) {
    return TABLE.row(size.getValue()).values();
  }

  public static Collection<Types> getTypesWithId(Types types) {
    return TABLE.column(types.getValue()).values();
  }

  public static <T> Types parseType(T type, int id) {
    final int intType = Integer.parseInt(String.valueOf(type));

    if (!TABLE.contains(intType, id)) {
      // return some default value or throw exception
    }
    return TABLE.get(intType, id);
  }
}
请注意,我必须更改
类型
的一些值,以避免重复的行和列组合,对于给定的行和列,只能有一个
类型
实例

enum Size {
  XL(2), LARGE(1), UNKNOWN(-1), UNKNOWN_2(-2);

  private final int value;
  Size(int val) {
    value = val;
  }

  public int getValue() {
    return value;
  }
}
public enum Types {
  SMALL(1), SMALL2(2), SMALL3(3),

  BIG(0), BIG1(1), BIG2(2), BIG3(3), BIG4(4);

  static final ImmutableTable<Integer, Integer, Types> TABLE =
    ImmutableTable.<Integer, Integer, Types>builder()
      .put(Size.XL.getValue(), BIG.getValue(), BIG)
      .put(Size.XL.getValue(), BIG2.getValue(), BIG2)
      .put(Size.XL.getValue(), BIG4.getValue(), BIG4)
      .put(Size.LARGE.getValue(), BIG4.getValue(), BIG4)
      .put(Size.LARGE.getValue(), SMALL2.getValue(), SMALL2)
      .put(Size.LARGE.getValue(), SMALL3.getValue(), SMALL3)
      .put(Size.UNKNOWN.getValue(), BIG.getValue(), BIG)
      .put(Size.UNKNOWN.getValue(), BIG1.getValue(), BIG1)
      .put(Size.UNKNOWN.getValue(), BIG4.getValue(), BIG4)
      .build();

  private final int id;

  Types(int id) {
    this.id = id;
  }

  public int getValue() {
    return id;
  }

  public static Collection<Types> getTypesWithSize(Size size) {
    return TABLE.row(size.getValue()).values();
  }

  public static Collection<Types> getTypesWithId(Types types) {
    return TABLE.column(types.getValue()).values();
  }

  public static <T> Types parseType(T type, int id) {
    final int intType = Integer.parseInt(String.valueOf(type));

    if (!TABLE.contains(intType, id)) {
      // return some default value or throw exception
    }
    return TABLE.get(intType, id);
  }
}
Types.getTypesWithSize(Size.LARGE); // returns [SMALL2, BIG4, SMALL3]
Types.getTypesWithId(Types.SMALL3); // returns [SMALL3]