zoukankan      html  css  js  c++  java
  • Map的基本用法

    实现Map接口的类用来存储键值对  Key---Value

    Map接口的实现类有HashMap和TreeMap等

    Map类中存储的键值对通过键来识别,所以键不能重复

    通过一个对象找到另一个对象

    注意:
    remove   ------是指从容器中移除某一键值对,没有真的的删除

    Delete  --------直接删除

     1 package text;
     2 
     3 
     4 import java.util.HashMap;
     5 import java.util.Map;
     6 
     7 /**
     8  *测试Map的基本用法 
     9  * **/
    10  
    11 public class TestMap{
    12     public static void main(String[] args){
    13         Map map= new HashMap();
    14         map.put("高崎","55");
    15         map.put("张三", new Wife("张曼玉"));
    16         Wife w=(Wife) map.get("张三");//get()方法返回的是Object对象,需要转型
    17         map.remove("高崎");
    18         Boolean t = map.containsKey("张三");
    19         System.out.println(map.get("张三"));
    20         System.out.println(w.name);
    21         System.out.println(map.get("高崎"));
    22         System.out.println(t);
    23     }
    24 }
    25 class Wife{
    26     String name;
    27     public Wife(String name){
    28         this.name=name;
    29     }
    30 }

    运行结果:

    text.Wife@6bbc4459
    张曼玉
    null
    true

    还有一些常用的方法

    int size()

    boolean isEmpty()

    putAll(Map t)

    void clear()

  • 相关阅读:
    Leetcode192. 统计词频
    Leetcode1046. 最后一块石头的重量
    Ubuntu20.04 NS3安装配置
    Ubuntu20.04 中文输入法+截图设置+NetAnim安装
    如何注册谷歌邮箱Gmail
    HDU 2612 Find a way
    友链
    2020沈阳区域赛补题&总结
    XShell中设置便捷的复制粘贴
    Hyper Text 超文本
  • 原文地址:https://www.cnblogs.com/qingsheng/p/9118668.html
Copyright © 2011-2022 走看看