Boost Java Development with Hutool: Essential Modules and Practical Examples

This article introduces the Hutool Java utility library, outlines its core modules, shows how to integrate it via Maven, and provides practical code examples for HTTP requests, random code generation, pinyin conversion, timing, number handling, data masking, email sending, and Bloom filter usage.

Su San Talks Tech
Su San Talks Tech
Su San Talks Tech
Boost Java Development with Hutool: Essential Modules and Practical Examples

Hutool is a lightweight yet comprehensive Java utility library that simplifies API usage through static methods, improving development efficiency and giving Java a more functional, "sweet" feel.

Hutool reduces the learning cost of related APIs, helps avoid bugs from incomplete wrappers, and serves both large projects for small problems and small projects for overall efficiency.

Modules Overview

hutool-aop : JDK dynamic proxy wrapper providing AOP support without IoC.

hutool-bloomFilter : Bloom filter utilities with various hash algorithms.

hutool-cache : Simple cache implementation.

hutool-core : Core utilities such as Bean operations, date handling, and common helpers.

hutool-cron : Cron expression based scheduling.

hutool-crypto : Encryption and decryption (symmetric, asymmetric, digest).

hutool-db : JDBC wrapper based on ActiveRecord.

hutool-dfa : Multi‑keyword search using DFA model.

hutool-extra : Third‑party integrations (template engines, mail, servlet, QR code, Emoji, FTP, segmentation, etc.).

hutool-http : HttpUrlConnection based HTTP client simplifying HTTPS, file upload, cookies, etc.

hutool-log : Automatic log facade detection.

hutool-script : Script execution wrapper (e.g., JavaScript).

hutool-setting : Enhanced configuration file and Properties handling.

hutool-system : System parameter access (JVM info, etc.).

hutool-json : JSON utilities.

hutool-captcha : Image captcha generation.

hutool-poi : POI‑based Excel and Word utilities.

hutool-socket : NIO/AIO socket wrapper.

hutool-jwt : JSON Web Token implementation.

How to Add Hutool

<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.16</version>
</dependency>

Common Usage Scenarios

HTTP Requests

Hutool‑http wraps HttpUrlConnection to make GET, POST, file upload, and cookie handling straightforward.

// GET request
String content = HttpUtil.get(url);
// POST request
HashMap<String, Object> param = new HashMap<>();
param.put("city", "西安");
String result = HttpUtil.post("www.javacn.site", param);

Random Verification Code

// Generate a 4‑digit random verification code
String verificationCode = RandomUtil.randomStringUpper(4);

Pinyin Utilities

Hutool provides a facade for pinyin conversion, automatically detecting one of TinyPinyin, JPinyin, or Pinyin4j via SPI.

Get full pinyin

// Output: ni hao
String pinyin = PinyinUtil.getPinyin("你好", " ");

Get first letters

// Output: h, s, d, y, g
String result = PinyinUtil.getFirstLetter("H是第一个", ", ");

Custom pinyin engine

Pinyin4jEngine engine = new Pinyin4jEngine();
String pinyin = engine.getPinyin("你好h", " ");

Timer

TimeInterval offers simple timing for method execution.

TimeInterval timer = DateUtil.timer();
// ... business logic
long ms = timer.interval(); // elapsed milliseconds
timer.intervalRestart(); // get elapsed and reset
long minutes = timer.intervalMinute();

Number Utilities

NumberUtil provides rounding, formatting, validation, and random number generation.

Rounding

double te1 = 123456.123456;
double te2 = 123456.128456;
Console.log(round(te1, 4)); // 123456.1235
Console.log(round(te2, 4)); // 123456.1285

Formatting

long c = 299792458; // speed of light
String formatted = NumberUtil.decimalFormat(",###", c); // 299,792,458

Validation

NumberUtil.isNumber

NumberUtil.isInteger

NumberUtil.isDouble

NumberUtil.isPrimes

Random Numbers

NumberUtil.generateRandomNumber

NumberUtil.generateBySet

Data Masking

DesensitizedUtil offers ready‑made masking for IDs, phone numbers, emails, addresses, etc.

// Mask ID card number
DesensitizedUtil.idCardNum("51343620000320711X", 1, 2); // 5***************1X
// Mask mobile phone
DesensitizedUtil.mobilePhone("18049531999"); // 180****1999
// Mask password
DesensitizedUtil.password("1234567890"); // **********

Email Sending

MailUtil simplifies JavaMail usage.

// Simple text email
MailUtil.send("[email protected]", "Test", "Email from Hutool test", false);

// HTML email with attachment
MailUtil.send("[email protected]", "Test", "<h1>Email from Hutool test</h1>", true, FileUtil.file("d:/aaa.xml"));

// Bulk email
ArrayList<String> tos = CollUtil.newArrayList(
    "[email protected]",
    "[email protected]",
    "[email protected]",
    "[email protected]"
);
MailUtil.send(tos, "Test", "Bulk email from Hutool", false);

Bloom Filter

The Bloom filter provides space‑efficient set membership testing.

// Initialize
BitMapBloomFilter filter = new BitMapBloomFilter(10);
filter.add("123");
filter.add("abc");
filter.add("ddd");
// Query
filter.contains("abc");

Conclusion

Hutool delivers a cost‑effective, elegant way to accelerate Java development, making code cleaner and more maintainable.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentHTTPhutoolCode Examplesbloom-filterUtility Library
Su San Talks Tech
Written by

Su San Talks Tech

Su San, former staff at several leading tech companies, is a top creator on Juejin and a premium creator on CSDN, and runs the free coding practice site www.susan.net.cn.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.