国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Java常用第三方工具類

這篇具有很好參考價值的文章主要介紹了Java常用第三方工具類。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、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.集合工具

示例代碼如下:

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)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • Swift常用的第三方庫

    以下是一些常用的Swift第三方庫及其鏈接: Alamofire:用于網(wǎng)絡(luò)請求的庫。https://github.com/Alamofire/Alamofire Kingfisher:用于異步下載和緩存圖片的庫。https://github.com/onevcat/Kingfisher SwiftyJSON:用于處理JSON數(shù)據(jù)的庫。https://github.com/SwiftyJSON/SwiftyJSON Realm:一個移動數(shù)據(jù)庫。https://github.

    2024年04月28日
    瀏覽(96)
  • spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實現(xiàn)小程序微信登錄

    spring boot整合第三方微信開發(fā)工具 weixin-java-miniapp 實現(xiàn)小程序微信登錄

    有時候項目需要用到微信登錄或獲取用戶的手機號碼,weixin-java-miniapp是一個好用的第三方工具,不用我們自己寫httpcline調(diào)用。 導(dǎo)入jar包 添加一個resource.properties文件,寫上小程序的appid和secret 添加兩個配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序給微信發(fā)送消息

    2024年02月16日
    瀏覽(93)
  • JMeter進階-常用第三方插件講解

    JMeter進階-常用第三方插件講解

    準備工作: 1.最新版本的JMeter是默認不展示插件管理器的,所以我們需要手動添加插件管理器 2.下載地址:https://jmeter-plugins.org/install/Install/,下載插件plugins-manager.jar,然后將jar包放在apache-jmeter-x.x.xlibext路徑下,重新打開jmeter客戶端即可在“選項”下面可以看到了Plugins-Ma

    2023年04月08日
    瀏覽(19)
  • SpringBoot集成常用第三方框架-ES

    SpringBoot集成常用第三方框架-ES

    作者主頁:編程指南針 作者簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者、CSDN博客專家 、CSDN內(nèi)容合伙人、掘金特邀作者、阿里云博客專家、51CTO特邀作者、多年架構(gòu)師設(shè)計經(jīng)驗、騰訊課堂常駐講師 主要內(nèi)容:Java項目、Python項目、前端項目、人工智能與大數(shù)據(jù)、簡歷模板、學(xué)習(xí)資料、面試題庫

    2024年02月03日
    瀏覽(96)
  • SpringBoot集成常用第三方框架-RabbitMQ

    SpringBoot集成常用第三方框架-RabbitMQ

    作者主頁:編程指南針 作者簡介:Java領(lǐng)域優(yōu)質(zhì)創(chuàng)作者、CSDN博客專家 、CSDN內(nèi)容合伙人、掘金特邀作者、阿里云博客專家、51CTO特邀作者、多年架構(gòu)師設(shè)計經(jīng)驗、騰訊課堂常駐講師 主要內(nèi)容:Java項目、Python項目、前端項目、人工智能與大數(shù)據(jù)、簡歷模板、學(xué)習(xí)資料、面試題庫

    2024年01月17日
    瀏覽(90)
  • 吐血整理!Python常用第三方庫,碼?。。?!

    吐血整理!Python常用第三方庫,碼?。。?!

    Python作為一種編程語言近年來越來越受歡迎,它為什么這么火? 其中一個重要原因就是因為Python的庫豐富——Python語言提供超過15萬個第三方庫,Python庫之間廣泛聯(lián)系、逐層封裝。幾乎覆蓋信息技術(shù)所有領(lǐng)域,下面簡單介紹下數(shù)據(jù)分析與可視化、網(wǎng)絡(luò)爬蟲、自動化、WEB開發(fā)、

    2024年02月11日
    瀏覽(46)
  • Android常用的第三方庫--.jar、.aar

    Android常用的第三方庫--.jar、.aar

    JAR(Java Archive,Java 歸檔文件)是與平臺無關(guān)的文件格式,它允許將許多文件組合成一個壓縮文 件。JAR是 Java 的一種文檔格式,是一種與平臺無關(guān)的文件格式,可將多個文件合成一個文件。只包含了class文件與清單文件 , 不包含資源文件,如圖片等所有res中的文件 。 JAR的優(yōu)

    2024年02月03日
    瀏覽(31)
  • 【python】(十九)python常用第三方庫——urllib3

    官方文檔:https://urllib3.readthedocs.io/en/stable/ Urllib3是一個功能強大,條理清晰,用于HTTP客戶端的Python庫,許多Python的原生系統(tǒng)已經(jīng)開始使用urllib3。Urllib3提供了很多python標(biāo)準庫里所沒有的重要特性: 線程安全 連接池管理 客戶端 SSL/TLS 驗證 支持 HTTP 和 SOCKS 代理 …… 通過 pip

    2024年02月13日
    瀏覽(124)
  • 避免使用第三方工具完成電腦環(huán)境檢測

    避免使用第三方工具完成電腦環(huán)境檢測

    在之前配置各種深度學(xué)習(xí)環(huán)境的時候經(jīng)常需要先檢測一下電腦的軟硬件環(huán)境,其實整個過程比較重復(fù)和固定,所以我們是否有可能一鍵檢測Python版本、PIP版本、Conda版本、CUDA版本、電腦系統(tǒng)、CPU核數(shù)、CPU頻率、內(nèi)存、硬盤等內(nèi)容這是很多Deepper苦惱的。這里會從軟件開始介紹,

    2024年02月10日
    瀏覽(31)
  • 京東數(shù)據(jù)分析工具推薦(京東第三方數(shù)據(jù)平臺)

    京東數(shù)據(jù)分析工具推薦(京東第三方數(shù)據(jù)平臺)

    京東平臺的店鋪眾多,同行數(shù)不勝數(shù)。作為商家,如果連自己競爭對手的情況都不知道的話,很難在這個平臺存活下去。 那么,這次鯨參謀就來重點說一下我們的京東數(shù)據(jù)分析工具里的“競品分析”功能。 競品分析,主要是對京東店鋪運營期間競爭對手的市場經(jīng)營狀況與策略

    2024年02月04日
    瀏覽(46)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包