List>集合中获取某个key并转换为List集合

时间:2023-03-09 19:52:23
List<Map<String, Object>>集合中获取某个key并转换为List<Integer>集合
package com.codyy.sso.controller.yuanqu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class Test {
public static void main(String[] args) {
List<Map<String, Object>> listMaps = new ArrayList<Map<String, Object>>(); Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("ID", "123");
map1.put("Name", "管理员2");
map1.put("year", "11010");
listMaps.add(map1);
System.out.println(listMaps);
System.out.println("___________________________"); Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("ID", "456");
map2.put("Name", "测试员");
map2.put("Year", "321010");
listMaps.add(map2);
System.out.println(listMaps); String role = null ;
StringBuffer roles = new StringBuffer();
for(int i = 0;i < listMaps.size();i++){
// 取出ID的值,拼接到roles中去
role = (String)listMaps.get(i).get("ID");
System.out.println("ID的值为:" + role);
roles.append(role + ",");
}

String[] str =roles.toString().split(",");
             List<Integer> Lids = new ArrayList<Integer>();
            for (int i = 0; i < str.length; i++) {
              Lids.add(new Integer(str[i]));
             }

            System.out.println(Lids);

    }
}