Java 8 常用工具类
Java 8 引入了许多实用的工具类和方法,尤其在函数式编程和集合操作方面提供了更简洁的解决方案。以下是常用的工具类及其典型用法:
java.util.Optional
Optional 用于避免空指针异常,提供更优雅的 null 处理方式。
Optional<String> optional = Optional.ofNullable(null);
String result = optional.orElse("default"); // 如果为null,返回"default"
optional.ifPresent(System.out::println); // 非空时执行操作
java.util.stream.Stream
Stream API 支持函数式风格操作集合,如过滤、映射、归约等。
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
.filter(s -> s.startsWith("a"))
.map(String::toUpperCase)
.forEach(System.out::println); // 输出"A"
java.time 包
Java 8 新增的日期时间 API(如 LocalDate、LocalTime、LocalDateTime)替代了旧的 Date 和 Calendar。
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = tomorrow.format(formatter);
java.util.function 包
常用函数式接口(如 Predicate、Function、Supplier、Consumer)支持 Lambda 表达式。
Predicate<String> isEmpty = String::isEmpty;
Function<String, Integer> lengthMapper = String::length;
Consumer<String> printer = System.out::println;
java.util.concurrent.ConcurrentHashMap
增强的并发集合类,支持原子操作和并行流处理。
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.computeIfAbsent("key", k -> 1); // 原子操作
map.forEach(1, (k, v) -> System.out.println(k + "=" + v)); // 并行遍历
java.util.Objects
工具类提供静态方法简化对象操作(如判空、比较等)。
Objects.equals(null, "test"); // 安全比较
Objects.requireNonNull(obj, "Object cannot be null"); // 非空校验
Collectors
Stream 的终端操作工具,用于集合的汇总、分组等。
List<String> names = Arrays.asList("John", "Jane", "Tom");
Map<Integer, List<String>> groupByLength = names.stream()
.collect(Collectors.groupingBy(String::length)); // 按长度分组
Comparator
支持链式调用和 Lambda 表达式简化排序逻辑。
List<String> words = Arrays.asList("banana", "apple", "cherry");
words.sort(Comparator.comparing(String::length).thenComparing(String::toString));
使用场景建议
- Optional:适用于方法返回值可能为 null 的场景。
- Stream:处理集合数据时优先选择,代码更简洁。
- java.time:所有日期时间操作均推荐使用新 API。
- 函数式接口:与 Lambda 结合实现行为参数化。
通过合理使用这些工具类,可以显著提升代码的可读性和健壮性。
Java 8 常用工具类
Java 8 引入了许多实用的工具类和方法,尤其在函数式编程和集合操作方面提供了更简洁的解决方案。以下是常用的工具类及其典型用法:
java.util.Optional
Optional 用于避免空指针异常,提供更优雅的 null 处理方式。
Optional<String> optional = Optional.ofNullable(null);
String result = optional.orElse("default"); // 如果为null,返回"default"
optional.ifPresent(System.out::println); // 非空时执行操作
java.util.stream.Stream
Stream API 支持函数式风格操作集合,如过滤、映射、归约等。
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
.filter(s -> s.startsWith("a"))
.map(String::toUpperCase)
.forEach(System.out::println); // 输出"A"
java.time 包
Java 8 新增的日期时间 API(如 LocalDate、LocalTime、LocalDateTime)替代了旧的 Date 和 Calendar。
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
String formattedDate = tomorrow.format(formatter);
java.util.function 包
常用函数式接口(如 Predicate、Function、Supplier、Consumer)支持 Lambda 表达式。
Predicate<String> isEmpty = String::isEmpty;
Function<String, Integer> lengthMapper = String::length;
Consumer<String> printer = System.out::println;
java.util.concurrent.ConcurrentHashMap
增强的并发集合类,支持原子操作和并行流处理。
ConcurrentHashMap<String, Integer> map = new ConcurrentHashMap<>();
map.computeIfAbsent("key", k -> 1); // 原子操作
map.forEach(1, (k, v) -> System.out.println(k + "=" + v)); // 并行遍历
java.util.Objects
工具类提供静态方法简化对象操作(如判空、比较等)。
Objects.equals(null, "test"); // 安全比较
Objects.requireNonNull(obj, "Object cannot be null"); // 非空校验
Collectors
Stream 的终端操作工具,用于集合的汇总、分组等。
List<String> names = Arrays.asList("John", "Jane", "Tom");
Map<Integer, List<String>> groupByLength = names.stream()
.collect(Collectors.groupingBy(String::length)); // 按长度分组
Comparator
支持链式调用和 Lambda 表达式简化排序逻辑。
List<String> words = Arrays.asList("banana", "apple", "cherry");
words.sort(Comparator.comparing(String::length).thenComparing(String::toString));
使用场景建议
- Optional:适用于方法返回值可能为 null 的场景。
- Stream:处理集合数据时优先选择,代码更简洁。
- java.time:所有日期时间操作均推荐使用新 API。
- 函数式接口:与 Lambda 结合实现行为参数化。
通过合理使用这些工具类,可以显著提升代码的可读性和健壮性。
2294

被折叠的 条评论
为什么被折叠?



