JDK1.8的新特性

最近在开发过程中,接触了一部分JDK1.8的新特性。

发现JDK1.8在集合中新增了Stream API功能以及新的时间类,大大减少了代码量,提高了效率。
在这里插入图片描述

下面说一下,把所遇到的知识大概整理一下

一、Stream API

1.1 分组

根据字段column将指定对象Obj的集合objList分组,转换为objMap,将字段作为Key,分组后的集合为Value

Map<String,List<Obj>> objMap = objList.stream().collect(Collectors.groupingBy(Obj::getColumn))

例如:
List<Student> students(学生集合)根据classId(班级id)分组

Map<String,List<Student>> studentMap = students.stream().collect(Collectors.groupingBy(Student::getClassId))

1.2 筛选

获取指定对象Obj集合objList中指定的字段column的集合

List<字段类型> columnList = objList.stream().map(Obj::getColumn).collect(Collectors.toList())

例如:
List<Student> students(学生集合)获取学生姓名(name)的集合

List<String> nameList = students.stream().map(Student::getName).collect(Collectors.toList())

1.3 去重

获取指定对象Obj集合objList中指定的字段column的集合,且集合中不存在重复的值

List<字段类型> columnList = objList.stream().map(Obj::getColumn).distinct().collect(Collectors.toList())

例如:
查询List<Student> students(学生集合)中来自哪些城市(city)

List<String> cityList = students.stream().map(Student::getCity).distinct().collect(Collectors.toList())

1.4 排序

集合list中的元素为基本类型时,可以通过sort方法进行排序。
jdk1.8 赋予了当前集合元素为对象时,可以根据对象的属性进行排序

//升序
sort(Comparator.comparing(类型::获取属性方法));
//降序
sort(Comparator.comparing(类型::获取属性方法)).reversed();

1.5 极值对象

通过max或者min方法,取得指定属性为最大或者最小的对象

例如:
获取List<Student> students(学生集合)中年龄最大和最小的学生

//获取年龄最大的学生
Student oldStudent = students.stream.max(Comparator.comparing(Student::getAge)).get();
//获取年龄最小的学生
Student youngStudent = students.stream.min(Comparator.comparing(Student::getAge)).get();

1.6 属性批量操作

对集合中的每个对象进行批量操作

students.stream.map(s->{
	s.setSchool("春天花花幼儿园");
	return s;
})

二、时间类

JDK1.8 引入了以下三个新的日期类

  • LocatDate
  • LocalTime
  • LocatDateTime

2.1 为什么使用新的日期类

以前的时间类如果不进行格式化,打印出来的效果不好,以Date为例:

System.out.println(new Date());

打印结果:

Fri May 07 09:20:18 CST 2021

使用SimpleDateFormat进行格式化

SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
System.out.println(simpleDateFormat.format(new Date()));

但SimpleDateFormat是线程不安全的

2.2 LocalDate

对年月日进行处理。
除了跟Date一样具有获取和设置年、月、日的方法,LocalDate还增加了额外的方法:

查询年份日期——
getDateOfYear()该日期为对应年第几天
getDateOfMonth()该日期为对应月第几天
getDateOfWeek()该日期为对应周第几天
判断、比较——
isLeapYear判断是否是闰年
lengthOfYear获取年份总天数,用来判断是366天还是365天
lengthOfMonth获取当月总天数
compareTo(LocalDate otherDay)当前日期和参数日期otherDay比较日期大小
isBefore(LocalDate otherDay)比较是否早于参数日期
isAfter(LocalDate otherDay)比较是否晚于参数日期
isEqual(LocalDate otherDay)比较两个日期是否相等
增加——
plusYear(long years)添加年数
plusMonth(long months)添加年数
plusWeek(long weeks)添加周数
plusDays(long days)添加天数
减少——
minusYear(long years)减少年数
minusMonth(long months)减少年数
minusWeek(long weeks)减少周数
minusDays(long days)减少天数

2.3 LocalTime

对时分秒纳秒进行处理。

添加——
plusHours(long hours)将当前时间加一小时
plusMinutes(long minutes)将当前时间加一分钟
plusSeconds(long seconds)将当前时间加一秒
减少——
minusHours(long hours)将当前时间减一小时
minusMinutes(long minutes)将当前时间减一分钟
minusSeconds(long seconds)将当前时间减一秒

2.4 LocalDateTime

同时处理年月日和时分秒纳秒。
基本上面两个类的方法它都有,可以对数据库的date和datetime两个类型的数据进行操作。

补充一个有关LocalDate转换为LocalDateTime的方法,主要是将LocalDate的数据转换为时间为0点0分0秒的LocalDateTime的数据

LocalDateTime time = LocalDateTime.of(LocalDate.now(),LocalTime.Min());

基本就是这些,有关的内容等我后面接触和学习后再补充;
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值