1.求符合条件的数值求和
List<ProductGatherVo> plists = smallTypeDao.getDataByDate(sDate,eDate,orderFlag,smallTypeId,version);
if(plists != null && plists.size() > 0) {
for(String str : dateLists) {
int startTime = Integer.valueOf(str.substring(0, 8));
int endTime = Integer.valueOf(str.substring(9));
int sum = plists.stream().filter(p -> startTime <= p.getTime() && endTime >= p.getTime())
.mapToInt(ProductGatherVo :: getUvCount).sum();
lists.add(sum);
}
}else {
//plists查询为空数据后,返回0
for(String s : dateLists) {
lists.add(0);
}
}
2.求复合要求的值并修改集合中的值返回新的list
List<String> weekChListCt = getWeekChTextList(currentYear);
List<String> weekChListLt = getWeekChTextList(currentYear-1);
//s : 第1周(01.01~01.07) ,当前年要大于或等于0101 取当前年已有的值
List<String> weekChListCurrent =weekChListCt.stream().filter(s -> currentMonthDay >=
Integer.valueOf(s.substring(s.indexOf("(")+1, s.indexOf("~")).replace(".", "")))
.map(st->{return currentYear +" "+ st;})
.collect(Collectors.toList());
//s : 第1周(01.01~01.07) ,上一年要小于于或等于0107 取上一年往后的值
List<String> weekChListLast =weekChListLt.stream().filter(s -> currentMonthDay <=
Integer.valueOf(s.substring(s.indexOf("~")+1,s.indexOf(")")).replace(".", "")))
.map(st->{return currentYear-1 +" "+ st;})
.collect(Collectors.toList());
3.将集合中的值倒序
/**
* 获取上一年的所有年月
* @return
*/
public static List<String> getSelectMonthChTextList(){
List<String> list = new ArrayList<String>();
Calendar c = Calendar.getInstance();
for(int i = 0; i < 12; i ++){
int k = c.get(Calendar.YEAR);
int j = c.get(Calendar.MONTH) + 1 - i;
String date = "";
if(j >= 1){
date = k + (j >= 10 ? "" : "0") + j;
} else {
int p = 11 - i;//剩余循环次数
int m = c.get(Calendar.YEAR) - 1;
int n = c.get(Calendar.MONTH) + 2 + p;
date = m + (n >= 10 ? "" : "0") + n;
}
list.add(date);
}
return list.stream().sorted().collect(Collectors.toList());
}