java技術博客

          jsp博客
          數據加載中……
          java中的treemap
          /**
           * 通過這個程序,測試樹映像的使用,表目集合的遍歷
           
          */

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

          public class TreeMapTest
          {
              
          public static void main(String[] args)
              
          {
                  TreeMap set 
          = new TreeMap();
                  TreeMapTest test  
          = new TreeMapTest();
                  
          //生成三個學生實例
                  Student tom = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  set.put(
          "one", tom);
                  set.put(
          "two", jack);
                  set.put(
          "three", smith);
                  
                  System.out.println(
          "現在映像中元素的個數是:" + set.size() + "\n");
              
                  test.getValue(set);
                  
                  System.out.println(
          "\n現在設置重復關鍵字,覆蓋原來的對象。\n");
                  Student rose 
          = new Student("Rose","20020413");
                  System.out.println(
          "覆蓋已經存在的關鍵字時返回值:" + set.put("two", rose) + "\n");
                  
                  test.getValue(set);
              }

              
              
          public void getValue(TreeMap set)
              
          {
                  System.out.println(
          "遍歷映像中的關鍵字/值對分別是:");
                  Set entries 
          = set.entrySet();//得到關鍵字/值對的集合
                  Iterator iter = entries.iterator();
                  
          while(iter.hasNext())
                  
          {
                      Map.Entry entry 
          = (Map.Entry)iter.next();
                      String key 
          = (String)entry.getKey();
                      Student value 
          = (Student)entry.getValue();
                      System.out.print(key);
                      System.out.println(
          "\t\t" + value);
                  }

              }

          }


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

          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;
              }

          }






          /**
           * 通過這個程序,測試樹映像的使用,表目集合的遍歷
           
          */

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

          public class TreeMapTest2
          {
              
          public static void main(String[] args)
              
          {
                  TreeMap set 
          = new TreeMap(new ReverseSort());
                  TreeMapTest2 test  
          = new TreeMapTest2();
                  
          //生成三個學生實例
                  Student tom = new Student("Tom","20020410");
                  Student jack 
          = new Student("Jack","20020411");
                  Student smith 
          = new Student("Smith","20020412");
                  
                  set.put(
          "one", tom);
                  set.put(
          "two", jack);
                  set.put(
          "three", smith);
                  
                  System.out.println(
          "現在映像中元素的個數是:" + set.size() + "\n");
              
                  test.getValue(set);
                  
                  System.out.println(
          "\n現在設置重復關鍵字,覆蓋原來的對象。\n");
                  Student rose 
          = new Student("Rose","20020413");
                  System.out.println(
          "覆蓋已經存在的關鍵字時返回值:" + set.put("two", rose) + "\n");
                  
                  test.getValue(set);
              }

              
              
          public void getValue(TreeMap set)
              
          {
                  System.out.println(
          "遍歷映像中的關鍵字/值對分別是:");
                  Set entries 
          = set.entrySet();//得到關鍵字/值對的集合
                  Iterator iter = entries.iterator();
                  
          while(iter.hasNext())
                  
          {
                      Map.Entry entry 
          = (Map.Entry)iter.next();
                      String key 
          = (String)entry.getKey();
                      Student value 
          = (Student)entry.getValue();
                      System.out.print(key);
                      System.out.println(
          "\t\t" + value);
                  }

              }

          }


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

          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:08 郭興華 閱讀(4606) 評論(0)  編輯  收藏


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


          網站導航:
           
          主站蜘蛛池模板: 红原县| 甘洛县| 荣成市| 台湾省| 木兰县| 南陵县| 黄骅市| 岳普湖县| 肇庆市| 得荣县| 堆龙德庆县| 惠安县| 富顺县| 昌宁县| 邵武市| 黑龙江省| 孙吴县| 朝阳市| 晋城| 基隆市| 南郑县| 保靖县| 塔河县| 景洪市| 金湖县| 和林格尔县| 太保市| 云龙县| 丹阳市| 独山县| 土默特右旗| 东乡县| 清水县| 安徽省| 双桥区| 久治县| 土默特右旗| 双牌县| 茶陵县| 株洲县| 莱西市|