`
freewxy
  • 浏览: 336957 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

hashMap的使用

阅读更多
    [size=small;]java.util.hashMap类继承自java.util.Map接口。Map类有以下方法:[/size]


void clear()    //从此映射中移除所有映射关系

Boolean containsKey(Object key)//如果此映射中含有指定键的映射关系,则返回true

Boolean equals(object o)//比较指定的对象与此映射是否相等

V get(object key)//返回指定键所映射的值,如果此映射不包含该见的关系,则返回null

int hashCode()//返回此映射的哈希码值

Boolean isEmpty()//如果此映射不包含键-值映射关系,则返回TRUE

Set keySet()//返回此映射中包含的键的set视图

V put(K key, V value)//将指定的值与此映射中的指定键关联(可选操作)

void putAll(Map extends K,?extends V> m)//从指定的映射中将所有映射关系复制到此映射中

V remove(object K )//如果存在一个键的映射关系,则将其从此映射中移除(可选操作)

int size()//返回此映射中的键-值关系数


[size=small;]java.util.HashMap中的方法:[/size]



[b][size=small;]
[b]HashMap
()
          构造一个具有默认初始容量 (16) 和默认加载因子 (0.75) 的空 HashMap。
HashMap(int initialCapacity)
          构造一个带指定初始容量和默认加载因子 (0.75) 的空 HashMap。
[url=http://freewxy.iteye.com/java/util/HashMap.html#HashMap(int, float)]HashMap[/url](int initialCapacity, float loadFactor)
          构造一个带指定初始容量和加载因子的空 HashMap。
HashMap(Map extends K,? extends V> m)
          构造一个映射关系与指定 Map 相同的新 HashMap。

[/size][/b]

[/b]


void
[b]clear
()
          从此映射中移除所有映射关系。
Object
clone()
          返回此 HashMap 实例的浅表副本:并不复制键和值本身。
boolean
containsKey(Object key)
          如果此映射包含对于指定键的映射关系,则返回 true。
boolean
containsValue(Object value)
          如果此映射将一个或多个键映射到指定值,则返回 true。
SetMap.EntryK,V>>
entrySet()
          返回此映射所包含的映射关系的 Set 视图。
V
get(Object key)
          返回指定键所映射的值;如果对于该键来说,此映射不包含任何映射关系,则返回 null。
boolean
isEmpty()
          如果此映射不包含键-值映射关系,则返回 true。
SetK>
keySet()
          返回此映射中所包含的键的 Set 视图。
V
[url=http://freewxy.iteye.com/java/util/HashMap.html#put(K, V)]put[/url](K key, V value)
          在此映射中关联指定值与指定键。
void
putAll(Map extends K,? extends V> m)
          将指定映射的所有映射关系复制到此映射中,这些映射关系将替换此映射目前针对指定映射中所有键的所有映射关系。
V
remove(Object key)
          从此映射中移除指定键的映射关系(如果存在)。
int
size()
          返回此映射中的键-值映射关系数。
CollectionV>
values[/b]()
          返回此映射所包含的值的 Collection 视图。
package wxy.example.hashmap;

import   java.util.Set; 
import   java.util.Map; 
import   java.util.HashMap; 
import   java.util.Iterator; 

public class HashMapExample 
{ 
    public static void main(String[] args) 
        { 
          //   Create   a   new   HashMap 
           Map map=new HashMap(); 
                //   Add   Items   to   the   HashMap   
           map.put(new Integer(1),"One "); // V put(K key, V value) 在此映射中关联指定值与指定键。 
           map.put(new Integer(2),"Two "); //Integer()是一个类
           map.put(new Integer(3),"Three "); 
           map.put(new Integer(4),"Four "); 
           map.put(new Integer(5),"Five "); 
           map.put(new Integer(6),"Six "); 
           map.put(new Integer(7),"Seven "); 
           map.put(new Integer(8),"Eight "); 
           map.put(new Integer(9),"Nine "); 
           map.put(new Integer(10),"Ten"); 

     //   Use   iterator   to   display   the   keys   and   associated   values   
          System.out.println("Map   Values   Before:   "); 
          Set keys =map.keySet(); //返回此映射中所包含的键的 Set 视图
          for(Iterator i=keys.iterator();i.hasNext();) 
          { //Iterator模式是用于遍历集合类的标准访问方法。它可以把访问逻辑
        	//从不同类型的集合类中抽象出来,从而避免向客户端暴露集合的内部结构。
              Integer key=(Integer)i.next(); 
              //get(Object key) 返回指定键所映射的值;
              //如果对于该键来说,此映射不包含任何映射关系,则返回 null。
              String value =(String)map.get(key); 
              System.out.println(key+"="+value); 
           } 

          //   Remove   the   entry   with   key   6   
           System.out.println("\nRemove   element   with   key   6 "   ); 
          //remove(key) 从此映射中移除指定键的映射关系(如果存在)。
           map.remove(new Integer(6)); 
          //   Use   iterator   to   display   the   keys   and   associated   values   
           System.out.println("\nMap   Values   After:   "   ); 
           keys =map.keySet();  //返回此映射中所包含的键的 Set 视图
           for(Iterator i=keys.iterator();i.hasNext();) 
           { 
               Integer key=(Integer)i.next(); 
               String value = (String)map.get(key); 
               System.out.println(key+"="+value); 
           } 
           //other test of the method
           System.out.println("<-Size()->:"+map.size());
           System.out.println("<-hashCode()->:"+map.hashCode());
           System.out.println("<-entrySet()->:"+map.entrySet());
           System.out.println("<-map.containsKey(new Integer(5)->:"+map.containsKey(new Integer(5)));
           System.out.println("<-map.containsKey(new Integer(6))->:"+map.containsKey(new Integer(6)));
           System.out.println("<-map.values()->:"+map.values());
           System.out.println("<-map.get(new Integer(3))->:"+map.get(new Integer(3)));
           System.out.println("<-map.keySet()->:"+map.keySet());
           System.out.println("<-map.containsValue(\"Ten\")->:"+map.containsValue("Ten"));
           System.out.println("<-getClass()->:"+map.getClass());
           System.out.println("<-map.isEmpty()->:"+map.isEmpty());
           map.clear();
           System.out.println("<-map.clear()->:");
           System.out.println("<-map.isEmpty()->:"+map.isEmpty());
           
        } 
} 


今天大概了解一下HashMap这个类,还有对迭代器的使用,比较晕!
分享到:
评论
2 楼 huangfenghit 2011-08-08  
好文章啊!
1 楼 贾懂凯 2010-10-20  
迭代器的四种用法我的那篇关于“集合框架”的文章里有。无非就是获得entry或者获得key的队列两种。内层的实现我也不是很清楚。

相关推荐

Global site tag (gtag.js) - Google Analytics