Java 按值获取字段

Java 按值获取字段,java,Java,有一个接口,用于定义键盘的代码。每个按钮都有代码 public interface KeyMap{ private static final int A = 23; private static final int B = 24; //other keys ... } 但我的问题是:如何通过数字(23,24,…)获得字母(A,B,…)。 比如: public String getKey(int value); 我试图解决创建映射的问题,但需要一次又一次地初始化完整映射。我考虑过Java r

有一个接口,用于定义键盘的代码。每个按钮都有代码

public interface KeyMap{

private static final int A = 23;
private static final int B = 24;
//other keys
...
}
但我的问题是:如何通过数字(23,24,…)获得字母(A,B,…)。 比如:

public String getKey(int value);

我试图解决创建映射的问题,但需要一次又一次地初始化完整映射。我考虑过Java reflect,但找不到正确的方法来实现它。

Java Reflection是可能的,但速度会非常慢,因为您必须遍历所有字段才能获得具有正确值的字段

另一项建议:

enum KeyCode {

  A(23),
  ...;

  private int code;

  private static final Map<Integer, KeyCode> keys = new HashMap<Integer, KeyCode>();

  static {
    for(KeyCode code : values()) {
        keys.put(code.code, code);
    }
  }

  private KeyCode(int code) {
    this.code = code;
  }

  public static KeyCode getKey(int code) {
   return keys.get(code);
  }

}
这将非常快,等等。但当然,另一个问题是,如果您不能使用现有的框架。据我所知,Swing已经做了一些关键映射,例如

编辑:

好的,因为你似乎必须使用一个预定义的,反射实际上似乎是唯一的方法。。。此方法将允许您按值获取所有字段名的映射

public static Map<Object, String> getFieldsByValue(Class<?> clz) {
    Map<Object, String> map = new HashMap<Object, String>();
    // Remember: Class.getField() returns only PUBLIC fields
    for (Field field : clz.getFields()) {
        // Check if it's a static variable
        if (Modifier.isStatic(field.getModifiers())) {
            // Add other checks, for example for "integer", if you want.
            try {
                // field.get(null) returns the value of the field for a static field
                map.put(field.get(null), field.getName());
            } catch (IllegalArgumentException e) {
                // should not happen, as we made sure the field is static
            } catch (IllegalAccessException e) {
                // should not happen, as we only listed public fields
            }
        }
    }
    return map;
}

Java反射是可能的,但它会非常慢,因为您必须迭代所有字段以获得具有正确值的字段

另一项建议:

enum KeyCode {

  A(23),
  ...;

  private int code;

  private static final Map<Integer, KeyCode> keys = new HashMap<Integer, KeyCode>();

  static {
    for(KeyCode code : values()) {
        keys.put(code.code, code);
    }
  }

  private KeyCode(int code) {
    this.code = code;
  }

  public static KeyCode getKey(int code) {
   return keys.get(code);
  }

}
这将非常快,等等。但当然,另一个问题是,如果您不能使用现有的框架。据我所知,Swing已经做了一些关键映射,例如

编辑:

好的,因为你似乎必须使用一个预定义的,反射实际上似乎是唯一的方法。。。此方法将允许您按值获取所有字段名的映射

public static Map<Object, String> getFieldsByValue(Class<?> clz) {
    Map<Object, String> map = new HashMap<Object, String>();
    // Remember: Class.getField() returns only PUBLIC fields
    for (Field field : clz.getFields()) {
        // Check if it's a static variable
        if (Modifier.isStatic(field.getModifiers())) {
            // Add other checks, for example for "integer", if you want.
            try {
                // field.get(null) returns the value of the field for a static field
                map.put(field.get(null), field.getName());
            } catch (IllegalArgumentException e) {
                // should not happen, as we made sure the field is static
            } catch (IllegalAccessException e) {
                // should not happen, as we only listed public fields
            }
        }
    }
    return map;
}

Java反射是可能的,但它会非常慢,因为您必须迭代所有字段以获得具有正确值的字段

另一项建议:

enum KeyCode {

  A(23),
  ...;

  private int code;

  private static final Map<Integer, KeyCode> keys = new HashMap<Integer, KeyCode>();

  static {
    for(KeyCode code : values()) {
        keys.put(code.code, code);
    }
  }

  private KeyCode(int code) {
    this.code = code;
  }

  public static KeyCode getKey(int code) {
   return keys.get(code);
  }

}
这将非常快,等等。但当然,另一个问题是,如果您不能使用现有的框架。据我所知,Swing已经做了一些关键映射,例如

编辑:

好的,因为你似乎必须使用一个预定义的,反射实际上似乎是唯一的方法。。。此方法将允许您按值获取所有字段名的映射

public static Map<Object, String> getFieldsByValue(Class<?> clz) {
    Map<Object, String> map = new HashMap<Object, String>();
    // Remember: Class.getField() returns only PUBLIC fields
    for (Field field : clz.getFields()) {
        // Check if it's a static variable
        if (Modifier.isStatic(field.getModifiers())) {
            // Add other checks, for example for "integer", if you want.
            try {
                // field.get(null) returns the value of the field for a static field
                map.put(field.get(null), field.getName());
            } catch (IllegalArgumentException e) {
                // should not happen, as we made sure the field is static
            } catch (IllegalAccessException e) {
                // should not happen, as we only listed public fields
            }
        }
    }
    return map;
}

Java反射是可能的,但它会非常慢,因为您必须迭代所有字段以获得具有正确值的字段

另一项建议:

enum KeyCode {

  A(23),
  ...;

  private int code;

  private static final Map<Integer, KeyCode> keys = new HashMap<Integer, KeyCode>();

  static {
    for(KeyCode code : values()) {
        keys.put(code.code, code);
    }
  }

  private KeyCode(int code) {
    this.code = code;
  }

  public static KeyCode getKey(int code) {
   return keys.get(code);
  }

}
这将非常快,等等。但当然,另一个问题是,如果您不能使用现有的框架。据我所知,Swing已经做了一些关键映射,例如

编辑:

好的,因为你似乎必须使用一个预定义的,反射实际上似乎是唯一的方法。。。此方法将允许您按值获取所有字段名的映射

public static Map<Object, String> getFieldsByValue(Class<?> clz) {
    Map<Object, String> map = new HashMap<Object, String>();
    // Remember: Class.getField() returns only PUBLIC fields
    for (Field field : clz.getFields()) {
        // Check if it's a static variable
        if (Modifier.isStatic(field.getModifiers())) {
            // Add other checks, for example for "integer", if you want.
            try {
                // field.get(null) returns the value of the field for a static field
                map.put(field.get(null), field.getName());
            } catch (IllegalArgumentException e) {
                // should not happen, as we made sure the field is static
            } catch (IllegalAccessException e) {
                // should not happen, as we only listed public fields
            }
        }
    }
    return map;
}

您正在实现自己的集合,如
Map
:)使用
HashMap
(或任何
Map
实现)并输入值,如
Map.put(“a”,23)
。要获取值
map.get(“A”)
,返回
23
您正在实现自己的集合,如
map
:)使用
HashMap
(或任何
map
实现)并输入值,如
map.put(“A”),23
。要获取值
map.get(“A”)
,返回
23
您正在实现自己的集合,如
map
:)使用
HashMap
(或任何
map
实现)并输入值,如
map.put(“A”),23
。要获取值
map.get(“A”)
,返回
23
您正在实现自己的集合,如
map
:)使用
HashMap
(或任何
map
实现)并输入值,如
map.put(“A”),23
。要获取value
map.get(“A”)
,返回
23
,实际上,它是用于快捷方式的,但也希望在页面上创建一个包含所有快捷方式的“帮助窗口”,因此我寻找获取键名的方法。好的,在这种情况下,反射几乎是您唯一的选择。将更新答案。实际上,它是用于快捷方式的,但也希望在页面上创建一个包含所有快捷方式的“帮助窗口”,因此我寻找获取键名的方法。好的,在这种情况下,反射几乎是您唯一的选择。将更新答案。实际上,它是用于快捷方式的,但也希望在页面上创建一个包含所有快捷方式的“帮助窗口”,因此我寻找获取键名的方法。好的,在这种情况下,反射几乎是您唯一的选择。将更新答案。实际上,它是用于快捷方式的,但也希望在页面上创建一个包含所有快捷方式的“帮助窗口”,因此我寻找获取键名的方法。好的,在这种情况下,反射几乎是您唯一的选择。将更新答案。