资讯动态

MAC-Java8-map转list

发布时间:2026/10/20 10:27:04 来源:尧图企业网站定制
在Java 8中,可以使用Stream API非常方便地将Map转换为List。这里有几个常见的场景,包括将Map的键、值或键值对转换为List。 ### 1. 将Map的所有键转换为Listimport java.util.*; public class MapToListExample { public static void main(String[] args) { MapString, Integer map = new HashMap(); map.put("one", 1); map.put("two", 2); map.put("three", 3); ListString 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) { MapString, Integer map = new HashMapl

读完文章,也想定制专属网站?

尧图设计师 24 小时内与您沟通定制方案

免费获取报价 →
↑