A. 怎麼把Map類型強制轉換成List類型
轉一個:
實現將HashMap轉換成為ArrayList,並將map的Key 、Value分別存放到兩個ArrayList當中
----------------------------------------------------
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
public class ConvertMapToList {
/**
* 實現將HashMap轉換成為ArrayList,並將map的Key 、Value分別存放到兩個ArrayList當中
* @param args
*/
public static void main(String[] args) {
Map map = new HashMap();
map.put("a", "a1");
map.put("b", "b1");
map.put("c", "c1");
List listKey = new ArrayList();
List listValue = new ArrayList();
Iterator it = map.keySet().iterator();
while (it.hasNext()) {
String key = it.next().toString();
listKey.add(key);
listValue.add(map.get(key));
}
System.out.println("Convert Finished !");
//output the context of the ArrayList
for(int i =0 ;i<listKey.size();i++){
System.out.print("Key :"+listKey.get(i));
System.out.println(" Value :"+listValue.get(i));
}
}
}
B. 怎麼辦map形轉換成list
1、創建java類:TestMap2List,
C. 在java里怎麼把hashmap轉換成arraylist和iterator
importjava.util.ArrayList;
import
java.util.HashMap;
importjava.util.Iterator;
import
java.util.List;
importjava.util.Map;
publicclassConvertMapToList{
public
staticvoidmain(String[]args){
Mapmap=newHashMap();
map.put("a","a1");
map.put("b",
"b1");
map.put("c",
"c1");
List
listKey=newArrayList();
List
listValue=newArrayList();
Iteratorit=
map.keySet().iterator();
while
(it.hasNext()){
System.out.println(map);
Stringkey=
it.next().toString();
listKey.add(key);
listValue.add(map.get(key));
}
System.out.println("Convert
Finished!");
//outputthecontextofthe
ArrayList
for(inti=0
;i<listKey.size();i++){
System.out.println(listKey);
System.out.print("Key
:"+listKey.get(i));
System.out.println("Value
:"+listValue.get(i));
}
}
}