分類匯總是統計中常用,舉例來說如統計學生成績,及格不及格的歸類,分優良中差等級歸類等,每個單項代碼很好寫,但是如果分類匯總的項目多了,能一種匯總寫一個函數嗎? 比如說有些科目60分才算及格,有些科目50分就算;有些老師喜歡分優良中差四等,有些老師卻喜歡分ABCD;不一而足,如果每個都寫一個函數無疑是個編寫和維護惡夢. 如果我們用匿名類把
分類匯總的規則和
分類匯總的過程分別抽象出來,代碼就清晰靈活多了,以下代碼講述了這個過程,代碼比較簡單,這里就不贅述了,相信大家都能看明白.
代碼下載:
http://www.tkk7.com/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

原理不復雜,這個抽象的過程還是有點意思的.