Java 常用类库
核心类库、常用第三方库与选型建议。
核心 Java 类库
The JDK covers most basics, so do not reach for third-party libraries too early. Knowing these packages removes many dependencies: java.util, java.time, java.nio.file, java.util.concurrent, java.util.stream.
| Package | Provides |
|---|---|
| java.util | Collections, Optional, Random |
| java.time | Date, time, duration, formatting |
| java.nio.file | Path, Files, directory walking |
| java.util.concurrent | Threads, pools, atomics, locks |
| java.util.stream | Declarative collection processing |
常用第三方库
Judge a library by three things: does it solve a real pain, is it actively maintained, does it have a clean security record. These are the de facto standards in the Java ecosystem.
| Library | Use case |
|---|---|
| Jackson | JSON serialisation |
| SLF4J + Logback | Logging facade and backend |
| Guava | Collections, caching, utilities |
| Hutool | All-in-one Chinese utility kit |
| JUnit 5 | Unit testing |
| HikariCP | High-performance JDBC pool |
如何选择类库
First ask whether the JDK already does it. Then check activity, last release and issue response time. Finally weigh size and transitive dependencies — do not pull a dozen jars for one small helper.
- Check the JDK first — many utilities already exist
- Prefer one library per concern, not three overlapping ones
- Watch transitive dependencies and CVEs
- Prefer a facade (SLF4J) so the backend can be swapped
使用示例
With Jackson, ObjectMapper is thread-safe and should be a reused singleton, not created per call. Serialise with writeValueAsString, deserialise with readValue.
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class JacksonDemo {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(Map.of("a", 1));
System.out.println(json);
Map<?, ?> back = mapper.readValue("{\"b\":2}", Map.class);
System.out.println(back.get("b"));
}
}{"a":1}
2ObjectMapper is expensive to build but thread-safe — reuse one instance. Creating it per call is a common performance problem.核心 Java 类库
JDK 自带的类库覆盖了绝大多数基础需求,不用急着引第三方。熟悉这几个包能省掉很多依赖:java.util、java.time、java.nio.file、java.util.concurrent、java.util.stream。
| 包 | 提供 |
|---|---|
| java.util | 集合、Optional、随机数 |
| java.time | 日期、时间、时长、格式化 |
| java.nio.file | Path、Files、目录遍历 |
| java.util.concurrent | 线程、线程池、原子类、锁 |
| java.util.stream | 声明式集合处理 |
常用第三方库
选库的三个标准:是否解决真实痛点、是否维护活跃、是否有安全漏洞记录。下面这些是 Java 生态里事实上的标准选择。
| 类库 | 用途 |
|---|---|
| Jackson | JSON 序列化 |
| SLF4J + Logback | 日志门面与实现 |
| Guava | 集合、缓存、工具方法 |
| Hutool | 国产全能工具集 |
| JUnit 5 | 单元测试 |
| HikariCP | 高性能 JDBC 连接池 |
如何选择类库
先问「JDK 有没有」——有就别引。再看 GitHub 活跃度、最近发布时间、issue 响应速度。最后评估包体积与传递依赖,避免为了一个小功能拖进一堆 jar。
- 先看 JDK —— 很多工具已有内置实现
- 一个问题只选一个库,避免功能重叠
- 关注传递依赖与安全漏洞
- 优先用门面(SLF4J),便于替换实现
使用示例
以 Jackson 为例:ObjectMapper 是线程安全的,应作为单例复用而不是每次 new。序列化用 writeValueAsString,反序列化用 readValue。
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
public class JacksonDemo {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(Map.of("a", 1));
System.out.println(json);
Map<?, ?> back = mapper.readValue("{\"b\":2}", Map.class);
System.out.println(back.get("b"));
}
}{"a":1}
2ObjectMapper 创建成本高但线程安全,全局复用一个实例即可。频繁 new 会明显拖慢序列化,这是很常见的性能问题。