文章目录
🍃Java 8 新特性
🌈1. Lambda 表达式
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.forEach((Integer number) -> System.out.println(number));
🌈2. 函数式接口和默认方法
✨函数式接口
以下是一个示例:
@FunctionalInterface
interface MyFunction {
int operate(int a, int b);
default void printMessage() {
System.out.println("Hello from functional interface!");
}
}
public class Main {
public static void main(String[] args) {
MyFunction add = (a, b) -> a + b;
int result = add.operate(3, 5); // 结果为 8
System.out.println(result);
add.printMessage(); // 输出: Hello from functional interface!
}
}
✨默认方法
🌈3.Stream API:
以下是一些常用的Stream API的方法和用法:
✨1.创建流
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Stream<Integer> stream = numbers.stream();
✨2. 中间操作
stream.filter(n -> n % 2 == 0) // 过滤偶数
.map(n -> n * 2) // 映射为原值的两倍
.sorted() // 排序
✨3. 终端操作
stream.collect(Collectors.toList()) // 收集为列表
.forEach(System.out::println); // 遍历并打印输出
✨4. 并行流
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.parallelStream()
.map(n -> n * 2)
.forEach(System.out::println);
✨5. 无限流
Stream.generate(() -> "Hello")
.limit(10) // 限制生成的元素个数
.forEach(System.out::println);
🌈4. 日期时间 API
以下是一些常用的Java 8日期时间API的功能:
✨1. LocalDate
LocalDate date = LocalDate.now(); // 当前日期
System.out.println(date); // 输出当前日期
✨2. LocalTime
LocalTime time = LocalTime.now(); // 当前时间
System.out.println(time); // 输出当前时间
✨3. LocalDateTime
LocalDateTime dateTime = LocalDateTime.now(); // 当前日期和时间
System.out.println(dateTime); // 输出当前日期和时间
✨4. ZonedDateTime
ZonedDateTime zonedDateTime = ZonedDateTime.now(); // 当前日期和时间(带时区)
System.out.println(zonedDateTime); // 输出当前日期和时间(带时区)
✨5. Duration和Period
Duration duration = Duration.ofMinutes(30); // 表示30分钟
Period period = Period.ofDays(7); // 表示7天
✨6. 格式化和解析
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
LocalDate date = LocalDate.parse("2022-01-01", formatter);
String dateString = date.format(formatter);
System.out.println(dateString); // 输出:2022-01-01
✨7. 操作和计算
LocalDate newDate = date.plusDays(5); // 加上5天
long daysBetween = ChronoUnit.DAYS.between(date, newDate); // 计算两个日期之间的天数差
✨8. 时区处理
ZonedDateTime zonedDateTime = ZonedDateTime.of(dateTime, ZoneId.of("America/New_York"));
ZonedDateTime adjustedDateTime = zonedDateTime.withZoneSameInstant(ZoneId.of("Asia/Tokyo"));
🌈方法引用
方法引用可以通过以下几种形式来表示:
✨1. 静态方法引用:类名::静态方法名
// 静态方法
void printMessage(String message) {
System.out.println(message);
}
// 方法引用
Consumer<String> consumer = MyClass::printMessage;
consumer.accept("Hello"); // 调用printMessage方法输出"Hello"
✨2. 实例方法引用:对象::实例方法名
// 实例方法
class MyClass {
void printMessage(String message) {
System.out.println(message);
}
}
// 方法引用
MyClass myObj = new MyClass();
Consumer<String> consumer = myObj::printMessage;
consumer.accept("Hello"); // 调用myObj的printMessage方法输出"Hello"
✨3. 特定类型的实例方法引用:特定类名::实例方法名
// 特定类型的实例方法
List<String> strings = Arrays.asList("A", "B", "C");
strings.sort((s1, s2) -> s1.compareToIgnoreCase(s2));
// 方法引用
strings.sort(String::compareToIgnoreCase); // 使用String类的compareToIgnoreCase方法进行排序
✨4. 构造函数引用:类名::new
// 构造函数引用
Supplier<List<String>> supplier = ArrayList::new;
List<String> list = supplier.get(); // 创建一个ArrayList实例
🌈 Optional类
✨1. 创建Optional对象
String str = "Hello";
Optional<String> optional = Optional.of(str); // 创建包含非空值的Optional对象
String str = null;
Optional<String> optional = Optional.ofNullable(str); // 创建可能为空的Optional对象
✨2. 判断Optional是否包含值
if (optional.isPresent()) {
System.out.println("Value is present");
} else {
System.out.println("Value is absent");
}
✨3. 获取Optional的值
String value = optional.get(); // 获取Optional对象中的值
System.out.println(value);
✨4. 处理Optional的值
optional.ifPresent(System.out::println); // 如果Optional对象非空,则打印值
✨5. 使用默认值
String value = optional.orElse("Default Value"); // 获取Optional对象中的值,如果为空,则返回默认值
System.out.println(value);