java技術博客

          jsp博客
          數據加載中……
          java中的HashMapTest
          /**
           * 通過這個程序,測試散列映像的存儲與遍歷,方法的使用
           
          */

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

          public class HashMapTest
          {
              
          public static void main(String[] args)
              
          {
                  HashMap map 
          = new HashMap();
                  HashMapTest test  
          = new HashMapTest();
                  
          //生成三個學生實例
                  Student tom = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  map.put(
          "one", tom);
                  map.put(
          "two", jack);
                  map.put(
          "three", smith);
                  
                  System.out.println(
          "現在映像中元素的個數是:" + map.size() + "\n");
                  
                  Set key 
          = map.keySet();//得到關鍵字的集合
                  System.out.println("現在映像中的關鍵字分別是:");
                  Iterator iterKey 
          = key.iterator();
                  
          while(iterKey.hasNext())
                  
          {
                      System.out.println(iterKey.next());
                  }

                  test.getValue(map);
                  
                  System.out.println(
          "\n現在設置重復關鍵字,覆蓋原來的對象。\n");
                  Student rose 
          = new Student("Rose","20020413");
                  System.out.println(
          "覆蓋已經存在的關鍵字時返回值:" + map.put("two", rose));
                  test.getValue(map);
              }

              
              
          public void getValue(HashMap map)
              
          {
                  Collection values 
          = map.values();//得到值的集合
                  System.out.println("\n現在映像中的值分別是:");
                  Iterator iterValue 
          = values.iterator();
                  
          while(iterValue.hasNext())
                  
          {
                      System.out.println(iterValue.next());
                  }

              }

          }


          /**
           * 我們設計的學生基本類
           
          */

          class Student
          {
              
          private String strName = "";//學生姓名
              private String strNumber = "";//學號
              private String strSex = "";//性別
              private String strBirthday = "";//出生年月
              private String strSpeciality = "";//專業
              private String strAddress = "";//地址

              
          public Student(String name, String number)
              
          {
                  strName 
          = name;
                  strNumber 
          = number;
              }


              
          public String getStudentName()
              
          {
                  
          return strName;
              }


              
          public String getStudentNumber()
              
          {
                  
          return strNumber;
              }


              
          public void setStudentSex(String sex)
              
          {
                  strSex 
          = sex;
              }


              
          public String getStudentSex()
              
          {
                  
          return strSex;
              }


              
          public String getStudentBirthday()
                  
          {
                  
          return strBirthday;
              }


              
          public void setStudentBirthday(String birthday)
              
          {
                  strBirthday 
          = birthday;
              }


              
          public String getStudentSpeciality()
              
          {
                  
          return strSpeciality;
              }


              
          public void setStudentSpeciality(String speciality)
              
          {
                  strSpeciality 
          = speciality;
              }


              
          public String getStudentAddress()
              
          {
                  
          return strAddress;
              }


              
          public void setStudentAddress(String address)
              
          {
                  strAddress 
          = address;
              }


              
          public String toString()
              
          {
                  String information 
          = "學生姓名=" + strName + ", 學號=" + strNumber;  
                  
          if!strSex.equals("") )
                      information 
          += ", 性別=" + strSex;
                  
          if!strBirthday.equals(""))
                      information 
          += ", 出生年月=" + strBirthday;
                  
          if!strSpeciality.equals("") )
                      information 
          += ", 專業=" + strSpeciality;
                  
          if!strAddress.equals("") )
                      information 
          += ", 籍貫=" + strAddress;
                  
          return information;
              }

          }

          posted on 2008-11-07 16:12 郭興華 閱讀(331) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 石狮市| 集贤县| 商南县| 太仆寺旗| 通化市| 友谊县| 黄梅县| 七台河市| 靖远县| 博野县| 神池县| 时尚| 裕民县| 连山| 久治县| 普兰店市| 瓦房店市| 屯门区| 兰州市| 河南省| 阳西县| 霍山县| 伽师县| 基隆市| 天门市| 高阳县| 邻水| 赤峰市| 天峻县| 池州市| 佛冈县| 乐东| 林西县| 红安县| 临夏县| 侯马市| 鄂托克前旗| 天门市| 江口县| 天等县| 正宁县|