「Java」Map(String,Boolean)に列挙(enum)型のデータを格納するサンプル

2020年11月11日

説明
1.public enum 列挙名 {
変数名(値)
}
2.Map<String, Boolean> 変数名 = new HashMap<String, Boolean>();
変数名.put(列挙.変数名の値,xxxx)
使用例
1.列挙型(enum)の定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.it;
public enum AuthSet {
delAuth("delAuth"),
updateAuth("updateAuth"),
selectAuth("selectAuth"),
createAuth("createAuth");
private String value;
private AuthSet(String value) {
this.value = value;
}
public String value() {
return this.value;
}
}
package com.arkgame.study.it; public enum AuthSet { delAuth("delAuth"), updateAuth("updateAuth"), selectAuth("selectAuth"), createAuth("createAuth"); private String value; private AuthSet(String value) { this.value = value; } public String value() { return this.value; } }
package com.arkgame.study.it;

public enum AuthSet {

      delAuth("delAuth"),
      updateAuth("updateAuth"),
      selectAuth("selectAuth"),
      createAuth("createAuth");

      private String value;

      private AuthSet(String value) {
            this.value = value;
      }

      public String value() {
            return this.value;
      }

}

2.Mapで列挙(enum)を利用する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.it;
import java.util.HashMap;
import java.util.Map;
public class MapBooleanDemo {
protected static Map<String, Boolean> mp = new HashMap<String, Boolean>();
public static void main(String[] args) {
// put enum element value
mp.put(AuthSet.updateAuth.value(), true);
mp.put(AuthSet.delAuth.value(), false);
mp.put(AuthSet.createAuth.value(), true);
// output element
func(mp);
}
//map element key value get method
public static void func(Map<String, Boolean> msb) {
for (String key : msb.keySet()) {
System.out.println("キー:" + key + " 値:" + msb.get(key));
}
}
}
package com.arkgame.study.it; import java.util.HashMap; import java.util.Map; public class MapBooleanDemo { protected static Map<String, Boolean> mp = new HashMap<String, Boolean>(); public static void main(String[] args) { // put enum element value mp.put(AuthSet.updateAuth.value(), true); mp.put(AuthSet.delAuth.value(), false); mp.put(AuthSet.createAuth.value(), true); // output element func(mp); } //map element key value get method public static void func(Map<String, Boolean> msb) { for (String key : msb.keySet()) { System.out.println("キー:" + key + " 値:" + msb.get(key)); } } }
package com.arkgame.study.it;

import java.util.HashMap;
import java.util.Map;

public class MapBooleanDemo {

      protected static Map<String, Boolean> mp = new HashMap<String, Boolean>();

      public static void main(String[] args) {

            // put enum element value
            mp.put(AuthSet.updateAuth.value(), true);
            mp.put(AuthSet.delAuth.value(), false);
            mp.put(AuthSet.createAuth.value(), true);

            // output element
            func(mp);
      }
      //map element key value get method
      public static void func(Map<String, Boolean> msb) {

            for (String key : msb.keySet()) {
                  System.out.println("キー:" + key + " 値:" + msb.get(key));
            }
      }

}

3.実行結果
キー:createAuth 値:true
キー:updateAuth 値:true
キー:delAuth 値:false

Java

Posted by arkgame