java lamda 时间操作
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date currentDate = new Date(System.currentTimeMillis());
Calendar calender = Calendar.getInstance();
calender.setTime(currentDate);
System.out.println("当天时间:"+currentDate.toLocaleString());
calender.add(Calendar.DATE, 3); //加3天
calender.add(Calendar.SECOND, -60); //减少60秒
Date dueTime=null;
try {
dueTime = simpleDateFormat.parse(simpleDateFormat.format(calender.getTime()).toString());
} catch (ParseException e) {
e.printStackTrace();
}
System.out.println("input into endDate:"+dueTime.toLocaleString());
java 8
LinkedHashMap<String, LogisticsCodeVo> codeMap = analyse.getCodeMap();
//获取所有数码产品
List<LogisticsCodeVo> logisticsList = codeMap.values().stream().collect(Collectors.toList());
//根据产品ID 分装数码
Map<Long, List<LogisticsCodeVo>> codesByproductIdMap = logisticsList.stream().collect(Collectors.groupingBy(LogisticsCodeVo::getProductId));
//所有有效数码
List<String> codes = codeMap.keySet().stream().collect(Collectors.toList());
Map<Long, ZdInProductParam> inProductParamMap = productItemList.stream().collect(Collectors.toMap(ZdInProductParam::getProductId, Function.identity()));
HashMap<String, LogisticsCodeVo> codeVoMap = (HashMap<String, LogisticsCodeVo>) logisticsCodeVo.stream().collect(Collectors.toMap(LogisticsCodeVo::getCode, Function.identity()));
LinkedHashMap<String, LogisticsCodeVo> codeHashMap = new LinkedHashMap<>();
//Map 转LinkedHashMap
codeVoMap.entrySet().stream().sorted(Map.Entry.comparingByKey()).forEachOrdered(x->codeHashMap.put(x.getKey(),x.getValue()));
String 转 Long
List<TestVo> listVo = new ArrayList<>();
List<String> sl = listVo.stream().map(TestVo :: getAge).collect(Collectors.toList());
sl.forEach(vo -> System.out.println(vo));
// 此处还有个mapToLong 但不知道杂用
List<Long> ll = listVo.stream().map(TestVo :: getAge).map(x -> Long.valueOf(x)).collect(Collectors.toList());
ll.forEach(vo -> System.out.println(vo));
}
@ApiModelProperty(value = "收货日期开始 yyyy-MM-dd")
@DateTimeFormat(pattern = "yyyy-MM-dd")
@JsonFormat(pattern = "yyyy-MM-dd")
private Date recDateStart;
List过滤
List<User> collect = list.stream().filter(u -> "北京".equals(u.getAddr())).collect(Collectors.toList());
List<QdStockSummaryDO> collect = initStack.stream().filter(u ->
DO.getChannelId().compareTo(u.getChannelId())==0 && DO.getProductId().compareTo(u.getProductId())==0
).collect(Collectors.toList());
String collect1 = list.stream().map(u -> u.getName()).collect(Collectors.joining(","));
List<String> strs=persons.stream().map(person -> person.getName()).collect(Collectors.toList());
// 将Person集合转化为Student集合
List<Student> students = persons.stream().map(person -> {
Student student = new Student();
BeanUtils.copyProperties(person, student);
if (person.getName() == "张三") {
student.setSchoolName("三中");
student.setsId(3L);
}
if (person.getName() == "李四") {
student.setSchoolName("四中");
student.setsId(4L);
}
return student;
}).collect(Collectors.toList());
System.out.println("students = " + students);
#去重
List<String> myList =
listAll.stream().distinct().collect(Collectors.toList());
// 对象根据name去重
List<Person> unique = persons.stream().collect(Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Person::getName))), ArrayList::new));
unique.forEach(p -> System.out.println(p));
lambda 统计list中对象的重复次数
List<Integer> list = new ArrayList() {
{
add(12);
add(20);
add(12);
}
};
Map<Integer, Long> map = list.stream().collect(Collectors.groupingBy(p -> p,Collectors.counting()));
map.forEach((k, v) -> System.out.println(k + ":" + v));
批量修改List属性的值
list.stream().map(p ->p.setIsDelete(JcChannelDeleteConstant.DELETED)).collect(Collectors.toList());
list.forEach(p->p.setIsDelete(1));