在Java 8中,可以使用Stream API非常方便地将Map转换为List。这里有几个常见的场景,包括将Map的键、值或键值对转换为List。 ### 1. 将Map的所有键转换为Listimport java.util.*; public class MapToListExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); List<String> keysList = map.keySet().stream().collect(Collectors.toList()); System.out.println(keysList); // 输出: [one, two, three] } }### 2. 将Map的所有值转换为Listimport java.util.*; public class MapToListExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap&l
在Java 8中,可以使用Stream API非常方便地将Map转换为List。这里有几个常见的场景,包括将Map的键、值或键值对转换为List。 ### 1. 将Map的所有键转换为Listimport java.util.*; public class MapToListExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap<>(); map.put("one", 1); map.put("two", 2); map.put("three", 3); List<String> keysList = map.keySet().stream().collect(Collectors.toList()); System.out.println(keysList); // 输出: [one, two, three] } }### 2. 将Map的所有值转换为Listimport java.util.*; public class MapToListExample { public static void main(String[] args) { Map<String, Integer> map = new HashMap&l