Java实现将Map转换为List的小代码

来源:互联网 发布:阿里云学生机 备案 编辑:程序博客网 时间:2024/05/19 14:00
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));
        }
    }
}


原创粉丝点击