有时候我们需要对list进行排序,方便我们查阅,下面就是本人做的一个小测试。
package www.huarui.biz.jfreecharts;
public class BuffBean {
private String month = null;
private int count = 0;
public String getMonth() {
return month;
}
public void setMonth(String month) {
this.month = month;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
package www.huarui.biz.jfreecharts;
import java.util.Comparator;
@SuppressWarnings("unchecked")
public class ComparatorBean implements Comparator {
@Override
public int compare(Object o1, Object o2) {
BuffBean bean1 = (BuffBean) o1;
BuffBean bean2 = (BuffBean) o2;
int flag = bean1.getMonth().compareTo(bean2.getMonth());
return flag;
}
}
public CategoryDataset yearOfLineDataset(int year) {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
MysqlJDBCUtil MysqlJDBCUtil = new MysqlJDBCUtil();
String sql = MysqlJDBCUtil.JDBC_Sql(year);
ArrayList max = produceMonth(year);
List buffList = MysqlJDBCUtil.executeSQL(sql);
ArrayList min = new ArrayList();
for (int i = 0; i < buffList.size(); i++) {
BuffBean bean = (BuffBean) buffList.get(i);
if (bean != null)
min.add(bean.getMonth());
}
List list = diff(max, min);
for (int j = 0; j < list.size(); j++) {
BuffBean sort = new BuffBean();
sort.setMonth(list.get(j).toString());
sort.setCount(0);
buffList.add(sort);
}
ComparatorBean comparator = new ComparatorBean();
Collections.sort(buffList, comparator);//使用自定义排序
for (int i = 0; i < buffList.size(); i++) {
BuffBean b = (BuffBean) buffList.get(i);
System.out.println("bean-month=" + b.getMonth());
System.out.println("bean-count=" + b.getCount());
dataset.addValue(b.getCount(), year + "", b.getMonth());
}
return dataset;
}