Java基本语法之对HashMap类型的List赋值与遍历

来源:互联网 发布:软件版本风险评估 编辑:程序博客网 时间:2024/06/05 20:37

直接上源码:

package com.daqintest.tcp;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Map.Entry;/** * @author *  */public class TestListContainHashMap {/** *  */public TestListContainHashMap() {// TODO Auto-generated constructor stub}/** * @param args */public static void main(String[] args) {int a = 1;int b = 0;int c = 1;String[] f = { "baseinfo", "collegeinfo", "majorinfo" };List<HashMap<String, String>> testList = new ArrayList<HashMap<String, String>>();HashMap<String, String> testHashMap = new HashMap<String, String>();for (int i = 0; i < f.length; i++) {testHashMap = new HashMap<String, String>();testHashMap.put("" + i, f[i]);switch (i) {case 0:if (a == 1) {testList.add(testHashMap);}break;case 1:if (b == 1) {testList.add(testHashMap);}break;case 2:if (c == 1) {testList.add(testHashMap);}break;default:break;}testHashMap = null;}// 遍历List ListIteratorfor (HashMap<String, String> hashMap : testList) {Iterator iterator = hashMap.entrySet().iterator();while (iterator.hasNext()) {Map.Entry entry = (Entry) iterator.next();System.out.println("key=" + entry.getKey() + "\tvalue=" + entry.getValue());}}}}


0 0
原创粉丝点击