分类汇总是统计中常用,举例来说如统计学生成绩,及格不及格的归类,分优良中差等级归类等,每个单项代码很好写,但是如果分类汇总的项目多了,能一种汇总写一个函数吗? 比如说有些科目60分才算及格,有些科目50分就算;有些老师喜欢分优良中差四等,有些老师却喜欢分ABCD;不一而足,如果每个都写一个函数无疑是个编写和维护恶梦. 如果我们用匿名类把
分类汇总的规则和
分类汇总的过程分别抽象出来,代码就清晰灵活多了,以下代码讲述了这个过程,代码比较简单,这里就不赘述了,相信大家都能看明白.
代码下载:
http://www.blogjava.net/Files/sitinspring/ClassSummary20070928113810.rar
首先是数据的基本类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;
}
}
然后是用于分类汇总的类,它强制子类实现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进行CRUD处理的StudentService类,注意getSum方法,它保留了筛选过程,筛选规则则不在其中:
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;
}
}
最后是测试代码,注意其中筛选规则的创建:
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));

// 分"优良中差"等级

ClassifyRule ruleCn = new ClassifyRule()
{

public String getKey()
{
String retval = "";

int score = student.getScore();

if (score >= 90)
{
retval = "优";

} 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分优良中差等级筛选");
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);
}
}
}
测试结果如下:
60分及格筛选
Key=及格 Value=5
Key=不及格 Value=2

50分及格筛选
Key=及格 Value=6
Key=不及格 Value=1

分优良中差等级筛选
Key=优 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

原理不复杂,这个抽象的过程还是有点意思的.