Java 常用类库

核心类库、常用第三方库与选型建议。

Beginner
Key Points优先用 JDK 自带类库;第三方首选成熟度高、维护活跃的(Jackson、SLF4J、Guava/Hutool、JUnit 5)。

核心 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.

PackageProvides
java.utilCollections, Optional, Random
java.timeDate, time, duration, formatting
java.nio.filePath, Files, directory walking
java.util.concurrentThreads, pools, atomics, locks
java.util.streamDeclarative 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.

LibraryUse case
JacksonJSON serialisation
SLF4J + LogbackLogging facade and backend
GuavaCollections, caching, utilities
HutoolAll-in-one Chinese utility kit
JUnit 5Unit testing
HikariCPHigh-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.

Java
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"));
    }
}
Output
{"a":1}
2
ObjectMapper is expensive to build but thread-safe — reuse one instance. Creating it per call is a common performance problem.

Friends & Partners