zoukankan      html  css  js  c++  java
  • Map集合类

    1.1 Map.clear方法——从Map集合中移除所有映射关系

    public static void main(String[] args) {
        Map map=new HashMap();       //定义Map集合
        map.put("昨天", "定制目录");      //向集合中添加元素
        map.put("今天", "开始写作");
        map.put("明天", "当然继续写作");
        System.out.println("map大小为:"+map.size());  //输出集合大小
        map.clear();           //清除Map集合的所有内容
        System.out.println("清除内容后的map大小是:"+map.size());
    }

    1.2  Map.containsKey方法——判断Map集合对象中是否包含指定的键名

    public static void main(String[] args) {
      Map map = new HashMap();       //定义Map对象
      map.put("apple", "新鲜的苹果");      //向集合中添加对象
      map.put("computer", "配置优良的计算机");
      map.put("book", "堆积成山的图书");
      map.put("time", new Date()); 
      String key = "book"; 
      boolean contains = map.containsKey(key);    //判断是否包含指定的键值
      if (contains) {         //如果条件为真
       System.out.println("在Map集合中包含键名" + key); //输出信息
      } else {
       System.out.println("在Map集合中不包含键名" + key);
      }
    }

    1.3  Map.containsValue方法——判断Map集合中是否包含指定的键值

    containsValue(Object value)

    value:要查询的Map集合的指定键值对象。

    public static void main(String[] args) {
      Map map = new HashMap();     //定义Map集合对象
      map.put("apple", "新鲜的苹果");    //向对象添加元素 
      map.put("computer", "配置优良的计算机");
      map.put("book", "堆积成山的图书");
      map.put("time", new Date()); 
      try {
       Thread.sleep(1000);      //休眠1秒,使两个Date对象不相等
      } catch (InterruptedException e) {
       e.printStackTrace();
      }
      Date value = new Date();     //创建日期时间对象
      boolean contains = map.containsValue(value); //判断集合中是否包含指定的value值
      if (contains) {       //如果条件成立 
       System.out.println("在Map集合中包含键值" + value); //输出信息
     } else {
       System.out.println("在Map集合中不包含键值" + value);
      }
    }

    1.4  Map.equals方法——判断Map集合是否与指定的对象相同

    public static void main(String[] args) throws InterruptedException {
        Map map1 = new HashMap();      //定义Map集合对象
        map1.put("apple", "新鲜的苹果");     //向集合中添加对象
        map1.put("computer", "配置优良的计算机");
        map1.put("book", "堆积成山的图书");
        Map map2 = new HashMap();
        map2.put("apple", "新鲜的苹果");     //定义Map集合对象map2
        map2.put("computer", "配置优良的计算机");
        map2.put("book", "堆积成山的图书");
        boolean contains = map1.equals(map2);   //判断集合是否相等
        if (contains) {
            System.out.println("两个Map对象相同");
        } else {
            System.out.println("这不是两个相同的Map对象");
        }
    }

    1.5  Map.get方法——返回指定键所映射的值

     public static void main(String[] args) {
      Map map = new HashMap();     //定义Map集合对象
      map.put("apple", "新鲜的苹果");    //向集合中添加对象
      map.put("computer", "配置优良的计算机");
      map.put("book", "堆积成山的图书");
      Object get = map.get("apple");    //获取指定键所映射的值
      if (get instanceof String) {    //判断键值是否为String类型
       String value = (String) get;    //获取指定的value值
       System.out.println("在Map集合中键名apple的键值是:" + value); //将value值输出
      }
    }

    1.6  Map.isEmpty方法——判断Map集合对象是否包含内容

    public static void main(String[] args) {
      System.out.println("创建Map集合对象"); 
      Map map = new HashMap();       //定义Map集合对象
      System.out.println("Map集合的isEmpty方法返回值是:" + map.isEmpty());
      //判断集合是否为空
      System.out.println("添加内容到Map集合");
      map.put("apple", "新鲜的苹果");       //向集合中添加对象
      map.put("computer", "配置优良的计算机");
      map.put("book", "堆积成山的图书");
      System.out.println("Map集合的isEmpty方法返回值是:" + map.isEmpty()); 
      //判断集合是否为空
    }

    1.7  Map.keySet方法——获取Map集合的所有键名

    public static void main(String[] args) {
      Map map = new HashMap();        //定义Map集合
      map.put("apple", "新鲜的苹果");      //向集合中添加元素
      map.put("computer", "配置优良的计算机");
      map.put("book", "堆积成山的图书");
      map.put("time", new Date());      //向集合中添加时间对象
      Set keySet = map.keySet();       //获取key集合对象
      for (Object keyName : keySet) {
       System.out.println("键名:" + keyName);   //输出键名
      }
    }

    1.8  Map.put方法——获取Map集合的所有键名

    put(K key, V value) 

    key:是要保存到Map集合中的键名。

    value:是要保存到Map集合中对应键名的键值对象

    public static void main(String[] args) throws InterruptedException {
        Map map = new HashMap();
        map.put("apple", "新鲜的苹果");
        map.put("computer", "配置优良的计算机");
        map.put("book", "堆积成山的图书");
        map.put("time", new Date());
        int size=map.size();
        System.out.println("Map集合的大小为:"+size);
    }

    运行结果为“Map集合的大小为:4”。

    1.9  Map.putAll方法——追加另一个Map对象到当前Map集合

    putAll(Map<? extends K,? extends V> m) 

    m:一个Map集合对象。

    public static void main(String[] args) {
      Map map1 = new HashMap();      //定义Map集合对象
        map1.put("apple", "新鲜的苹果");     //向集合中添加对象
        map1.put("computer", "配置优良的计算机");
        map1.put("book", "堆积成山的图书");
        System.out.println("第一个Map集合大小为:"+map1.size()) //输出集合长度
        Map map2 = new HashMap();      //定义Map集合map2
        map2.put("apple2", "新鲜的苹果");     //向集合中添加对象
        map2.put("computer2", "配置优良的计算机");
        map2.put("book", "堆积成山的图书");
        System.out.println("第二个Map集合大小为:"+map2.size()); //输出集合长度
        System.out.println("把第二个Map集合添加到第一个Map集合中");
        map1.putAll(map2);        //将map2中的对象添加到map1中
        System.out.println("整合后的第一个Map集合大小为:"+map1.size());
    }

    2.0  Map.values方法——获取Map集合中的所有键值对象

    public static void main(String[] args) {
        Map map = new HashMap();      //定义Map集合对象
        map.put("apple", "新鲜的苹果");     //向集合中添加对象
        map.put("computer", "配置优良的计算机");
        map.put("book", "堆积成山的图书");
        Collection values = map.values();    //获取Map集合的value集合
        for (Object object : values) {
            System.out.println("键值:" + object.toString()); //输出键值对象
        }
    }

  • 相关阅读:
    Visual Studio 2008 每日提示(十一)
    Visual Studio 2008 每日提示(十二)
    13 个应该记住的最不寻常的搜索引擎
    Web1.0及WEB2.0的比较
    Comparing Properties to Methods
    1
    Struct Constructor Restrictions
    简单的动态下拉菜单
    权限设计(转)
    conceptpolymorphism
  • 原文地址:https://www.cnblogs.com/zxg-6/p/5650991.html
Copyright © 2011-2022 走看看