java 8 api添加了一个新的抽象称为流stream,可以让你以一种声明的方式处理数据。
stream 使用一种类似用 sql 语句从数据库查询数据的直观方式来提供一种对 java 集合运算和表达的高阶抽象。
0.数据统计
list<map<string, object>> valuelist = new arraylist<>();
map max = valuelist.stream().max(comparator.comparing(s -> (double)s.get("value"))).get();
map min = valuelist.stream().min(comparator.comparing(s -> (double)s.get("value"))).get();
double avg = valuelist.stream().maptodouble(s -> (double)s.get("value")).average().orelse(0d);
double sum = valuelist.stream().maptodouble(s -> (double)s.get("value")).sum();
1.过滤元素 - filter()
filter()方法根据给定的条件筛选出符合条件的元素,返回一个新的流。
示例:
list<dictionaries> dictionaries = list.stream().filter( s -> s.gettype().equals("0")).collect(collectors.tolist());
2.去重元素 - distinct()
distinct()方法对流中的元素进行去重
示例:
// 去重
list<dictionaries> dictionaries = list.stream().distinct().collect(collectors.tolist());
// 根据名称去重
list<dictionaries> dictionaries = list.stream().collect(
collectors.collectingandthen(
collectors.tocollection(() -> new treeset<>(comparator.comparing(work::getrepairdepart))), arraylist::new)
);
3.排序元素 - sorted()
sorted()方法对流中的元素进行排序,默认是按照自然顺序排序,也可以传入自定义的比较器;
示例:
// 升序排序
list<dictionaries> dictionaries = list.stream().sorted().collect(collectors.tolist());
// 降序排序
list<dictionaries> dictionaries = list.stream().sorted(comparator.reverseorder()).collect(collectors.tolist());
// 定制升序排序
list<dictionaries> dictionaries = list.stream().sorted(comparator.comparing(student::getage)).collect(collectors.tolist());
// 定制降序排序
list<dictionaries> dictionaries = list.stream().sorted(comparator.comparing(student::getage).reverseorder()).collect(collectors.tolist());
4.收集结果-collect()
collect()方法将流中的元素收集到一个集合中。
示例:
(1)按时间统计:
// 按小时统计
map<string, double> stationhh = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm-dd hh").format(item.gettime()), collectors.summingdouble(sensorrealtime::getvalue)));
// 按天统计
map<string, double> stationhh = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm-dd").format(item.gettime()), collectors.summingdouble(sensorrealtime::getvalue)));
// 按周统计
map<string, double> stationhh = list.stream().collect(collectors.groupingby(item -> getweeknumber(item.getteltime()), collectors.summingdouble(sensorrealtime::getvalue)));
// 按月统计
map<string, double> stationhh = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm").format(item.gettime()), collectors.summingdouble(sensorrealtime::getvalue)));
// 按年统计
map<string, double> stationhh = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy").format(item.gettime()), collectors.summingdouble(sensorrealtime::getvalue)));
// 获取周数
public string getweeknumber(date date){
localdate localdate = date.toinstant().atzone(zoneid.systemdefault()).tolocaldate();
int weeknumber = localdate.get(weekfields.iso.weekofmonth());
return integer.tostring(weeknumber);
}
(2)按时间分组:
// 按小时分组
map<string, list<callrecords>> listhh = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm-dd hh").format(item.getteltime())));
// 按天分组
map<string, list<callrecords>> listdd = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm-dd").format(item.getteltime())));
// 按周分组
map<string, list<callrecords>> listww = list.stream().collect(collectors.groupingby(item -> getweeknumber(item.getteltime())));
// 按月分组
map<string, list<callrecords>> listww = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy-mm").format(item.getteltime())));
// 按年分组
map<string, list<callrecords>> listww = list.stream().collect(collectors.groupingby(item -> new simpledateformat("yyyy").format(item.getteltime())));
// 获取周数
public string getweeknumber(date date){
localdate localdate = date.toinstant().atzone(zoneid.systemdefault()).tolocaldate();
int weeknumber = localdate.get(weekfields.iso.weekofmonth());
return integer.tostring(weeknumber);
}
其他:
sql 分时间查询:
下面例子中表名为tablename,条件字段名为inputdate
查询今天
select * from tablename where datediff(day,inputdate,getdate())=0
查询昨天
select * from tablename where datediff(day,inputdate,getdate())=1
查询本周
select * from tablename where datediff(week,inputdate,getdate())=0
查询上周
select * from tablename where datediff(week,inputdate,getdate())=1
查询本月
select * from tablename where datediff(month,inputdate,getdate())=0
查询上月
select * from tablename where datediff(month,inputdate,getdate())=1
查询本季度的
select * from t_interviewinfo where datediff(qq,inputdate,getdate())=0
发表评论