Reputation: 151
I want to print the value which I have created with the type Map dictionary
First, I have a class of Enum, then after that I define another static(I thought this will run one time when the object is initalized?), in this static, I will create dictionary for each of the enum.
Enum class:
public enum myEnumValues{
testingFile1,
testingFile2;
// this part I thought it will automatically make the dictionary based on the put I have specified? Here I use 2 put with values "check1" and "check2"
public static final Map<myEnumValues, String> var;
static{
Map<myEnumValues, String> putting = new EnumMap<>(myEnumValues.class);
putting.put(myEnumValues.testingFile1, "check1");
putting.put(myEnumValues.testingFile2, "check2");
var = Collections.unmodifiable(putting);
}
}
My Test class:
//Is there a way to print the dictionary value for both keys "testingFile1" and "testingFile2"? I think I understand it very wrong with my method. I am still learning Java.
import folder.data.myEnumValues;
@Test public void CheckTestForMyEnumValues(){
Map<myEnumValues, String> putting = new EnumMap<>(myEnumValues.class);
System.out.println(putting.get(myEnumValues.testingFile1));
System.out.println(putting.get(myEnumValues.testingFile2));
}
My expected result should be:
check1
check2
My goal of creating this enum dictionary class:
1) I will create another class that will have a variable a. It will then compare
if (x == myEnumValues.testingFile1){
var a == myEnumValues.get(); // store the a with value for key "testingFile1".
}else{
var a == null;
}
My test class is mainly for me to get the value for the key and then I will add more codes but the thing is now I could not even make a dictionary with enum and also I could not even know whether the dictionary is made and call the value of each enum. That is why I created this question.
Upvotes: 0
Views: 231
Reputation: 1683
You can print both keys and values for all entries within a map with following single line (Java 8)
putting.forEach((k, v) -> System.out.println(String.format("Key: %s, value: %s", k, v)));
Upvotes: 0
Reputation: 16053
Use the map that you've created in the static
block for accessing the values. Don't create a new map because the new map contains nothing.
@Test public void CheckTestForMyEnumValues(){
System.out.println(var.get(myEnumValues.testingFile1));
System.out.println(var.get(myEnumValues.testingFile2));
}
Upvotes: 1