-
- 1. Lambda表达式:简化匿名内部类
-
- 1.1 传统方式 vs Lambda表达式
- 1.2 集合遍历对比
- 1.3 事件监听器简化
- 2. Stream API:革命性的集合操作
-
- 2.1 基本Stream操作示例
- 2.2 数值流操作
- 2.3 分组和分区
- 3. Optional:优雅处理null
-
- 3.1 基本Optional用法
- 3.2 Optional实践示例
- 4. 方法引用:更简洁的Lambda
-
- 5. 新的日期时间API
-
- 5.1 基本日期操作
- 5.2 日期格式化和解析
- 5.3 日期时间段计算
- 6. 默认方法和静态方法
-
- 7. 并行流提高性能
-
- 7.1 顺序流 vs 并行流
- 7.2 并行流注意事项
- 8. CompletableFuture:异步编程
-
- 8.1 基本异步操作
- 8.2 组合多个Future
- 9. 综合重构示例
-
- 9.1 传统代码示例
- 9.2 Java 8重构版本
- 10. 性能考虑与最佳实践
-
- 10.1 Stream性能测试
- 10.2 Java 8最佳实践
- 11. 常见问题与解决方案
-
- 11.1 调试Lambda表达式
- 11.2 异常处理
- 12. 总结与进一步学习
-
1. Lambda表达式:简化匿名内部类
1.1 传统方式 vs Lambda表达式
Runnable oldRunnable = new Runnable() {
@Override
public void run() {
System.out.println("Running in old way");
}
};
Runnable newRunnable = () -> System.out.println("Running with Lambda");
1.2 集合遍历对比
List<String> languages = Arrays.asList("Java", "Python", "C++", "JavaScript");
for (String lang : languages) {
System.out.println(lang);
}
languages.forEach(lang -> System.out.println(lang));
languages.forEach(System.out::println);
1.3 事件监听器简化
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("Button clicked!");
}
});
button.addActionListener(e -> System.out.println("Button clicked!"));
2. Stream API:革命性的集合操作
2.1 基本Stream操作示例
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
List<String> longNames = names.stream()
.filter(name -> name.length() > 4)
.collect(Collectors.toList());
List<Integer> nameLengths = names.stream()
.map(String::length)
.collect(Collectors.toList());
List<String> sortedNames = names.stream()
.sorted()
.collect(Collectors.toList());
2.2 数值流操作
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
int sum = numbers.stream().reduce(0, Integer::sum);
double average = numbers.stream()
.mapToInt(Integer::intValue)
.average()
.orElse(0.0);
int max = numbers.stream().max(Integer::compare).orElse(0);
2.3 分组和分区
class Person {
String name;
int age;
String city;
}
List<Person> people = Arrays.asList(
new Person("Alice", 25, "New York"),
new Person("Bob", 30, "Chicago"),
new Person("Charlie", 25, "New York"),
new Person("David", 30, "Chicago")
);
Map<String, List<Person>> peopleByCity = people.stream()
.collect(Collectors.groupingBy(Person::getCity));
Map<Boolean, List<Person>> partitionedPeople = people.stream()
.collect(Collectors.partitioningBy(p -> p.getAge() > 25));
3. Optional:优雅处理null
3.1 基本Optional用法
public String getCityTraditional(Person person) {
if (person != null) {
Address address = person.getAddress();
if (address != null) {
return address.getCity();
}
}
return "Unknown";
}
public String getCityWithOptional(Person person) {
return Optional.ofNullable(person)
.map(Person::getAddress)
.map(Address::getCity)
.orElse("Unknown");
}
3.2 Optional实践示例
List<String> possibleNullList = getPossibleNullList();
String result = null;
if (possibleNullList != null && !possibleNullList.isEmpty()) {
result = possibleNullList.get(0);
}
String result = Optional.ofNullable(possibleNullList)
.filter(list -> !list.isEmpty())
.map(list -> list.get(0))
.orElse("default");
4. 方法引用:更简洁的Lambda
4.1 四种方法引用类型
Function<String, Integer> parser = Integer::parseInt;
List<String> strings = Arrays.asList("a", "b", "c");
strings.forEach(System.out::println);
StringComparator myComparator = new StringComparator();
strings.sort(myComparator::compare);
Supplier<List<String>> listSupplier = ArrayList::new;
4.2 方法引用实践
List<String> names = Arrays.asList("alice", "bob", "charlie");
List<String> upperNames = names.stream()
.map(name -> name.toUpperCase())
.collect(Collectors.toList());
List<String> upperNames = names.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
5. 新的日期时间API
5.1 基本日期操作
LocalDate today = LocalDate.now();
LocalDate independenceDay = LocalDate.of(2023, Month.JULY, 4);
LocalDate nextWeek = today.plusWeeks(1);
LocalDate previousMonthSameDay = today.minusMonths(1);
boolean isAfter = today.isAfter(independenceDay);
5.2 日期格式化和解析
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
String formatted = now.format(formatter);
String dateStr = "2023-07-04 12:30:00";
LocalDateTime parsed = LocalDateTime.parse(dateStr, formatter);
5.3 日期时间段计算
LocalDate startDate = LocalDate.of(2023, 1, 1);
LocalDate endDate = LocalDate.of(2023, 12, 31);
long daysBetween = ChronoUnit.DAYS.between(startDate, endDate);
LocalDate birthDate = LocalDate.of(1990, 5, 15);
long age = ChronoUnit.YEARS.between(birthDate, LocalDate.now());
6. 默认方法和静态方法
6.1 接口默认方法
interface Vehicle {
String getBrand();
default String turnAlarmOn() {
return "Turning the vehicle alarm on.";
}
default String turnAlarmOff() {
return "Turning the vehicle alarm off.";
}
}
class Car implements Vehicle {
@Override
public String getBrand() {
return "BMW";
}
}
Vehicle car = new Car();
System.out.println(car.getBrand());
System.out.println(car.turnAlarmOn());
6.2 接口静态方法
interface MathOperations {
static int add(int a, int b) {
return a + b;
}
static int subtract(int a, int b) {
return a - b;
}
}
int sum = MathOperations.add(5, 3);
int difference = MathOperations.subtract(5, 3);
7. 并行流提高性能
7.1 顺序流 vs 并行流
List<Integer> numbers = IntStream.rangeClosed(1, 1_000_000)
.boxed()
.collect(Collectors.toList());
long start = System.currentTimeMillis();
long count = numbers.stream()
.filter(n -> n % 2 == 0)
.count();
long sequentialTime = System.currentTimeMillis() - start;
start = System.currentTimeMillis();
count = numbers.parallelStream()
.filter(n -> n % 2 == 0)
.count();
long parallelTime = System.currentTimeMillis() - start;
System.out.println("Sequential time: " + sequentialTime + "ms");
System.out.println("Parallel time: " + parallelTime + "ms");
7.2 并行流注意事项
int[] sharedCounter = new int[1];
IntStream.range(0, 10_000).parallel()
.forEach(i -> sharedCounter[0]++);
AtomicInteger safeCounter = new AtomicInteger(0);
IntStream.range(0, 10_000).parallel()
.forEach(i -> safeCounter.incrementAndGet());
8. CompletableFuture:异步编程
8.1 基本异步操作
CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IllegalStateException(e);
}
return "Result of the asynchronous computation";
});
String result = future.get();
System.out.println(result);
8.2 组合多个Future
CompletableFuture<String> getUser = CompletableFuture.supplyAsync(() -> {
try { Thread.sleep(500); } catch (InterruptedException e) {}
return "User123";
});
CompletableFuture<String> getOrder = CompletableFuture.supplyAsync(() -> {
try { Thread.sleep(800); } catch (InterruptedException e) {}
return "Order456";
});
CompletableFuture<String> combined = getUser.thenCombine(getOrder,
(user, order) -> "User " + user + " has order " + order);
System.out.println(combined.get());
9. 综合重构示例
9.1 传统代码示例
public class OrderProcessor {
public List<Order> processOrders(List<Order> orders) {
List<Order> validOrders = new ArrayList<>();
for (Order order : orders) {
if (order.isValid() && order.getAmount() > 0) {
validOrders.add(order);
}
}
Collections.sort(validOrders, new Comparator<Order>() {
@Override
public int compare(Order o1, Order o2) {
return Double.compare(o1.getAmount(), o2.getAmount());
}
});
double total = 0;
for (Order order : validOrders) {
total += order.getAmount();
}
System.out.println("Total amount: " + total);
return validOrders;
}
}
9.2 Java 8重构版本
public class OrderProcessor {
public List<Order> processOrders(List<Order> orders) {
return orders.stream()
.filter(Order::isValid)
.filter(order -> order.getAmount() > 0)
.sorted(Comparator.comparingDouble(Order::getAmount))
.peek(order -> System.out.println("Processing order: " + order.getId()))
.collect(Collectors.toList());
}
public double calculateTotal(List<Order> orders) {
return orders.stream()
.mapToDouble(Order::getAmount)
.sum();
}
}
10. 性能考虑与最佳实践
10.1 Stream性能测试
public class PerformanceTest {
private static final int SIZE = 10_000_000;
private static List<Integer> numbers = IntStream.range(0, SIZE)
.boxed()
.collect(Collectors.toList());
public static void main(String[] args) {
long start = System.currentTimeMillis();
int sum = 0;
for (int n : numbers) {
sum += n;
}
long time = System.currentTimeMillis() - start;
System.out.println("For loop sum: " + sum + ", time: " + time + "ms");
start = System.currentTimeMillis();
sum = numbers.stream().mapToInt(Integer::intValue).sum();
time = System.currentTimeMillis() - start;
System.out.println("Sequential stream sum: " + sum + ", time: " + time + "ms");
start = System.currentTimeMillis();
sum = numbers.parallelStream().mapToInt(Integer::intValue).sum();
time = System.currentTimeMillis() - start;
System.out.println("Parallel stream sum: " + sum + ", time: " + time + "ms");
}
}
10.2 Java 8最佳实践
list.forEach(x -> System.out.println(x));
list.forEach(System.out::println);
List<String> result = new ArrayList<>();
list.forEach(s -> result.add(s.toUpperCase()));
List<String> result = list.stream()
.map(String::toUpperCase)
.collect(Collectors.toList());
list.parallelStream()...
Date oldDate = new Date();
LocalDate newDate = LocalDate.now();
Optional.ofNullable(getPossibleNullValue())
.ifPresentOrElse(
value -> process(value),
() -> handleNull()
);
11. 常见问题与解决方案
11.1 调试Lambda表达式
list.stream()
.filter(s -> {
System.out.println("Filtering: " + s);
return s.length() > 3;
})
.map(s -> {
System.out.println("Mapping: " + s);
return s.toUpperCase();
})
.forEach(System.out::println);
list.stream()
.peek(s -> System.out.println("Before filter: " + s))
.filter(s -> s.length() > 3)
.peek(s -> System.out.println("After filter: " + s))
.map(String::toUpperCase)
.forEach(System.out::println);
11.2 异常处理
List<String> urls = Arrays.asList("http://example.com", "http://invalid.url");
urls.stream()
.map(url -> {
try {
return new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
}
})
.filter(Objects::nonNull)
.forEach(System.out::println);
urls.stream()
.map(ThrowingFunction.unchecked(URL::new))
.forEach(System.out::println);
@FunctionalInterface
public interface ThrowingFunction<T, R, E extends Exception> {
R apply(T t) throws E;
static <T, R> Function<T, R> unchecked(ThrowingFunction<T, R, Exception> f) {
return t -> {
try {
return f.apply(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
}
12. 总结与进一步学习
12.1 Java 8特性总结表
特性 |
主要用途 |
示例 |
Lambda表达式 |
简化匿名内部类 |
() -> System.out.println("Hi") |
Stream API |
集合处理流水线 |
list.stream().filter().map().collect() |
Optional |
避免NullPointerException |
Optional.ofNullable(value).orElse(default) |
方法引用 |
简化Lambda |
String::toUpperCase |
新日期API |
更好的日期处理 |
LocalDate.now().plusDays(1) |
默认方法 |
接口演化 |
interface A { default void foo() {} } |
并行流 |
并行处理集合 |
list.parallelStream().forEach() |
CompletableFuture |
异步编程 |
CompletableFuture.supplyAsync(() -> "result") |