-1

Not sure how to phrase that but I have this:

In a

public class DefinedValues{
public static final int CommandGroupLength = 0x00000001;
}

I want a way of getting the String "CommandGroupLength" from value 0x00000001;

Is that possible?

user2568374
  • 1,164
  • 4
  • 11
  • 21
  • 1
    do you want to access the name of the variable that has the value 0x00000001? than it is not possible. But you can create a Map, which has Key-Value Pairs and from that you could achieve the result – Japu_D_Cret Mar 29 '17 at 13:40
  • Why do you want to do this? There might be an easier solution. – Aloso Mar 29 '17 at 13:41
  • You could probably use reflection and go through all fields of the class, but at that point you should stop and really think whether you want to do this, and then you should *not* do it. – cbr Mar 29 '17 at 13:46
  • We need a little more context regarding your requirements for a good solution. Reflection or a static array of constant strings might solve your problem. – Jim U Mar 29 '17 at 15:28
  • @Japu_D_Cret, I accept your first sentence of your answer. It appears that this cannot be done. Hard to explain the context, it is needed for something but can't explain because too lengthy. – user2568374 Mar 31 '17 at 16:34
  • @user2568374 i'm pretty sure it *can* be done, but your question wasn't that clear and if you would edit your question to add more context we might come up with a better solution – Japu_D_Cret Mar 31 '17 at 16:43

1 Answers1

1

do you want to access the name of the variable that has the value 0x00000001? than this is not possible:

public class DefinedValues {
  public static final int CommandGroupLength = 0x00000001;
}

with Java8 it is technically possible to at least get the name of a variable via reflection, see Java Reflection: How to get the name of a variable?

you can achieve the same thing much easier with a Map, which contains Key-Value-Pairs.

Map<String,Integer> myMap= new HashMap<>();
myMap.put("CommandGroupLength", 0x00000001);

then you write a function which searches in the entrySet of the Map for all keys that have that value, since it is not ensured there is only one, it'll need to return a Collection or Array or something similar. here's my code:

public static void main(String[] args) {
  Map<String,Integer> myMap = new HashMap<>();
  myMap.put("CommandGroupLength", 0x00000001);
  myMap.put("testA", 5);
  myMap.put("testB", 12);
  myMap.put("testC", 42);

  System.out.println("Searching for value 0x00000001 in myMap");
  Set<String> searchResults = findKeyByValue(myMap, 0x00000001);
  System.out.println("I found the following keys:");
  boolean isFirst = true;
  for(String result : searchResults) {
    if(isFirst)
      isFirst = false;
    else
      System.out.printf(", ");

    System.out.printf("%s", result);
  }
}

public static Set<String> findKeyByValue(Map<String, Integer> map, Integer value) {
  Set<String> result = new HashSet<>();

  if(value != null) {
    Set<Entry<String, Integer>> entrySet = map.entrySet();

    for(Entry<String, Integer> entry : entrySet) {
      if(value.equals(entry.getValue())) {
        result.add(entry.getKey());
      }
    }
  }

  return result;
}
Community
  • 1
  • 1
Japu_D_Cret
  • 632
  • 5
  • 18