优秀的编程知识分享平台

网站首页 > 技术文章 正文

如何在Java中将Map转为List?(java把map转换成json)

nanyue 2024-10-18 07:37:26 技术文章 10 ℃

如何将Map转为List?分为2种情况,一种是将Map中的key转为List,一种是将Map中的value转为List


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

public class Main2Test {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "1a");
        map.put(2, "2b");
        map.put(3, "3c");
        map.put(3, "3d");
        map.put(4, "4a");

        List<Integer> keyList = new ArrayList(map.keySet());
        for (Integer tmp : keyList) {
            System.out.println(tmp);
        }

        List<String> valueList = new ArrayList<>(map.values());
        for (String tmp : valueList) {
            System.out.println(tmp);
        }
    }
}

复制代码


作者:湖土
链接:https://juejin.cn/post/7019103344715104263
来源:稀土掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


最近发表
标签列表