「Java」配列の要素をHashMapに格納するサンプル
書式
for (Map.Entry<String, String> 変数名 : map名.entrySet()) {
使用例
package com.arkgame.study.tm;
import java.util.HashMap;
import java.util.Map;
public class HashMapDemo {
      private static final String[] cftA = { "key01", "key02", "key03", "key04" };
      private static final String[] cftB = { "value-11", "value-22", null, "value-44", null, "test22" };
      public static void main(String[] args) {
            HashMap<String, String> resMap = new HashMap<String, String>();
            String strVal;
            for (int i = 0; i < cftA.length; i++) {
                  if (cftB[i] == null) {
                        strVal = "99";
                  } else {
                        strVal = cftB[i];
                  }
                  // 要素の格納
                  resMap.put(cftA[i], strVal);
            }
            System.out.println("mapの長さ: " + resMap.size());
            // mapの要素を取得
            for (Map.Entry<String, String> cft : resMap.entrySet()) {
                  System.out.println("キー: " + cft.getKey() + " 値: " + cft.getValue());
            }
      }
}
実行結果
mapの長さ: 4
キー: key04 値: value-44
キー: key03 値: 99
キー: key02 値: value-22
キー: key01 値: value-11