方法引用的前提:(Lambda体中调用方法的参数列表和返回值类型)要和(函数式接口中抽象方法的参数列表和返回值类型)一致
方法引用三种语法格式
类::实例方法名
规则:若Lambda参数列表中的第一参数是实例方法的调用者,而第二个参数是实例方法的参数时,可以使用ClassName::method
格式:ClassName::new
构造器引用的前提:需要调用的构造器的参数列表与函数时接口中抽象方法的参数列表保持一致
格式:Type::new
流(Stream)是数据渠道,用于操作数据源(集合、数组等)所生成的元素序列
注意:
Stream操作的三个步骤
flatMap用法示例:
@Test
public void test3() {
List<String> list = Arrays.asList("aaa", "bbb", "ccc", "ddd");
// 不用flatMap
Stream<Stream<Character>> stream = list.stream()
.map(method1::filterCharacter);
stream.forEach((sm) -> {
sm.forEach(System.out::println);
});
// 使用flatMap
Stream<Character> characterStream = list.stream()
.flatMap(method1::filterCharacter);
characterStream.forEach(System.out::println);
}
public static Stream<Character> filterCharacter(String str) {
List<Character> list = new ArrayList<>();
for (Character ch : str.toCharArray()) {
list.add(ch);
}
return list.stream();
}
结果均为:
List<Employee> employees = Arrays.asList(
new Employee("张三", 18, 9999.99),
new Employee("李四", 58, 5555.55),
new Employee("王五", 26, 3333.33),
new Employee("赵六", 36, 6666.66),
new Employee("田七", 12, 8888.88)
);
@Test
public void test4() {
List<String> list = Arrays.asList("ccc", "aaa", "bbb", "ddd");
// 自然排序
list.stream()
.sorted()
.forEach(System.out::println);
// 定制排序-->年龄相同按照姓名排序,年龄不同就按照年龄排序
employees.stream()
.sorted((e1, e2) -> {
if (e1.getAge().equals(e2.getAge())) {
return e1.getName().compareTo(e2.getName());
} else {
return e1.getAge().compareTo(e2.getAge());
}
}).forEach(System.out::println);
}
结果:
@Test
public void testLocalDate() {
// 获取当前日期
LocalDate localDate = LocalDate.now();
System.out.println("localDate = " + localDate);
LocalTime localTimeNow = LocalTime.now();
System.out.println("localTimeNow = " + localTimeNow);
LocalDateTime localDateTimeNow = LocalDateTime.now();
System.out.println("localDateTimeNow = " + localDateTimeNow);
// 指定日期进行返回
LocalDate localDateOf = LocalDate.of(2023, 10, 12);
System.out.println("localDateOf = " + localDateOf);
LocalTime localTimeOf = LocalTime.of(20, 10, 20);
System.out.println("localTimeOf = " + localTimeOf);
LocalDateTime localDateTimeOf = LocalDateTime.of(2023, 10, 22, 20, 10, 20);
System.out.println("localDateTimeOf = " + localDateTimeOf);
// 对日期进行加减 三种类型差不多这里只对一种进行举例
LocalDateTime localDateTimeNow2 = localDateTimeNow.plusDays(1);
System.out.println("localDateTimeNow2 = " + localDateTimeNow2);
LocalDateTime localDateTimeNow3 = localDateTimeNow.minusDays(1);
System.out.println("localDateTimeNow3 = " + localDateTimeNow3);
// 获取时间对应具体信息
int hour = localDateTimeNow.getHour();
System.out.println("hour = " + hour);
// 将字符串转换为日期
String stringTime = "2023-10-22T22:29:10";
LocalDateTime parse = LocalDateTime.parse(stringTime);
System.out.println("parse = " + parse);
String stringTime2 = "2023-10-22 22:29:10";
LocalDateTime parse1 = LocalDateTime.parse(stringTime2, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
System.out.println("parse1 = " + parse1);
// 结果
// localDate = 2023-12-28
// localTimeNow = 09:49:32.055
// localDateTimeNow = 2023-12-28T09:49:32.055
// localDateOf = 2023-10-12
// localTimeOf = 20:10:20
// localDateTimeOf = 2023-10-22T20:10:20
// localDateTimeNow2 = 2023-12-29T09:49:32.055
// localDateTimeNow3 = 2023-12-27T09:49:32.055
// hour = 9
// parse = 2023-10-22T22:29:10
// parse1 = 2023-10-22T22:29:10
}
@Test
public void instantTest() {
//获取当前时间 时间以UTC时间展示,与中国时间差距8小时
Instant instant = Instant.now();
System.out.println("UTC时间:"+instant);
//中国时间 可以通过设置偏移量来获取
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println("中国时间:"+offsetDateTime);
// 获取当前时间戳
long l = instant.toEpochMilli();
System.out.println("当前时间戳 = " + l);
/*UTC时间:2023-12-28T09:07:26.512Z
中国时间:2023-12-28T17:07:26.512+08:00
当前时间戳 = 1703754446512*/
}
@Test
public void durationTest() {
Instant instant = Instant.now();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
Instant instant1 = Instant.now();
Duration duration = Duration.between(instant, instant1);
System.out.println("duration.getSeconds() = " + duration.getSeconds());
System.out.println("duration.toMillis() = " + duration.toMillis());
// duration.getSeconds() = 1
// duration.toMillis() = 1004
LocalDateTime localDateTimeNow = LocalDateTime.now();
LocalDateTime localDateTimeOf = LocalDateTime.of(2023, 12, 26, 20, 10, 20);
Duration between = Duration.between(localDateTimeOf, localDateTimeNow);
System.out.println("between.toDays() = " + between.toDays());
System.out.println("between.toHours() = " + between.toHours());
// between.toDays() = 1
// between.toHours() = 45
}
@Test
public void periodTest() {
LocalDate localDate = LocalDate.now();
LocalDate localDateOf = LocalDate.of(2023, 10, 12);
Period between = Period.between(localDateOf, localDate);
System.out.println("between.getMonths() = " + between.getMonths());
System.out.println("between.getDays() = " + between.getDays());
// between.getMonths() = 2
// between.getDays() = 16
}
@Test
public void test1() {
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println("当前时间:"+localDateTime);
LocalDateTime localDateTime1 = localDateTime.withDayOfMonth(10);
System.out.println("手动指定日期时间:"+localDateTime1);
//获取当月第一天
LocalDateTime with = localDateTime.with(TemporalAdjusters.firstDayOfMonth());
System.out.println("使用TemporalAdjuster获取当月第一天:"+with);
//获取下一年的第一天
System.out.println(localDateTime.with(TemporalAdjusters.firstDayOfNextYear()));
//获取年中第一天
System.out.println(localDateTime.with(TemporalAdjusters.lastDayOfYear()));
//获取月中最后一天
System.out.println(localDateTime.with(TemporalAdjusters.lastDayOfMonth()));
//获取下个星期一
System.out.println(localDateTime.with(TemporalAdjusters.next(DayOfWeek.MONDAY)));
// 当前时间:2023-12-28T17:24:39.326
// 手动指定日期时间:2023-12-10T17:24:39.326
// 使用TemporalAdjuster获取当月第一天:2023-12-01T17:24:39.326
// 2024-01-01T17:24:39.326
// 2023-12-31T17:24:39.326
// 2023-12-31T17:24:39.326
// 2024-01-01T17:24:39.326
}
@Test
public void test1() {
//本身内置了很多格式化格式
DateTimeFormatter formatter = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
// 也可自定义格式
DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("yyyyMMdd HH:mm:ss");
//当前时间
LocalDateTime dateTime = LocalDateTime.now();
/**
* 两种写法效果一样
*/
System.out.println("格式化日期一:"+formatter.format(dateTime));
System.out.println("格式化日期二:"+dateTime.format(formatter));
System.out.println("自定义格式化日期一:"+formatter1.format(dateTime));
System.out.println("自定义格式化日期二:"+dateTime.format(formatter1));
//字符串转换时间
String format = dateTime.format(formatter1);
LocalDateTime parse = LocalDateTime.parse(format, formatter1);
System.out.println("自定义格式化日期三:"+parse);
// 格式化日期一:2023-12-28T17:27:55.202
// 格式化日期二:2023-12-28T17:27:55.202
// 自定义格式化日期一:20231228 17:27:55
// 自定义格式化日期二:20231228 17:27:55
// 自定义格式化日期三:2023-12-28T17:27:55
}
参考其他博主大佬的文章重复注解与类型注解