package com.util;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class HashMapAndListAndHashtable {
/**
* -->Map
* List -->Map -->List-->Map
*
* for list --- while map
*
* @throws Exception
*/
public void listAndHashMap() throws Exception{
try{
Map mapLst0 = new HashMap();
mapLst0.put("testkey","testName");
Map mapLst1 = new HashMap();
mapLst1.put("ESTIMDEF","評価値1");
mapLst1.put("ESTIMNAME","評価名1");
Map mapLst2 = new HashMap();
mapLst2.put("ESTIMDEF","評価値2");
mapLst2.put("ESTIMNAME","評価名2");
Map mapLst3 = new HashMap();
mapLst3.put("ESTIMDEF","評価値3");
mapLst3.put("ESTIMNAME","評価名3");
Map mapLst4 = new HashMap();
mapLst4.put("ESTIMDEF","評価値4");
mapLst4.put("ESTIMNAME","評価名4");
List lstMa1 = new ArrayList();
lstMa1.add(mapLst1);
lstMa1.add(mapLst2);
List lstMa2 = new ArrayList();
lstMa2.add(mapLst3);
lstMa2.add(mapLst4);
Map map = new HashMap();
map.put("key",mapLst0);
map.put("lstMa1",lstMa1);
Map map1 = new HashMap();
map1.put("key",mapLst0);
map1.put("lstMa1",lstMa2);
List lst = new ArrayList();
lst.add(map);
lst.add(map1);
Map mapfor = null;
Map mapfor1 = null;
List lstfor = null;
String key = null;
String key1 = null;
Iterator itor = null;
Iterator itor1 = null;
//one line name
if(lst != null || lst.size() != 0){
mapfor = (Map)lst.get(0);
itor = mapfor.keySet().iterator();
while(itor.hasNext()){
key = (String) itor.next();
if (key.startsWith("lstMa1")) {
lstfor = (List)mapfor.get(key);
for(int j = 0; j < lstfor.size(); j++){
mapfor1 = (Map)lstfor.get(j);
itor1 = mapfor1.keySet().iterator();
while(itor1.hasNext()){
key1 = (String)itor1.next();
if(key1.startsWith("ESTIMNAME")){
System.out.println("mapfor1 name = " + mapfor1.get(key1));
}
}
}
}
}
}
//many lines value
for(int i = 0; i < lst.size(); i++){
mapfor = (Map)lst.get(i);
itor = mapfor.keySet().iterator();
while(itor.hasNext()){
key = (String) itor.next();
if (key.startsWith("lstMa1")) {
lstfor = (List)mapfor.get(key);
for(int j = 0; j < lstfor.size(); j++){
mapfor1 = (Map)lstfor.get(j);
itor1 = mapfor1.keySet().iterator();
while(itor1.hasNext()){
key1 = (String)itor1.next();
if(key1.startsWith("ESTIMDEF")){
System.out.println("mapfor1 value = " + mapfor1.get(key1));
}
}
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
/**
* while hashtable
*/
public void hashtable(){
Map mapLst1 = new HashMap();
mapLst1.put("ESTIMDEF","評価値1");
mapLst1.put("ESTIMNAME","評価名1");
Map mapLst2 = new HashMap();
mapLst2.put("ESTIMDEF","評価値2");
mapLst2.put("ESTIMNAME","評価名2");
List lst = new ArrayList();
lst.add("xuyz");
lst.add("lit");
lst.add("aaa");
Hashtable hashtable = new Hashtable();
hashtable.put("mapLst1",mapLst1);
hashtable.put("mapLst2",mapLst2);
hashtable.put("lst",lst);
String key = null;
//get enumModigy
Enumeration enumModigy = hashtable.keys();
while (enumModigy.hasMoreElements()) {
//get key
key = (String)enumModigy.nextElement();
Object obj = hashtable.get(key);
System.out.println("obj = " + obj);
}
}
/**
* main()
* @param args
*/
public static void main(String[] args) {
HashMapAndListAndHashtable test = new HashMapAndListAndHashtable();
try{
//
test.listAndHashMap();
System.out.println("#############################");
//
test.hashtable();
}catch (Exception e){
e.printStackTrace();
}
}
}
发表于 @ 2005年12月20日 14:29:00|编辑