Java常用类库
-
-
- [1. **Java基础类库**](#1. Java基础类库)
-
- [1.1 **`java.lang`**](#1.1
java.lang
) - [1.2 **`java.util`**](#1.2
java.util
) - [1.3 **`java.io`**](#1.3
java.io
) - [1.4 **`java.nio`**](#1.4
java.nio
) - [1.5 **`java.time`**](#1.5
java.time
)
- [1.1 **`java.lang`**](#1.1
- [2. **第三方类库**](#2. 第三方类库)
-
- [2.1 **Apache Commons**](#2.1 Apache Commons)
- [2.2 **Google Guava**](#2.2 Google Guava)
- [2.3 **Jackson**](#2.3 Jackson)
- [2.4 **Lombok**](#2.4 Lombok)
- [3. **Spring相关类库**](#3. Spring相关类库)
- [4. **并发类库**](#4. 并发类库)
-
- [4.1 **`java.util.concurrent`**](#4.1
java.util.concurrent
)
- [4.1 **`java.util.concurrent`**](#4.1
- [5. **网络类库**](#5. 网络类库)
-
- [5.1 **`java.net`**](#5.1
java.net
) - [5.2 **OkHttp**](#5.2 OkHttp)
- [5.1 **`java.net`**](#5.1
-
Java中有许多常用的类库,它们大大提高了开发效率。以下是一些常见的Java类库及其典型用法:
1. Java基础类库
这些类库是JDK自带的,广泛应用于各种Java项目中。
1.1 java.lang
-
Object
:所有类的父类,提供基础的方法如equals()
、hashCode()
、toString()
等。 -
String
:用于字符串操作。典型用法:javaString str = "Hello, World!"; String upperStr = str.toUpperCase(); // HELLO, WORLD! String subStr = str.substring(0, 5); // Hello
-
Math
:提供基本数学运算,如取整、三角函数等。javaint max = Math.max(10, 20); // 20 double sqrt = Math.sqrt(16); // 4.0
-
Thread
:用于多线程编程。javaThread thread = new Thread(() -> { System.out.println("Thread running"); }); thread.start();
1.2 java.util
-
ArrayList
:动态数组。javaList<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana");
-
HashMap
:键值对的集合。javaMap<String, Integer> map = new HashMap<>(); map.put("John", 25); int age = map.get("John"); // 25
-
Collections
:集合工具类,提供排序、查找等功能。javaList<Integer> numbers = Arrays.asList(3, 1, 2); Collections.sort(numbers); // [1, 2, 3]
1.3 java.io
-
File
:用于文件操作。javaFile file = new File("test.txt"); boolean exists = file.exists();
-
BufferedReader
:用于读取文本文件。javaBufferedReader reader = new BufferedReader(new FileReader("test.txt")); String line = reader.readLine(); reader.close();
1.4 java.nio
-
Files
:用于高效的文件操作。javaPath path = Paths.get("test.txt"); List<String> lines = Files.readAllLines(path);
1.5 java.time
-
LocalDate
、LocalTime
、LocalDateTime
:用于处理日期和时间。javaLocalDate today = LocalDate.now(); LocalDate birthDate = LocalDate.of(1990, Month.JANUARY, 1);
2. 第三方类库
以下是常见的第三方库,这些库不包含在JDK中,但可以通过Maven或Gradle引入。
2.1 Apache Commons
-
Commons Lang
:提供各种实用工具类。javaString reversed = StringUtils.reverse("abc"); // "cba"
-
Commons IO
:简化I/O操作。javaString content = FileUtils.readFileToString(new File("test.txt"), "UTF-8");
2.2 Google Guava
-
Lists
:快速创建集合。javaList<String> list = Lists.newArrayList("a", "b", "c");
-
Joiner
:将集合转换为字符串。javaString result = Joiner.on(", ").join(list); // "a, b, c"
2.3 Jackson
用于JSON的序列化和反序列化。
java
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(object); // 序列化
MyClass obj = mapper.readValue(jsonString, MyClass.class); // 反序列化
2.4 Lombok
简化Java代码,自动生成getter、setter、构造函数等。
java
@Data
public class User {
private String name;
private int age;
}
3. Spring相关类库
-
Spring Context
:管理Bean的生命周期和依赖注入。javaApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyService myService = context.getBean(MyService.class);
-
Spring Boot
:用于快速创建Spring应用。java@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
4. 并发类库
4.1 java.util.concurrent
-
ExecutorService
:用于管理线程池。javaExecutorService executor = Executors.newFixedThreadPool(10); executor.submit(() -> System.out.println("Task executed")); executor.shutdown();
-
CountDownLatch
:同步工具类。javaCountDownLatch latch = new CountDownLatch(3); latch.await(); // 等待计数为0
5. 网络类库
5.1 java.net
-
HttpURLConnection
:用于HTTP请求。javaURL url = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); int responseCode = conn.getResponseCode();
5.2 OkHttp
流行的HTTP客户端库。
java
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().url("http://example.com").build();
Response response = client.newCall(request).execute();
这些类库涵盖了Java开发中的各个方面,熟练掌握它们能够大大提高开发效率。