和風(fēng)細(xì)雨

          世上本無難事,心以為難,斯乃真難。茍不存一難之見于心,則運(yùn)用之術(shù)自出。

          用匿名類處理分類匯總的方法

          分類匯總是統(tǒng)計(jì)中常用,舉例來說如統(tǒng)計(jì)學(xué)生成績,及格不及格的歸類,分優(yōu)良中差等級歸類等,每個單項(xiàng)代碼很好寫,但是如果分類匯總的項(xiàng)目多了,能一種匯總寫一個函數(shù)嗎? 比如說有些科目60分才算及格,有些科目50分就算;有些老師喜歡分優(yōu)良中差四等,有些老師卻喜歡分ABCD;不一而足,如果每個都寫一個函數(shù)無疑是個編寫和維護(hù)惡夢. 如果我們用匿名類把分類匯總的規(guī)則分類匯總的過程分別抽象出來,代碼就清晰靈活多了,以下代碼講述了這個過程,代碼比較簡單,這里就不贅述了,相信大家都能看明白.

          代碼下載:
          http://www.aygfsteel.com/Files/sitinspring/ClassSummary20070928113810.rar

          首先是數(shù)據(jù)的基本類Student:
          public class Student{
              
          private String name;
              
          private int score;
              
              
          public Student(String name,int score){
                  
          this.name=name;
                  
          this.score=score;
              }

              
              
          public String getName() {
                  
          return name;
              }

              
          public void setName(String name) {
                  
          this.name = name;
              }

              
          public int getScore() {
                  
          return score;
              }

              
          public void setScore(int score) {
                  
          this.score = score;
              }
              
          }

          然后是用于分類匯總的類,它強(qiáng)制子類實(shí)現(xiàn)getKey和getvalue兩個方法:
          public abstract class ClassifyRule {
              
          public Student student;
              
              
          public ClassifyRule(){        
              }
              

              
          public void setStudent(Student student) {
                  
          this.student = student;
              }

              
              
          abstract public String getKey();
              
          abstract public int getValue();
          }

          接下來是對Student進(jìn)行CRUD處理的StudentService類,注意getSum方法,它保留了篩選過程,篩選規(guī)則則不在其中:
          import java.util.ArrayList;
          import java.util.Hashtable;
          import java.util.List;

          public class StudentService {
              
          private List<Student> students;

              
          public StudentService() {
                  students 
          = new ArrayList<Student>();
              }


              
          public void add(Student student) {
                  students.add(student);
              }


              
          public Hashtable<String, Integer> getSum(ClassifyRule rule) {
                  Hashtable
          <String, Integer> ht = new Hashtable<String, Integer>();

                  
          for (Student student : students) {
                      rule.setStudent(student);
                      String key 
          = rule.getKey();
                      
          int value = rule.getValue();

                      
          if (ht.containsKey(key)) {
                          Integer oldValue 
          = ht.remove(key);
                          oldValue 
          += value;
                          ht.put(key, oldValue);
                      }
           else {
                          ht.put(key, value);
                      }

                  }


                  
          return ht;
              }

          }

          最后是測試代碼,注意其中篩選規(guī)則的創(chuàng)建:
          import java.util.Hashtable;
          import java.util.Iterator;

          public class Test {
              
          public static void main(String[] args) {
                  
          // 初始化
                  StudentService service = new StudentService();
                  service.add(
          new Student("Andy"90));
                  service.add(
          new Student("Bill"95));
                  service.add(
          new Student("Cindy"70));
                  service.add(
          new Student("Dural"85));
                  service.add(
          new Student("Edin"60));
                  service.add(
          new Student("Felix"55));
                  service.add(
          new Student("Green"15));

                  
          // 60分及格篩選
                  ClassifyRule rule60 = new ClassifyRule() {
                      
          public String getKey() {
                          
          return student.getScore() >= 60 ? "及格" : "不及格";
                      }


                      
          public int getValue() {
                          
          return 1;
                      }

                  }
          ;

                  System.out.println(
          "60分及格篩選");
                  printHt(service.getSum(rule60));

                  
          // 50分及格篩選
                  ClassifyRule rule50 = new ClassifyRule() {
                      
          public String getKey() {
                          
          return student.getScore() >= 50 ? "及格" : "不及格";
                      }


                      
          public int getValue() {
                          
          return 1;
                      }

                  }
          ;

                  System.out.println(
          "\n50分及格篩選");
                  printHt(service.getSum(rule50));

                  
          // 分"優(yōu)良中差"等級
                  ClassifyRule ruleCn = new ClassifyRule() {
                      
          public String getKey() {
                          String retval 
          = "";

                          
          int score = student.getScore();
                          
          if (score >= 90{
                              retval 
          = "優(yōu)";
                          }
           else if (score >= 80{
                              retval 
          = "";
                          }
           else if (score >= 60{
                              retval 
          = "";
                          }
           else if (score > 0{
                              retval 
          = "";
                          }


                          
          return retval;
                      }


                      
          public int getValue() {
                          
          return 1;
                      }

                  }
          ;

                  System.out.println(
          "\n分優(yōu)良中差等級篩選");
                  printHt(service.getSum(ruleCn));

                  
          // 分"ABCD"等級
                  ClassifyRule ruleWest = new ClassifyRule() {
                      
          public String getKey() {
                          String retval 
          = "";

                          
          int score = student.getScore();
                          
          if (score >= 90{
                              retval 
          = "A";
                          }
           else if (score >= 80{
                              retval 
          = "B";
                          }
           else if (score >= 60{
                              retval 
          = "C";
                          }
           else if (score > 0{
                              retval 
          = "D";
                          }


                          
          return retval;
                      }


                      
          public int getValue() {
                          
          return 1;
                      }

                  }
          ;

                  System.out.println(
          "\n分ABCD等級篩選");
                  printHt(service.getSum(ruleWest));
              }


              
          private static void printHt(Hashtable ht) {
                  
          for (Iterator it = ht.keySet().iterator(); it.hasNext();) {
                      String key 
          = (String) it.next();
                      Integer value 
          = (Integer) ht.get(key);
                      System.out.println(
          "Key=" + key + " Value=" + value);
                  }

              }

          }

          測試結(jié)果如下:

           

          60分及格篩選
          Key
          =及格 Value=5
          Key
          =不及格 Value=2

          50分及格篩選
          Key
          =及格 Value=6
          Key
          =不及格 Value=1

          分優(yōu)良中差等級篩選
          Key
          =優(yōu) Value=2
          Key
          =良 Value=1
          Key
          =中 Value=2
          Key
          =差 Value=2

          分ABCD等級篩選
          Key
          =A Value=2
          Key
          =D Value=2
          Key
          =C Value=2
          Key
          =B Value=1

          原理不復(fù)雜,這個抽象的過程還是有點(diǎn)意思的.

          posted on 2008-02-22 12:15 和風(fēng)細(xì)雨 閱讀(393) 評論(0)  編輯  收藏 所屬分類: 算法

          主站蜘蛛池模板: 榆社县| 通州区| 民勤县| 特克斯县| 永和县| 石城县| 西藏| 湄潭县| 双柏县| 昭觉县| 邵阳县| 桐城市| 赣州市| 隆子县| 遂平县| 温泉县| 西平县| 库车县| 洞头县| 龙里县| 福建省| 南部县| 金山区| 巴塘县| 霍城县| 吴忠市| 凤台县| 南川市| 府谷县| 塔城市| 紫阳县| 金沙县| 德格县| 广汉市| 万源市| 江阴市| 本溪| 桑植县| 临城县| 平谷区| 鹤山市|