Java对List元素进行按自定义排序

有时候我们需要对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;
}

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java 数据结构(十二):Collections工具类的使用

Collections工具类1.作用:操作Collection和Map的工具类2.常用方法:reverse(List):反转List中元素的顺序shuffle(List):对List集合元素进行随机排序sort(List):根据元素的自然顺序对指定List集合元素升序排序sort(List,Comparator)

List的Select 和Select().tolist()

List<PersondelpnewList<Person{newPerson{Id1,Name"小明1",Age11,Sign0},newPerson{Id2,Name"小明2",Age12,

Java对List元素进行按自定义排序 - HelloWorld