1. Lambda表达式:允许一个函数作为方法参数传递到方法中去
(parm)->expression;
或
(parm)->{expression;}
- 实操
Callable<Integer> integerCallable = () -> 5;
IntFunction<Integer> integerIntFunction = (int x) -> (3 * x);
IntBinaryOperator intBinaryOperator = (int a, int b) -> a + b;
Consumer<String> stringConsumer = (String s) -> {
System.out.println(s);
};
2. Stream:数据源的元素队列,支持聚合操作(想象成流)
-
数据源:集合,数组
-
聚合操作:filter,map,reduce,find,match,sorted
-
实操
-
filter(过滤)
List<String> list = new ArrayList(); list.add("A"); list.add(""); list.add("B"); System.out.println(list); List<String> collect = list.stream().filter(x -> !x.isEmpty()).collect(Collectors.toList());
-
forEach(循环)
list.stream().forEach(x-> System.out.println(x));
-
map(做映射)
List<Integer> collect1 = list1.stream().map(x -> x * x).collect(Collectors.toList());
-
limit(截取指定数量)
List<Integer> collect2 = list1.stream().limit(2).collect(Collectors.toList());
-
sorted(排序)
List<Integer> collect3 = list1.stream().sorted((a, b) -> b.compareTo(a)).collect(Collectors.toList());
-
Collectors(工具类)
Collectors.toList() Collectors.toSet() Collectors.toMap()
-
mapToInt(统计操作,max,min,avg,sum)
IntSummaryStatistics intSummaryStatistics = list1.stream().mapToInt(a -> a).summaryStatistics(); System.out.println(intSummaryStatistics.getMin());
-
3. 接口中提供默认方法和静态方法
- 默认方法
public interface DefaultInterfaceDemo { default void eat(String name){ System.out.println(name); } }
- 静态方法
static void eat(String name,int count){ System.out.println(name); }
4. 新的 date api
-
旧版存在问题:
-
设计较差(java.util,java.sql 都有date)
-
非线程安全的
-
处理时区比较麻烦
-
-
新版提供:
-
Local(本地):简化了日期处理,没有时区的问题
-
Zoned(时区):指定时区处理时间
-
//获取当前时间
LocalDateTime now = LocalDateTime.now();
System.out.println(now);
//获取时间的年月日
System.out.println(now.getYear() +""+ now.getMonthValue() + now.getDayOfMonth());
//解析
DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
LocalDateTime parse = LocalDateTime.parse("2025-10-11 11:11:12", pattern);
System.out.println(parse);
ZoneId zoneId = ZoneId.systemDefault();
System.out.println(zoneId);
ZoneId of = ZoneId.of("Asia/Shanghai");
System.out.println(of);