HashMap的總結

 

//HashMap的介紹

import java.util.HashMap;

public class MapTest1

{

       public static void main(String[] args)

       {

              HashMap map = new HashMap();

              map.put("a", "zhangsan");

              map.put("b", "lisi");

              map.put("c", "wangwu");

              map.put("a", "zhaoliu");

              //System.out.println(map);

              String value = (String)map.get("b");

              System.out.println(value);

              System.out.println("--------------");

              String value2 = (String)map.get("d");

              System.out.println(value2);

       }

}

 

 

 

 

 

public class MapTest2

{

       public static void main(String[] args)

       {

              HashMap map = new HashMap();

              String str = new String("zhangsan");

              map.put("a", str);

              map.put("a", str);

             

              System.out.println(map);

       }

}

 

 

 

 

 

public class MapTest3

{

       public static void main(String[] args)

       {

              HashMap map = new HashMap();

             

              map.put("a", "aa");

              map.put("b", "bb");

              map.put("c", "cc");

              map.put("d", "dd");

              map.put("e", "ee");

             

              Set set = map.keySet();

             

              for(Iterator iter = set.iterator(); iter.hasNext();)

              {

                     String key = (String)iter.next();

                     String value = (String)map.get(key);

                     System.out.println(key + "=" + value);

              }

       }

}

 

 

 

 

 

public class MapTest4

{

       public static void main(String[] args)

       {

              HashMap map = new HashMap();

              for(int i = 0; i < args.length; i++)

              {

                     if(map.get(args[i]) == null)

                     {

                            map.put(args[i], new Integer(1));

                     }

                     else

                     {

                            Integer in = (Integer)map.get(args[i]);

                            in = new Integer(in.intValue() + 1);

                            map.put(args[i], in);                   

                     }

              }

             

              Set set = map.keySet();

              for(Iterator iter = set.iterator(); iter.hasNext();)

              {

                     String key = (String)iter.next();

                     Integer value = (Integer)map.get(key);

                     System.out.println(key + " : " + value);

              }

       }

}

 

 

 

 

 

public class MapTest5

{

       public static void main(String[] args)

       {

              HashMap map = new HashMap();

              map.put("a", "aa");

              map.put("b", "bb");

              map.put("c", "cc");

              map.put("d", "dd");

              Set set = map.entrySet();

              for(Iterator iter = set.iterator(); iter.hasNext();)

              {

                     Map.Entry entry = (Map.Entry)iter.next();

                     String key = (String)entry.getKey();

                     String value = (String)entry.getValue();

                     System.out.println(key + " : " + value);

              }

       }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章