HashMap的值全部取出来,分别存到两个ArrayList中

2024-12-31 03:37:54
推荐回答(3个)
回答1:

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

public class Du {

public static void main(String[] args) {

HashMap map = new HashMap();

map.put("one", new Integer(111));
map.put("two", new Integer(22));
map.put("three", new Integer(3333));

List keyList = new ArrayList(map.keySet());
List valueList = new ArrayList(map.values());

for(int i = 0; i < map.size(); i++){
System.out.print("Key list element: " + keyList.get(i));
System.out.println("\nValue list element: " + valueList.get(i));
System.out.println();

}

}

}

回答2:

public static void main(String args[]){
HashMap aa = new HashMap();
aa.put("0","1");
aa.put("1","2");
aa.put("2","3");
ArrayList listkey = new ArrayList();
ArrayList listvalue = new ArrayList();
上面的方法表示占同

}

回答3:

public void MapToList(Map m){
List key = new ArrayList();
List value = new ArrayList();
Iterator iter = m.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
key.add(entry.getKey().toString());
value.add(entry.getValue().toString());
}
}