用匿名类处理分类汇总的
方法
快递客服问题件处理详细方法山木方法pdf计算方法pdf华与华方法下载八字理论方法下载
分类汇总是统计中常用,举例来说如统计学生成绩,及格不及格的归类,分优良中差等级归类等,每个单项代码很好写,但是如果分类汇总的项目多了,能一种汇总写一个函数吗?比如说有些科目60分才算及格,有些科目50分就算;有些老师喜欢分优良中差四
等,有些老师却喜欢分ABCD;不一而足,如果每个都写一个函数无
疑是个编写和维护恶梦.如果我们用匿名类把分类汇总的规则和
分类汇总的过程分别抽象出来,代码就清晰灵活多了,以下代码讲述了这个过程,代码比较简单,这里就不赘述了,相信大家都能看明白.
首先是数据的基本类Student:
publicclassStudent{
privateStringname;
privateintscore;
publicStudent(Stringname,intscore){
this.name=name;
this.score=score;
}
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicintgetScore(){
returnscore;
}
publicvoidsetScore(intscore){
this.score=score;
}
}
然后是用于分类汇总的类,它强制子类实现getKey和getval
ue两个方法:
publicabstractclassClassifyRule{
publicStudentstudent;
publicClassifyRule(){
}
publicvoidsetStudent(Studentstudent){
this.student=student;
}
abstractpublicStringgetKey();
abstractpublicintgetValue();
}
接下来是对Student进行CRUD处理的StudentService类,
注意getSum方法,它保留了筛选过程,筛选规则则不在其中:
importjava.util.ArrayList;
importjava.util.Hashtable;
importjava.util.List;
publicclassStudentService{
privateList
students;
publicStudentService(){
students=newArrayList();
}
publicvoidadd(Studentstudent){
students.add(student);
}
publicHashtablegetSum(ClassifyRulerule){
Hashtableht=newHashtable();
for(Studentstudent:students){
rule.setStudent(student);
Stringkey=rule.getKey();
intvalue=rule.getValue();
if(ht.containsKey(key)){
IntegeroldValue=ht.remove(key);
oldValue+=value;
ht.put(key,oldValue);
}else{
ht.put(key,value);
}
}
returnht;
}
}
最后是测试代码,注意其中筛选规则的创建:
importjava.util.Hashtable;
importjava.util.Iterator;
publicclassTest{
publicstaticvoidmain(String[]args){
//初始化
StudentServiceservice=newStudentService();
service.add(newStudent("Andy",90));
service.add(newStudent("Bill",95));
service.add(newStudent("Cindy",70));
service.add(newStudent("Dural",85));
service.add(newStudent("Edin",60));
service.add(newStudent("Felix",55));
service.add(newStudent("Green",15));
//60分及格筛选
ClassifyRulerule60=newClassifyRule(){
publicStringgetKey(){
returnstudent.getScore()>=60?"及格":"不及格";
}
publicintgetValue(){
return1;
}
};
System.out.println("60分及格筛选");
printHt(service.getSum(rule60));
//50分及格筛选
ClassifyRulerule50=newClassifyRule(){
publicStringgetKey(){
returnstudent.getScore()>=50?"及格":"不及格";
}
publicintgetValue(){
return1;
}
};
System.out.println("\n50分及格筛选");
printHt(service.getSum(rule50));
//分"优良中差"等级
ClassifyRuleruleCn=newClassifyRule(){
publicStringgetKey(){
Stringretval="";
intscore=student.getScore();
if(score>=90){
retval="优";
}elseif(score>=80){
retval="良";
}elseif(score>=60){
retval="中";
}elseif(score>0){
retval="差";
}
returnretval;
}
publicintgetValue(){
return1;
}
};
System.out.println("\n分优良中差等级筛选");
printHt(service.getSum(ruleCn));
//分"ABCD"等级
ClassifyRuleruleWest=newClassifyRule(){
publicStringgetKey(){
Stringretval="";
intscore=student.getScore();
if(score>=90){
retval="A";
}elseif(score>=80){
retval="B";
}elseif(score>=60){
retval="C";
}elseif(score>0){
retval="D";
}
returnretval;
}
publicintgetValue(){
return1;
}
};
System.out.println("\n分ABCD等级筛选");
printHt(service.getSum(ruleWest));
}
privatestaticvoidprintHt(Hashtableht){
for(Iteratorit=ht.keySet().iterator();it.hasNext();){
Stringkey=(String)it.next();
Integervalue=(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=AValue=2
Key=DValue=2
Key=CValue=2
Key=BValue=1