如何将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);
        }
    }
}

复制代码

map转list的方法(如何在Java中将Map转为List)(1)