「Java」final static Mapを使うサンプル
説明
java.util.Map.put(String key, String value)
指定された値と指定されたキーをこのマップで関連付けます(オプションの操作)。
パラメータ:
key – 指定された値が関連付けられるキー
value – 指定されたキーに関連付けられる値
Javaコード
package com.arkgame.study;
import java.util.HashMap;
import java.util.Map;
public class MapStrDemo {
public final static Map<String, String> cftMap = new HashMap<>();
static {
cftMap.put("K001", "123");
cftMap.put("K002", "456");
}
public static void main(String[] args) {
String strA = "K001";
String resultA = test(strA);
String strB = "K002";
String resultB = test(strB);
System.out.println("文字列Aの戻り値:" + resultA);
System.out.println("文字列Bの戻り値:" + resultB);
}
private static String test(String key) {
String val = cftMap.get(key);
String result = "";
if ("123".equals(val)) {
result = "key001-result";
}
if ("456".equals(val)) {
result = "key002-result";
}
return result;
}
}
package com.arkgame.study;
import java.util.HashMap;
import java.util.Map;
public class MapStrDemo {
public final static Map<String, String> cftMap = new HashMap<>();
static {
cftMap.put("K001", "123");
cftMap.put("K002", "456");
}
public static void main(String[] args) {
String strA = "K001";
String resultA = test(strA);
String strB = "K002";
String resultB = test(strB);
System.out.println("文字列Aの戻り値:" + resultA);
System.out.println("文字列Bの戻り値:" + resultB);
}
private static String test(String key) {
String val = cftMap.get(key);
String result = "";
if ("123".equals(val)) {
result = "key001-result";
}
if ("456".equals(val)) {
result = "key002-result";
}
return result;
}
}
package com.arkgame.study; import java.util.HashMap; import java.util.Map; public class MapStrDemo { public final static Map<String, String> cftMap = new HashMap<>(); static { cftMap.put("K001", "123"); cftMap.put("K002", "456"); } public static void main(String[] args) { String strA = "K001"; String resultA = test(strA); String strB = "K002"; String resultB = test(strB); System.out.println("文字列Aの戻り値:" + resultA); System.out.println("文字列Bの戻り値:" + resultB); } private static String test(String key) { String val = cftMap.get(key); String result = ""; if ("123".equals(val)) { result = "key001-result"; } if ("456".equals(val)) { result = "key002-result"; } return result; } }
実行結果
文字列Aの戻り値:key001-result
文字列Bの戻り値:key002-result