• Java学习map2


    HashMap 类位于 java.util 包中,使用前需要引入它,语法格式如下:

    import java.util.HashMap; // 引入 HashMap 类

    以下实例我们创建一个 HashMap 对象 Sites, 整型(Integer)的 key 和字符串(String)类型的 value:

    HashMap<Integer, String> Sites = new HashMap<Integer, String>();

    添加元素

    HashMap 类提供类很多有用的方法,添加键值对(key-value)可以使用 put() 方法:

    实例

    // 引入 HashMap 类      
    import java.util.HashMap;

    public class RunoobTest {
        public static void main(String[] args) {
            // 创建 HashMap 对象 Sites
            HashMap<Integer, String> Sites = new HashMap<Integer, String>();
            // 添加键值对
            Sites.put(1, "Google");
            Sites.put(2, "Runoob");
            Sites.put(3, "Taobao");
            Sites.put(4, "Zhihu");
            System.out.println(Sites);
        }
    }

    执行以上代码,输出结果如下:

    {1=Google, 2=Runoob, 3=Taobao, 4=Zhihu}

    以下实例创建一个整型(String)的 key 和 整型(String)的 value:

    实例

    // 引入 HashMap 类      
    import java.util.HashMap;

    public class RunoobTest {
        public static void main(String[] args) {
            // 创建 HashMap 对象 Sites
            HashMap<String, String> Sites = new HashMap<String, String>();
            // 添加键值对
            Sites.put("one", "Google");
            Sites.put("two", "Runoob");
            Sites.put("three", "Taobao");
            Sites.put("four", "Zhihu");
            System.out.println(Sites);
        }
    }

    执行以上代码,输出结果如下:

    {four=Zhihu, one=Google, two=Runoob, three=Taobao}
  • 相关阅读:
    郭大小
    最近翻译的三篇新闻
    又是一年教师节
    PowerDesigner 12.5 反向工程sql server
    Sql Server使用技巧
    一个纠结的silverlight问题
    PowerDesigner 15 使用技巧
    windows2008 + iis7 下载特殊后缀名文件设置方法
    无法读取配置节system.serviceModel因为它缺少节声明的解决方法
    PowerDesigner 15对ACCESS进行反向工程
  • 原文地址:https://www.cnblogs.com/L-L-ALICE/p/14220678.html
Copyright © 2020-2023  润新知