「Java」ListにMap型(String,Object)の要素(key、value)を取り出す方法

説明
List<Map<String, Object>> lstMp = new ArrayList<>();
Map<String, Object> mpA = new HashMap<String, Object>();
Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.java.study;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class LstMapPrintDemo {
public static void main(String[] args) {
List<Map<String, Object>> lstMp = new ArrayList<>();
// mpA
Map<String, Object> mpA = new HashMap<String, Object>();
mpA.put("cftKey01", true);
mpA.put("cftKey02", false);
mpA.put("cftKey03", 101);
// mpB
Map<String, Object> mpB = new HashMap<String, Object>();
mpB.put("cftKeyA", "A01");
mpB.put("cftKeyB", 2456);
// map add to list
lstMp.add(mpA);
lstMp.add(mpB);
for (int i = 0; i < lstMp.size(); i++) {
System.out.println("リストの要素" + (i + 1));
Map<String, Object> ttMap = lstMp.get(i);
Iterator<String> itr = ttMap.keySet().iterator();
while (itr.hasNext()) {
String keyName = (String) itr.next();
System.out.println(" Mapのkey: " + keyName + " MapのValue: " + ttMap.get(keyName));
}
}
}
}
package com.arkgame.java.study; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class LstMapPrintDemo { public static void main(String[] args) { List<Map<String, Object>> lstMp = new ArrayList<>(); // mpA Map<String, Object> mpA = new HashMap<String, Object>(); mpA.put("cftKey01", true); mpA.put("cftKey02", false); mpA.put("cftKey03", 101); // mpB Map<String, Object> mpB = new HashMap<String, Object>(); mpB.put("cftKeyA", "A01"); mpB.put("cftKeyB", 2456); // map add to list lstMp.add(mpA); lstMp.add(mpB); for (int i = 0; i < lstMp.size(); i++) { System.out.println("リストの要素" + (i + 1)); Map<String, Object> ttMap = lstMp.get(i); Iterator<String> itr = ttMap.keySet().iterator(); while (itr.hasNext()) { String keyName = (String) itr.next(); System.out.println(" Mapのkey: " + keyName + " MapのValue: " + ttMap.get(keyName)); } } } }
package com.arkgame.java.study;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class LstMapPrintDemo {

      public static void main(String[] args) {
            List<Map<String, Object>> lstMp = new ArrayList<>();

            // mpA
            Map<String, Object> mpA = new HashMap<String, Object>();
            mpA.put("cftKey01", true);
            mpA.put("cftKey02", false);
            mpA.put("cftKey03", 101);

            // mpB
            Map<String, Object> mpB = new HashMap<String, Object>();
            mpB.put("cftKeyA", "A01");
            mpB.put("cftKeyB", 2456);

            // map add to list
            lstMp.add(mpA);
            lstMp.add(mpB);

            for (int i = 0; i < lstMp.size(); i++) {
                  System.out.println("リストの要素" + (i + 1));
                  Map<String, Object> ttMap = lstMp.get(i);
                  Iterator<String> itr = ttMap.keySet().iterator();

                  while (itr.hasNext()) {
                        String keyName = (String) itr.next();
                        System.out.println(" Mapのkey: " + keyName + " MapのValue: " + ttMap.get(keyName));
                  }
            }

      }

}

結果
リストの要素1
Mapのkey: cftKey02 MapのValue: false
Mapのkey: cftKey03 MapのValue: 101
Mapのkey: cftKey01 MapのValue: true
リストの要素2
Mapのkey: cftKeyB MapのValue: 2456
Mapのkey: cftKeyA MapのValue: A01

Java

Posted by arkgame