Map接口集合方法解析

2016-06-21 17:13 阅读 5,715 次 评论 0 条

一.摘要

Map是一个键和值的集合数据结构,每一个键映射唯一的值,在声明Map对象时需要同时声明用作键的对象类。一个Map提供有用的方法遍历包含的所有键,以及各种方法访问和更新键、值对。

二.方法解析

1.clear()

Map移除所有的元素,置空集合

  1. mMap.clear();  

2.containsKey(Object key)

当前Map包含指定的键,返回true

  1. boolean isContained=mMap.containsKey(key);  

3.containsValue(Object val)

当前Map包含指定的值,返回true

  1. boolean isContained=mMap.containsValue(value);  

4.equals(Map map)

当前Map和指定的Map包含相同的映射关系,返回true

  1. /** 
  2.  *相同的元素个数,键、值一致,返回true 
  3.  * 
  4.  */  
  5. Map map=new HashMap();  
  6.         map.put("id"new Integer(123456789));  
  7. Map map2=new HashMap();  
  8.         map2.put("id"new Integer(123456789));  
  9. System.out.println(map.equals(map2));  
  1. /** 
  2.  *不同的元素个数,返回false 
  3.  * 
  4.  */  
  5. Map map=new HashMap();  
  6.         map.put("id"new Integer(123456789));  
  7. Map map2=new HashMap();  
  8.         map2.put("id"new Integer(123456789));  
  9.         map2.put("token_id"new Integer(123456789));  
  10. System.out.println(map.equals(map2));  
  1. /** 
  2.  *相同的元素个数,键一致,值不一致,返回false 
  3.  * 
  4.  */  
  5. Map map=new HashMap();  
  6.         map.put("id"new Integer(12345));  
  7. Map map2=new HashMap();  
  8.         map2.put("id"new Integer(123456789));  
  9. System.out.println(map.equals(map2));  
  1. /** 
  2.  *相同的元素个数,值一致,键不一致,返回false 
  3.  * 
  4.  */  
  5. Map map=new HashMap();  
  6.         map.put("id"new Integer(123456789));  
  7. Map map2=new HashMap();  
  8.         map2.put("token_id"new Integer(123456789));  
  9. System.out.println(map.equals(map2));  

5.get(Object key)

Map根据键获取值,直接通过指定一个对象的key获取,也可以返回key的集合Set,遍历所有的键、值

  1. Integer value=(Integer)map.get("id");  
  2.         System.out.println(value);  
  1. /** 
  2.  *遍历Map集合 
  3.  * 
  4.  */  
  5. Set keySet = map.keySet();  
  6. Iterator iter = keySet.iterator();  
  7.        while (iter.hasNext()) {  
  8.            String key = (String)iter.next();  
  9.            System.out.println(key+"="+map.get(key));  
  10.        }   

6.keySet()

返回键的Set集合

  1. /** 
  2.  *返回Set集合 
  3.  * 
  4.  */  
  5. Set keySet = map.keySet();  

6.entrySet()

返回映射关系的Set集合

  1. /** 
  2.  *返回Set集合 
  3.  * 
  4.  */  
  5. Set<Entry<String,Integer>> mappingSet = map.entrySet();  
  6. Iterator iter=mappingSet.iterator();  
  7.        while(iter.hasNext()){  
  8.            Entry<String, Integer> entry=(Entry<String, Integer>) iter.next();  
  9.            String key=entry.getKey();  
  10.            Integer val=entry.getValue();  
  11.            System.out.println(key+"="+val);  
  12.        }  

7.isEmpty()

当前Map为空,返回false

  1. boolean isEmpty=map.isEmpty();  

8.put(Object key,Object value)

存储键值对到当前Map集合

  1. map.put("id"new Integer(123456789));  

9.putAll(Map map)

将另一个Map集合的键值对复制到当前Map

  1. /** 
  2.  * 将另一个Map集合的键值对复制到当前Map 
  3.  */  
  4. ap.putAll(map2);  

10.remove(Object key)

删除指定键的映射关系

  1. /** 
  2.   * 删除指定键的映射关系 
  3.   */  
  4. map.remove("id");  

11.size()

返回当前Map集合的大小

  1. /** 
  2.   * 返回当前Map集合的大小 
  3.   */  
  4. map.size();  

12.values()

返回Map包含的值的Collection集合

  1. /** 
  2.   * 返回Map包含的值的Collection 
  3.   */  
  4. Collection collection=map.values();  
  5. Iterator iter=collection.iterator();  
  6.      while(iter.hasNext()){  
  7.        Integer entry=(Integer) iter.next();  
  8.        System.out.println(entry);  
  9.    }  

你可能感兴趣的文章

来源:TeachCourse每周一次,深入学习Android教程,关注(QQ158#9359$239或公众号TeachCourse)
转载请注明出处: https://www.teachcourse.cn/1834.html ,谢谢支持!

资源分享

分类:Java基础 标签:, ,
WordPress文章分页插件:Multi-page Toolkit WordPress文章分页插件:Mult
如何快速的搭建自己的第一个网站? 如何快速的搭建自己的第一个网站
Android Spinner控件自定义样式分析 Android Spinner控件自定义样式
Head First Of Design Pattern Head First Of Design Patt