将Map转换成List

将Map转换成ListMap 转换为 List

Map转换为List

三种Map:包含 Map

>, Map

,stream

1、Map

>

package CSDN; import java.util.*; import java.util.function.Function; / * 2021/12/8 22:15 */ public class MapToList { 
    public static void main(String[] args) { 
    // 包含匿名方法的map转换成list,结果是hashCode(猜测是) Map<String, Function<Integer, Void>> funMap = mapIncludeFun(); List<Function<Integer, Void>> listFun = new ArrayList<>(); for (Map.Entry<String, Function<Integer, Void>> map : funMap.entrySet()) { 
    listFun.add(map.getValue()); } listFun.forEach(System.out :: println); } / * map中包含Function * * @return map */ private static Map<String, Function<Integer, Void>> mapIncludeFun() { 
    Map<String, Function<Integer, Void>> mapFun = new HashMap<>(); mapFun.put("a", test_a); mapFun.put("c", test_c); mapFun.put("d", test_c); return mapFun; } / * 包含Function Map的匿名函数 */ private static final Function<Integer, Void> test_a = (Integer num) -> { 
    System.out.println("测试函数a:" + num); return null; }; / * 包含Function Map的匿名函数 */ private static final Function<Integer, Void> test_c = (Integer num) -> { 
    System.out.println("测试函数c:" + num); return null; }; } 
结果:1CSDN.MapToList$$Lambda$1/@6acbcfc0 CSDN.MapToList$$Lambda$2/@5f184fc6 CSDN.MapToList$$Lambda$2/@5f184fc6 

2、Map

package CSDN; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; / * 2021/12/8 22:15 */ public class MapToList2 { 
    public static void main(String[] args) { 
    // 只包含简单的包装类的map转换成list,结果OK Map<Integer, String> simpleMap = simpleMap(); List<String> list = new ArrayList<>(); for (Map.Entry<Integer, String> map : simpleMap.entrySet()) { 
    list.add(map.getValue()); } list.forEach(System.out :: println); } / * map中不包含函数,只包含常见的包装类 * * @return map */ private static Map<Integer, String> simpleMap() { 
    Map<Integer, String> map = new HashMap<>(); map.put(3, "xiaoping"); map.put(9, "小铃铛"); return map; } } 
结果:2、 xiaoping 小铃铛 

3、map转换成list,使用stream

package CSDN; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.stream.Collectors; / * 2021/12/8 22:15 */ public class MapToList3 { 
    public static void main(String[] args) { 
    Map<Integer, String> map = new HashMap<>(); map.put(3, "liming"); map.put(12, "热忱"); List<Integer> list = new ArrayList(map.keySet()); list.forEach(System.out :: println); // 3 // 12 List<String> list1 = new ArrayList(map.values()); list1.forEach(System.out :: println); // liming // 热忱 List<Integer> list3 = map.keySet().stream().collect(Collectors.toList()); list3.forEach(System.out :: println); // 3 // 12 List<String> list4 = map.values().stream().collect(Collectors.toList()); list4.forEach(System.out :: println); // liming // 热忱 List<String> list5 = map.values().stream() .filter(x -> !"liming".equalsIgnoreCase(x)) .collect(Collectors.toList()); list5.forEach(System.out :: println); // 热忱 } } 
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/214119.html原文链接:https://javaforall.net

(0)
上一篇 2026年3月18日 下午4:57
下一篇 2026年3月18日 下午4:57


相关推荐

  • Android百度地图接入

    Android百度地图接入

    2021年9月30日
    48
  • apache 和 nginx 的区别

    apache 和 nginx 的区别1、nginx比apache占用更少的内存及资源2、抗并发—–nginx处理请求是异步非阻塞的,而apache则是阻塞型的,在高并发下nginx能保持低资源低消耗高性能3、apache少bug,nginx的bug相对较多 4、nginx运行效率高,占用资源少,代理功能强大,很适合做前端响应服务器5、Apache在处理动态有优势,Nginx并发性比较好,CPU

    2022年6月5日
    30
  • 高德地图设置中国经纬度范围

    高德地图设置中国经纬度范围高德地图设置范围有个参数 regionregion 表示地图中的一块区域 它有两个字段一个是 center 就是这块区域中心点的经纬度 另一个是 spanspan 表示的是 regoin 的范围 它有两个字段一个是 latitudeDelt 表示纬度范围 南纬和北纬加一起应该有 180 度 所以它的范围应该是大于 0 度 小于等于 180 度 另一个是 longitudeDel 表示经度范围 东经

    2026年3月16日
    2
  • CreateMutex WaitForSingleObject ReleaseMutex使用「建议收藏」

    CreateMutex WaitForSingleObject ReleaseMutex使用「建议收藏」HANDLECreateMutex( LPSECURITY_ATTRIBUTESlpMutexAttributes,// BOOLbInitialOwner, //flagforinitialownership LPCTSTRlpName    //pointertomutex-objectname );

    2022年6月26日
    29
  • CSS控制文本超出指定宽度显示省略号和文本不换行

    一般的文字截断(适用于内联与块):.text-overflow{display:block;/*内联对象需加*/width:31em;/*何问起hovertree.com*/word

    2021年12月23日
    48
  • 在python中调用并使用c语言

    在python中调用并使用c语言三个步骤 1 编写好 c 语言程序 2 将 c 程序编译成 so 文件 3 编写 python 使用 python 运行写个很简单的 demo 第一步 写一个 c 语言文件实现两数字相加 文件名字叫 add c include lt stdio h gt intadd int int int floatadd float float float intadd int intnum1 intn

    2026年3月27日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号