zoukankan      html  css  js  c++  java
  • [Java] 遍历HashMap和HashMap转换成List的两种方式

    遍历HashMap和HashMap转换成List

    /**
    * convert the map to the list(1)
    */
    public static void main(String[] args) {
       Map<String, String> maps = new HashMap<String, String>();
       maps.put("a", "aa");
       maps.put("b", "bb");
       maps.put("c", "cc");
       maps.put("d", "dd");
       maps.put("e", "ee");
       maps.put("f", "ff");
      
       List<String> strList = new ArrayList<String>();
      
       for (String str : maps.values()) {
        strList.add(str);
       }
      
       for (int i = 0; i < strList.size(); i++) {
       
        System.out.println(strList.get(i));
       }
    }
    /**
    * convert the map to the list(2)
    */
    public static void main(String[] args) {
       Map<String, String> maps = new HashMap<String, String>();
       maps.put("a", "aa");
       maps.put("b", "bb");
       maps.put("c", "cc");
       maps.put("d", "dd");
       maps.put("e", "ee");
       maps.put("f", "ff");
      
       List<String> strList = new ArrayList<String>(maps.values());
      
       for (int i = 0; i < strList.size(); i++) {
       
        System.out.println(strList.get(i));
       }
    }

    控制台输出结果:

    dd
    aa
    cc
    ff
    bb
    ee

    (HashMap无序排列)

    --------------------------------------

    欢迎您,进入 我系程序猿 的cnBlog博客。

    你不能改变你的过去,但你可以让你的未来变得更美好。一旦时间浪费了,生命就浪费了。

    You cannot improve your past, but you can improve your future. Once time is wasted, life is wasted.

    --------------------------------------

    分享到QQ空间  

  • 相关阅读:
    注册表
    windows.location.href在IE6下停止工作
    LINUX配置IP的三种方式
    InnoSetup 打包代码 检测.netFramework
    SQLiteHelper
    黑马程序员_看视频记笔记_C#编程基础02
    通过注册表来检测是否安装Office
    SQLiteHelper
    TSQL
    IIS下发布关于Excel导入导出时遇到的问题集锦
  • 原文地址:https://www.cnblogs.com/jqmtony/p/3711508.html
Copyright © 2011-2022 走看看