Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Common Java Libraries

Common Java Libraries

Java provides a rich set of class libraries; here are some of the most commonly used classes.

1. Core Java Class Library

The Java standard library provides a rich set of built-in class libraries; here are some commonly used core libraries:

Category Library Name Main Function
Collections Framework java.util.ArrayList Dynamic array implementation
java.util.LinkedList Doubly linked list implementation
java.util.HashMap Map implemented with a hash table
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 time zone
**** java.util.Date Current date
**** java.text.SimpleDateFormat Date and time formatting
**** java.util.Calendar Handle date and time
**** java.util.GregorianCalendar Implements the Gregorian calendar
**** java.time.Instant Represents a 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 amount (hours, minutes, seconds, nanoseconds)
Network Programming java.net.URL URL handling
java.net.Socket Socket programming

2. Common Third-Party Libraries

The following are widely used third-party libraries in the Java ecosystem:

Category Library Name Main Function 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 building and dependency management https://maven.apache.org/
Gradle Flexible build tool https://gradle.org/

3. How to Choose a Suitable Library

3.1 Evaluation Criteria

  1. Functional Requirements: First clarify what functionality the project needs
  2. Community Support: An active community means better support and documentation
  3. Performance: Requires special consideration for performance-sensitive applications
  4. Learning curve: Consider the team's learning cost
  5. Maintenance Status: Check whether the project is still actively maintained

3.2 Version Selection Suggestions


4. Usage Examples

4.1 Using Jackson to Process 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

(

)

;

       

}

   

}

}

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

)

)

;

   

}

}