Java Common Class Libraries
Java provides a rich set of class libraries. The following are some of the most commonly used classes.
1. Core Java Class Libraries
The Java standard library provides a rich set of built-in libraries. The following are some commonly used core libraries:
| Category | Library Name | Main Functionality |
|---|---|---|
| Collection Framework | java.util.ArrayList |
Dynamic array implementation |
java.util.LinkedList |
Doubly linked list implementation | |
java.util.HashMap |
Hash table-based Map | |
java.util.Vector |
Implements a dynamic array | |
java.util.HashSet |
Hash table-based Set | |
java.util.Scanner |
Get user input | |
java.util.regex.Pattern |
Regular expressions | |
java.util.regex.Macher |
Regular expressions | |
| IO/NIO | java.io.File |
File and directory operations |
java.nio.file.Files |
File operation utility class | |
java.io.InputStream |
Byte stream base class | |
java.io.OutputStream |
Byte stream base class | |
| Multithreading | java.lang.Thread |
Thread operation class |
java.util.concurrent.ExecutorService |
Thread pool management | |
| Date and Time | java.time.LocalDate |
Date handling |
java.time.LocalDateTime |
Date and time handling | |
java.time.ZonedDateTime |
Date and time with timezone | |
java.util.Date |
Current date | |
java.text.SimpleDateFormat |
Date-time formatting | |
java.util.Calendar |
Handle date and time | |
java.util.GregorianCalendar |
Implements the Gregorian calendar | |
java.time.Instant |
Represents an instantaneous point on the timeline | |
java.time.ChronoUnit |
Standard unit for measuring time | |
java.time.Period |
Represents the time interval between two dates | |
java.time.Duration |
Represents a time-based period (hours, minutes, seconds, nanoseconds) | |
| Network Programming | java.net.URL |
URL handling |
java.net.Socket |
Socket programming |
2. Common Third-Party Libraries
The following are third-party libraries widely used in the Java ecosystem:
| Category | Library Name | Main Functionality | Official Website |
|---|---|---|---|
| JSON Processing | Jackson |
JSON serialization/deserialization | https://github.com/FasterXML/jackson |
Gson |
Google's JSON library | https://github.com/google/gson | |
| Unit Testing | JUnit |
Java unit testing framework | https://junit.org/junit5/ |
Mockito |
Mock testing framework | https://site.mockito.org/ | |
| Logging | Log4j |
Logging framework | https://logging.apache.org/log4j/ |
SLF4J |
Logging facade framework | https://www.slf4j.org/ | |
| Web Development | Spring Framework |
Enterprise application framework | https://spring.io/projects/spring-framework |
Spring Boot |
Rapid application development framework | https://spring.io/projects/spring-boot | |
| Database | Hibernate |
ORM framework | https://hibernate.org/ |
MyBatis |
SQL mapping framework | https://mybatis.org/mybatis-3/ | |
| Build Tools | Maven |
Project build and dependency management | https://maven.apache.org/ |
Gradle |
Flexible build tool | https://gradle.org/ |
3. How to Choose the Right Library
3.1 Evaluation Criteria
- Functional requirements: First, clarify what functionality the project needs
- Community support: An active community means better support and documentation
- Performance: Special consideration is needed for performance-sensitive applications
- Learning curve: Consider the team's learning cost
- Maintenance status: Check whether the project is still actively maintained
3.2 Version Selection Recommendations
- Prioritize LTS (Long-Term Support) versions
- Avoid using versions that are no longer maintained
- New projects can consider newer stable versions
4. Usage Examples
4.1 Using Jackson to Handle JSON
Example
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// Object to JSON
String json = mapper.writeValueAsString(new User("Zhang San", 25));
System.out.println(json);
// JSON to Object
User user = mapper.readValue(json, User.class);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class JsonExample {
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
try {
// Object to JSON
String json = mapper.writeValueAsString(new User("Zhang San", 25));
System.out.println(json);
// JSON to Object
User user = mapper.readValue(json, User.class);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
}
}
}
4.2 Using JUnit for Unit Testing
Example
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAdd() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}
import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest {
@Test
void testAdd() {
Calculator calc = new Calculator();
assertEquals(5, calc.add(2, 3));
}
}