java8中特性使用stream() DateTimeFormatter等

一、 stream() 使用

1.1 排序

//单个正序排列  按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge)).collect(Collectors.toList());

//单个倒叙排列 按年龄排序(Integer类型)
List<StudentInfo> studentsSortName = studentList.stream().sorted(Comparator.comparing(StudentInfo::getAge).reversed()).collect(Collectors.toList());

 //多个排序条件 按年龄排序(Integer类型)
 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()//平均值
//String d    
OptionalDouble max = adjustRecords.stream().mapToDouble(aa -> Double.parseDouble(aa.getValue())).max();
                        highEle = max.getAsDouble();
//将一个list中对象某个字段提取成新list
List<Object> newList = objectList.stream().map(Object::getVar).collect(Collectors.toList());

1.3 分类 转化为map

//字段 -- list
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));

//s
long c = numList.stream().filter(e -> e % 2 == 0).count(); 


1.4 类型转换

// 数组转化为list
List<Integer> list1 = Arrays.stream(array).boxed().collect(Collectors.toList());
// list转化为数组
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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值