java不同版本特性示例

This commit is contained in:
maxf
2025-08-05 18:53:13 +08:00
parent 5c87319677
commit a4be814442
66 changed files with 1619 additions and 101 deletions

View File

@@ -0,0 +1,40 @@
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author maxiaofeng
* @date 2025/8/5 13:53
*/
public class DateTimeExample {
/**
* 新的日期时间API示例
* Java 8引入了新的日期时间API解决了之前Date和Calendar类的缺陷
*/
static void dateTimeExample() {
System.out.println("\n=== 新日期时间API示例 ===");
// LocalDate示例 - 只包含日期
LocalDate today = LocalDate.now();
System.out.println("今天的日期: " + today);
LocalDate specificDate = LocalDate.of(2025, 7, 18);
System.out.println("指定日期: " + specificDate);
// LocalDateTime示例 - 包含日期和时间
LocalDateTime now = LocalDateTime.now();
System.out.println("当前日期时间: " + now);
// 日期格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formattedDate = now.format(formatter);
System.out.println("格式化后的日期时间: " + formattedDate);
// 日期计算
LocalDate nextWeek = today.plusWeeks(1);
System.out.println("下周的日期: " + nextWeek);
LocalDate lastMonth = today.minusMonths(1);
System.out.println("上个月的日期: " + lastMonth);
}
}

View File

@@ -0,0 +1,36 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
/**
* @author maxiaofeng
* @date 2025/8/5 13:53
*/
public class FunctionExample {
/**
* 函数式接口示例
* Java 8提供了许多内置的函数式接口
*/
static void functionalInterfaceExample() {
System.out.println("\n=== 函数式接口示例 ===");
// Predicate示例 - 用于判断
Predicate<Integer> isEven = n -> n % 2 == 0;
System.out.println("10是偶数吗? " + isEven.test(10));
System.out.println("11是偶数吗? " + isEven.test(11));
// Consumer示例 - 用于消费数据
Consumer<String> printer = System.out::println;
printer.accept("使用Consumer打印信息");
// Function示例 - 用于转换
java.util.function.Function<String, Integer> stringToLength = String::length;
int length = stringToLength.apply("Java 8");
System.out.println("字符串'Java 8'的长度: " + length);
// Supplier示例 - 用于生成数据
java.util.function.Supplier<Double> randomSupplier = Math::random;
System.out.println("随机数: " + randomSupplier.get());
}
}

View File

@@ -0,0 +1,51 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* @author maxiaofeng
* @date 2025/8/5 13:51
*/
public class LambdaExample {
/**
* Lambda表达式示例
* Lambda表达式是Java 8最重要的新特性之一它允许把函数作为一个方法的参数
*/
public static void lambdaExample() {
System.out.println("\n=== Lambda表达式示例 ===");
// 传统的匿名内部类写法
Runnable oldRunnable = new Runnable() {
@Override
public void run() {
System.out.println("传统方式执行Runnable");
}
};
// Lambda表达式写法
Runnable newRunnable = () -> System.out.println("Lambda方式执行Runnable");
oldRunnable.run();
newRunnable.run();
// 集合排序的Lambda表达式示例
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
// 传统方式
Collections.sort(names, new Comparator<String>() {
@Override
public int compare(String a, String b) {
return a.compareTo(b);
}
});
// Lambda方式
Collections.sort(names, (a, b) -> a.compareTo(b));
// 更简洁的Lambda方式
names.sort((a, b) -> b.compareTo(a)); // 逆序排列
System.out.println("Lambda排序结果: " + names);
}
}

View File

@@ -0,0 +1,26 @@
/**
* @author maxiaofeng
* @date 2025/7/18 10:06
*/
public class Main {
public static void main(String[] args) {
System.out.println("hello java8.");
// 1. Lambda表达式示例
LambdaExample.lambdaExample();
// 2. Stream API示例
StreamExample.streamExample();
// 3. Optional示例
OptionalExample.optionalExample();
// 4. 新的日期时间API示例
DateTimeExample.dateTimeExample();
// 5. 函数式接口示例
FunctionExample.functionalInterfaceExample();
}
}

View File

@@ -0,0 +1,35 @@
import java.util.Optional;
/**
* @author maxiaofeng
* @date 2025/8/5 13:52
*/
public class OptionalExample {
/**
* Optional示例
* Optional类是一个可以为null的容器对象用来避免空指针异常
*/
public static void optionalExample() {
System.out.println("\n=== Optional示例 ===");
// 创建Optional对象
Optional<String> optional = Optional.of("Hello Java 8");
Optional<String> emptyOptional = Optional.empty();
// 判断是否有值
if (optional.isPresent()) {
System.out.println("Optional中的值: " + optional.get());
}
// 使用orElse提供默认值
String value = emptyOptional.orElse("默认值");
System.out.println("Optional默认值: " + value);
// 使用ifPresent执行操作
optional.ifPresent(s -> System.out.println("处理值: " + s.toUpperCase()));
// 使用map转换值
Optional<String> upperCase = optional.map(String::toUpperCase);
upperCase.ifPresent(s -> System.out.println("转换后的值: " + s));
}
}

View File

@@ -0,0 +1,83 @@
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
* @author maxiaofeng
* @date 2025/8/5 13:52
*/
public class StreamExample {
/**
* Stream API示例
* Stream API是Java 8中处理集合的关键抽象概念它可以对集合进行各种操作
*/
public static void streamExample() {
System.out.println("\n=== Stream API示例 ===");
List<Person> people = Arrays.asList(
new Person("Alice", 25),
new Person("Bob", 30),
new Person("Charlie", 35),
new Person("David", 40),
new Person("Eve", 20)
);
// 过滤年龄大于25的人
List<Person> filtered = people.stream()
.filter(person -> person.getAge() > 25)
.collect(Collectors.toList());
System.out.println("年龄大于25的人: " + filtered);
// 提取所有人的姓名
List<String> names = people.stream()
.map(Person::getName)
.collect(Collectors.toList());
System.out.println("所有人的姓名: " + names);
// 计算年龄总和
int totalAge = people.stream()
.mapToInt(Person::getAge)
.sum();
System.out.println("年龄总和: " + totalAge);
// 查找第一个年龄大于30的人
Optional<Person> person = people.stream()
.filter(p -> p.getAge() > 30)
.findFirst();
person.ifPresent(p -> System.out.println("第一个年龄大于30的人: " + p));
// 并行流示例
long count = people.parallelStream()
.filter(p -> p.getAge() > 25)
.count();
System.out.println("年龄大于25的人数: " + count);
}
/**
* 人员实体类
*/
public static class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public String toString() {
return "Person{name='" + name + "', age=" + age + "}";
}
}
}