一、 stream() 使用
1.1 排序
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());
List<StudentInfo> studentsSortName = studentList.stream() .sorted(Comparator.comparing(StudentInfo::getAge).reversed().thenComparing(StudentInfo::getHeight)).collect(Collectors.toList());
1.2 取值
Sensor sensor1 = sensorMongoList.stream().max(Comparator.comparing(Sensor::getNum)).get();
list.stream().mapToDouble(User::getHeight).sum()
list.stream().mapToDouble(User::getHeight).max()
list.stream().mapToDouble(User::getHeight).min()
list.stream().mapToDouble(User::getHeight).average()
OptionalDouble max = adjustRecords.stream().mapToDouble(aa -> Double.parseDouble(aa.getValue())).max();
highEle = max.getAsDouble();
List<Object> newList = objectList.stream().map(Object::getVar).collect(Collectors.toList());
1.3 分类 转化为map
Map<String, List<Student>> sellerList = stus.stream().collect(Collectors.groupingBy(Student::getName, Collectors.toList()));
Map<Object, Object> map = objectList.stream().collect(Collectors.toMap(Object::getVar, Object::getVar1));
Map<Object, Object> map = objectList.stream().collect(Collectors.toMap(Object::getVar, object -> object));
long c = numList.stream().filter(e -> e % 2 == 0).count();
1.4 类型转换
List<Integer> list1 = Arrays.stream(array).boxed().collect(Collectors.toList());
int[] intArr1 = list.stream().mapToInt(Integer::valueOf).toArray();
1.5 过滤
List<PersonModel> collect = data
.stream()
.filter(person -> "男".equals(person.getSex()))
.collect(toList());
System.out.println(collect);
二、时间转换 DateTimeFormatter
参考:JAVA8 LocalDateTime
时间 使用案例
DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
DateTimeFormatter df1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String str="2021-10-01 08:00:00";
LocalDateTime ldt= LocalDateTime.parse(str,df);
System.out.println("String类型的时间转成LocalDateTime:"+ldt);
System.out.println("LocalDateTime按照DateTimeFormatter格式化:"+ldt.format(df1));
System.out.println("LocalDateTime按照DateTimeFormatter格式化:"+df1.format(ldt));
LocalDateTime newLdt= LocalDateTime.of(2021,11,11,0,0,0);
System.out.println("LocalDateTime创建时间:"+newLdt);
Date date = new Date();
Instant instant = date.toInstant();
ZoneId zoneId = ZoneId.systemDefault();
LocalDateTime newLdt1 = instant.atZone(zoneId).toLocalDateTime();
System.out.println("Data转化为LocalDateTime:"+newLdt1);
System.out.println("获取指定日期前一天:"+newLdt1.minusDays(1));
System.out.println("获取指定日期后一天:"+newLdt1.plusDays(1));
LocalDate lastMonth = LocalDate.now().minusMonths(1);
LocalDate firstDayLastMonth = lastMonth.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("上个月的第一天为:"+firstDayLastMonth);
LocalDate lastDayLastMonth = lastMonth.with(TemporalAdjusters.lastDayOfMonth());
System.out.println("上个月的最后一天为:"+lastDayLastMonth);