Java基础知识_Map_collection其他

来源:互联网 发布:建筑设计效果图软件 编辑:程序博客网 时间:2024/06/15 15:16

</pre><h1>一、Map</h1><p> </p><h2>(一)Map概述</h2><p> </p><p><strong>接口 Map<K,V></strong></p><p align="left"><strong>类型参数:</strong></p><p align="left">K - 此映射所维护的键的类型</p><p align="left">V - 映射值的类型</p><p>将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射到一个值。</p><table border="1" cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td colspan="2" style="background:#CCCCFF"><p align="left"><strong>嵌套类摘要</strong></p></td></tr><tr><td valign="top"><p align="right">static interface</p></td><td><p align="left"><strong><a target=_blank href="file:///D:/JDK%20API/api/java/util/Map.Entry.html" title="java.util 中的接口">Map.Entry</a><<a target=_blank href="file:///D:/JDK%20API/api/java/util/Map.Entry.html" title="Map.Entry 中的类型参数">K</a>,<a target=_blank href="file:///D:/JDK%20API/api/java/util/Map.Entry.html" title="Map.Entry 中的类型参数">V</a>></strong> 映射项(键-值对)。</p></td></tr></tbody></table><p>Map</p><p>       |--Hashtable:底层是哈希表数据结构,不可以存入null键null值。该集合是线程同步的。jdk1.0.效率低。</p><p>       |--HashMap:底层是哈希表数据结构,允许使用 null 值和 null 键,该集合是不同步的。将hashtable替代,jdk1.2.效率高。</p><p>       |--TreeMap:底层是二叉树数据结构。线程不同步。可以用于给map集合中的键进行排序。</p><p> </p><p> </p><p>和Set很像。</p><p>其实,Set底层就是使用了Map集合。</p><p> </p><h2>(二)共性方法</h2><p>1,添加。</p><p>              put(Kkey, V value) </p><p>              putAll(Map<?extends K,? extends V> m) </p><p> </p><p>       2,删除。</p><p>              clear()</p><p>              remove(Objectkey) </p><p> </p><p>       3,判断。</p><p>              containsValue(Objectvalue) </p><p>              containsKey(Objectkey) </p><p>              isEmpty()</p><p> </p><p> </p><p>       4,获取。</p><p>              get(Objectkey) </p><p>              size()</p><p>              values()</p><p> </p><p>              entrySet()</p><p>              keySet()</p><p> </p><p> </p><h2>(三)自练</h2><p></p><pre name="code" class="java">import java.util.*; /* 需求:用Map接口的子类对象演示Map的共性方法 */  class MapDemo{       publicstatic void main(String[] args)       {              Map<String,Integer>hm = new HashMap<String,Integer>();                           hm.put("zhangsan4",14);              hm.put("zhangsan",11);              hm.put("zhangsan3",13);               hm.put("zhangsan2",12);              hm.put("zhangsan2",16);//两次输出键重复,之前的数据被覆盖               sop(hm);               //判断是否包含键或值              sop(hm.containsKey("zhangsan3"));//true              sop(hm.containsValue(33));//false               //获取              sop(hm.get("zhangsan4"));               //删除              sop(hm.remove("zhangsan4"));//通过键删除映射关系               sop(hm);               //获取Map集合中的所有值,并将其返回到Collection对象中              Collection<Integer>coll = hm.values();               sop(coll);        }        publicstatic void sop(Object obj)       {              System.out.println(obj);       }} 


 

(四)Map集合的两种取出方式

 

第一种Set<k>keySet:

将map中所有的键存入到Set集合。因为set具备迭代器。

所有可以迭代方式取出所有的键,在根据get方法。获取每一个键对应的值。

 

import java.util.*;/* 需求:用Map第一种取出方式keySet取出Map集合中的键值 */ class KeySetDemo{       publicstatic void main(String[] args)       {              Map<Integer,String>hm = new HashMap<Integer,String>();                           hm.put(14,"zhangsan4");              hm.put(11,"zhangsan");              hm.put(13,"zhangsan3");              hm.put(12,"zhangsan2");              hm.put(16,"zhangsan5");                             Set<Integer>ks = hm.keySet();               sop(ks);               Iterator<Integer>it = ks.iterator();               while(it.hasNext())              {                     Integerkey = it.next();                     sop("key:"+ key + " value:" + hm.get(key));              }          }        publicstatic void sop(Object obj)       {              System.out.println(obj);       }}           


 

       Map集合的取出原理:将map集合转成set集合。在通过迭代器取出。

 

第二种Set<Map.Entry<k,v>>entrySet:

将map集合中的映射关系存入到了set集合中,而这个关系的数据类型就是:Map.Entry

 

Entry其实就是Map中的一个static内部接口。

为什么要定义在内部呢?

因为只有有了Map集合,有了键值对,才会有键值的映射关系。

关系属于Map集合中的一个内部事物。

而且该事物在直接访问Map集合中的元素。

 

import java.util.*;/* 需求:使用第二种取出方式取出所有元素 */ class EntrySetDemo{       publicstatic void main(String[] args)       {              Map<Integer,String>hm = new HashMap<Integer,String>();                           hm.put(14,"zhangsan4");              hm.put(11,"zhangsan");              hm.put(13,"zhangsan3");              hm.put(12,"zhangsan2");              hm.put(16,"zhangsan5");                             Set<Map.Entry<Integer,String>>entrySet = hm.entrySet();               Iterator<Map.Entry<Integer,String>>it = entrySet.iterator();               while(it.hasNext())              {                     Map.Entry<Integer,String>me = it.next();                      Integerkey = me.getKey();                     Stringvalue = me.getValue();                                         sop("key:"+ key + " value:" + value);              }       }        publicstatic void sop(Object obj)       {              System.out.println(obj);       }} 


============================================================

二Hashtable

此类实现一个哈希表,该哈希表将键映射到相应的值。任何非 null 对象都可以用作键或值。

为了成功地在哈希表中存储和获取对象,用作的对象必须实现 hashCode 方法和 equals 方法

 

下面这个示例创建了一个数字的哈希表。它将数字的名称用作键:

   Hashtable<String, Integer> numbers
     = new Hashtable<String, Integer>();
   numbers.put("one", 1);
   numbers.put("two", 2);
   numbers.put("three", 3);

要获取一个数字,可以使用以下代码:

   Integer n = numbers.get("two");
     if (n != null) {
         System.out.println("two = " + n);
     }
   }


原创粉丝点击