一、Apache StringUtils:專為Java字符串而生的工具類
首先引入依賴:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
1.字符串判空
isEmpty: 判斷null和""
isNotEmpty:判斷null和""
isBlank:判斷null和""和" "
isNotBlank:判斷null和""和" "
示例代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-479990.html
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: StringUtils測試
*/
@SpringBootTest
public class StringTest {
@Test
void test1() {
String str1 = null;
String str2 = "";
String str3 = " ";
String str4 = "abc";
String str5 = "null";
System.out.println(StringUtils.isEmpty(str1));
System.out.println(StringUtils.isEmpty(str2));
System.out.println(StringUtils.isEmpty(str3));
System.out.println(StringUtils.isEmpty(str4));
System.out.println(StringUtils.isEmpty(str5));
System.out.println("=====");
System.out.println(StringUtils.isNotEmpty(str1));
System.out.println(StringUtils.isNotEmpty(str2));
System.out.println(StringUtils.isNotEmpty(str3));
System.out.println(StringUtils.isNotEmpty(str4));
System.out.println(StringUtils.isNotEmpty(str5));
System.out.println("=====");
System.out.println(StringUtils.isBlank(str1));
System.out.println(StringUtils.isBlank(str2));
System.out.println(StringUtils.isBlank(str3));
System.out.println(StringUtils.isBlank(str4));
System.out.println(StringUtils.isBlank(str5));
System.out.println("=====");
System.out.println(StringUtils.isNotBlank(str1));
System.out.println(StringUtils.isNotBlank(str2));
System.out.println(StringUtils.isNotBlank(str3));
System.out.println(StringUtils.isNotBlank(str4));
System.out.println(StringUtils.isNotBlank(str5));
}
}
執(zhí)行結(jié)果:
true
true
false
false
false
=====
false
false
true
true
true
=====
true
true
true
false
false
=====
false
false
false
true
true
2.分割字符串
使用StringUtils的split()方法分割字符串成數(shù)組。
示例代碼如下:
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Arrays;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: StringUtils測試
*/
@SpringBootTest
public class StringTest {
@Test
void test1() {
String result = "a,b,c";
String[] arr = StringUtils.split(result, ",");
// 輸出[a, b, c]
System.out.println(Arrays.toString(arr));
}
}
3.判斷是否純數(shù)字
使用StringUtils的isNumeric()方法判斷字符串是否是純數(shù)字形式。
示例代碼如下:
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: StringUtils測試
*/
@SpringBootTest
public class StringTest {
@Test
void test1() {
String data1 = "2";
// 輸出true
System.out.println(StringUtils.isNumeric(data1));
String data2 = "hello";
// 輸出false
System.out.println(StringUtils.isNumeric(data2));
}
}
4.將集合拼接成字符串
使用StringUtils的join(list,"拼接的字符")方法將集合的數(shù)據(jù)拼接成字符串。
示例代碼如下:
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.List;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: StringUtils測試
*/
@SpringBootTest
public class StringTest {
@Test
void test1() {
List<String> list = new ArrayList<>();
list.add("a");
list.add("b");
list.add("c");
// 輸出a,b,c
System.out.println(StringUtils.join(list, ","));
}
}
5.其他方法
-
trim(String str)
:去除字符串首尾的空白字符。 -
trimToEmpty(String str)
:去除字符串首尾的空白字符,如果字符串為 null,則返回空字符串。 -
trimToNull(String str)
:去除字符串首尾的空白字符,如果結(jié)果為空字符串,則返回 null。 -
equals(String str1, String str2)
:比較兩個字符串是否相等。 -
equalsIgnoreCase(String str1, String str2)
:比較兩個字符串是否相等,忽略大小寫。 -
startsWith(String str, String prefix)
:檢查字符串是否以指定的前綴開頭。 -
endsWith(String str, String suffix)
:檢查字符串是否以指定的后綴結(jié)尾。 -
contains(String str, CharSequence seq)
:檢查字符串是否包含指定的字符序列。 -
indexOf(String str, CharSequence seq)
:返回指定字符序列在字符串中首次出現(xiàn)的索引,如果沒有找到,則返回 -1。 -
lastIndexOf(String str, CharSequence seq)
:返回指定字符序列在字符串中最后一次出現(xiàn)的索引,如果沒有找到,則返回 -1。 -
substring(String str, int start, int end)
:截取字符串中指定范圍的子串。 -
replace(String str, String searchString, String replacement)
:替換字符串中所有出現(xiàn)的搜索字符串為指定的替換字符串。 -
replaceAll(String str, String regex, String replacement)
:使用正則表達式替換字符串中所有匹配的部分。 -
join(Iterable<?> iterable, String separator)
:使用指定的分隔符將可迭代對象中的元素連接為一個字符串。 -
split(String str, String separator)
:使用指定的分隔符將字符串分割為一個字符串?dāng)?shù)組。 -
capitalize(String str)
:將字符串的第一個字符轉(zhuǎn)換為大寫。 -
uncapitalize(String str)
:將字符串的第一個字符轉(zhuǎn)換為小寫。
二、Hutool工具包
引入依賴:
<!--hutool-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.12</version>
</dependency>
1.類型轉(zhuǎn)換
我們接收客戶端傳過來的數(shù)據(jù)的時候,通常我們需要把這些數(shù)據(jù)轉(zhuǎn)換成我們需要的數(shù)據(jù)類型。
示例代碼如下:
import cn.hutool.core.convert.Convert;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: hutool測試
*/
@SpringBootTest
public class HutoolTest {
@Test
void test1() {
String param = null;
// 輸出null
System.out.println(Convert.toInt(param));
// 設(shè)置了默認值 輸出0
System.out.println(Convert.toInt(param, 0));
// 輸出Fri Jun 09 00:00:00 CST 2023
System.out.println(Convert.toDate("2023年06月09日"));
}
}
2.日期時間轉(zhuǎn)換
示例代碼如下:
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: hutool測試
*/
@SpringBootTest
public class HutoolTest {
@Test
void test1() {
// 輸出2023-06-09 09:55:24
System.out.println(DateUtil.date());
// 輸出2023-06-09 00:00:00
DateTime dateTime = DateUtil.parse("2023-06-09");
System.out.println(dateTime);
// 輸出2023-06-09
Date date = new Date();
System.out.println(DateUtil.formatDate(date));
}
}
3.反射工具
Hutool 封裝的反射工具 ReflectUtil 包括:
- 獲取構(gòu)造方法
- 獲取字段
- 獲取字段值
- 獲取方法
- 執(zhí)行方法(對象方法和靜態(tài)方法)
示例代碼如下:
import cn.hutool.core.util.ReflectUtil;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: ReflectUtil測試
*/
public class ReflectTest {
private int id;
public ReflectTest() {
System.out.println("構(gòu)造方法");
}
public void show() {
System.out.println("普通方法");
}
public static void main(String[] args) throws IllegalAccessException {
// 獲取對象
ReflectTest reflectTest = ReflectUtil.newInstance(ReflectTest.class);
reflectTest.show();
// 構(gòu)造方法
Constructor[] constructors = ReflectUtil.getConstructors(ReflectTest.class);
for (Constructor constructor : constructors) {
System.out.println(constructor.getName());
}
// 獲取字段
Field field = ReflectUtil.getField(ReflectTest.class, "id");
field.setInt(reflectTest, 20);
// 輸出20
System.out.println(ReflectUtil.getFieldValue(reflectTest, field));
Method[] methods = ReflectUtil.getMethods(ReflectTest.class);
for (Method method : methods) {
System.out.println(method.getName());
}
// 獲取指定方法
Method method = ReflectUtil.getMethod(ReflectTest.class, "show");
System.out.println(method.getName());
// 執(zhí)行方法 輸出普通方法
ReflectUtil.invoke(reflectTest, "show");
}
}
4.身份證工具
使用IdcardUtil的isValidCard驗證身份證是否合法。
示例代碼如下:
import cn.hutool.core.util.IdcardUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: hutool測試
*/
@SpringBootTest
public class HutoolTest {
@Test
void test1() {
// 輸出false
System.out.println(IdcardUtil.isValidCard("43243"));
}
}
5.字段驗證器
驗證客戶端傳過來的數(shù)據(jù),比如手機號碼、郵箱、IP地址等是否合法。
示例代碼如下:
import cn.hutool.core.lang.Validator;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: hutool測試
*/
@SpringBootTest
public class HutoolTest {
@Test
void test1() {
String email = "hello@qq.com";
// 輸出true
System.out.println(Validator.isEmail(email));
String mobile = "1887888";
// 輸出false
System.out.println(Validator.isMobile(mobile));
String ip = "192.168";
// 輸出false
System.out.println(Validator.isIpv4(ip));
}
}
6.緩存工具
CacheUtil 是 Hutool 封裝的創(chuàng)建緩存的快捷工具類,可以創(chuàng)建不同的緩存對象:
- FIFOCache:先入先出,元素不停的加入緩存直到緩存滿為止,當(dāng)緩存滿時,清理過期緩存對象,清理后依舊滿則刪除先入的緩存。
- LFUCache,最少使用,根據(jù)使用次數(shù)來判定對象是否被持續(xù)緩存,當(dāng)緩存滿時清理過期對象,清理后依舊滿的情況下清除最少訪問的對象并將其他對象的訪問數(shù)減去這個最少訪問數(shù),以便新對象進入后可以公平計數(shù)。
- LRUCache,最近最久未使用,根據(jù)使用時間來判定對象是否被持續(xù)緩存,當(dāng)對象被訪問時放入緩存,當(dāng)緩存滿了,最久未被使用的對象將被移除。
示例代碼如下:
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: hutool測試
*/
@SpringBootTest
public class HutoolTest {
@Test
void test1() {
Cache<String, String> fifoCache = CacheUtil.newFIFOCache(3);
fifoCache.put("key1", "a");
fifoCache.put("key2", "b");
fifoCache.put("key3", "c");
fifoCache.put("key4", "d");
// 緩存大小為 3,所以 key4 放入后 key1 被清除 輸出null
System.out.println(fifoCache.get("key1"));
// 輸出b
System.out.println(fifoCache.get("key2"));
Cache<String, String> lfuCache = CacheUtil.newLFUCache(3);
lfuCache.put("key1", "a");
// 使用次數(shù)+1
lfuCache.get("key1");
lfuCache.put("key2", "b");
lfuCache.put("key3", "c");
lfuCache.put("key4", "d");
// 由于緩存容量只有 3,當(dāng)加入第 4 個元素的時候,最少使用的將被移除(2,3被移除)
// 都是輸出null
System.out.println(lfuCache.get("key2"));
System.out.println(lfuCache.get("key3"));
Cache<String, String> lruCache = CacheUtil.newLRUCache(3);
lruCache.put("key1", "a");
lruCache.put("key2", "b");
lruCache.put("key3", "c");
// 使用時間近了
lruCache.get("key1");
lruCache.put("key4", "d");
// 由于緩存容量只有 3,當(dāng)加入第 4 個元素的時候,最久使用的將被移除(2)
String value2 = lruCache.get("key2");
// 輸出null
System.out.println(value2);
}
}
三、Guava:Google開源的Java工具庫?
引入依賴
<!--guava-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.1-jre</version>
</dependency>
1.字符串處理
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Optional;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: Guava測試
*/
@SpringBootTest
public class GuavaTest {
@Test
void test() {
// 1.字符串拼接
Joiner joiner = Joiner.on(",");
// 輸出hello,world,qq
System.out.println(joiner.join("hello", "world", "qq"));
// 2.字符串拆分
String data = "hello,world,qq";
// 輸出[hello, world, qq]
System.out.println(Splitter.on(",").splitToList(data));
}
}
2.集合工具文章來源:http://www.zghlxwxcb.cn/news/detail-479990.html
示例代碼如下:
import com.google.common.collect.Lists;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
/**
* @author qinxun
* @date 2023-06-09
* @Descripion: Guava測試
*/
@SpringBootTest
public class GuavaTest {
@Test
void test() {
// 創(chuàng)建空集合
List<Integer> emptyList = Lists.newArrayList();
// 初始化集合
List<Integer> initList = Lists.newArrayList(1, 2, 3);
// 輸出[]
System.out.println(emptyList);
// 輸出[1, 2, 3]
System.out.println(initList);
}
}
到了這里,關(guān)于Java常用第三方工具類的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!