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和""和" "

示例代码如下:文章来源地址https://www.toymoban.com/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));
    }
}

执行结果:

true
true
false
false
false
=====
false
false
true
true
true
=====
true
true
true
false
false
=====
false
false
false
true
true

2.分割字符串

使用StringUtils的split()方法分割字符串成数组。

示例代码如下:

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.判断是否纯数字

使用StringUtils的isNumeric()方法判断字符串是否是纯数字形式。

示例代码如下:

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,"拼接的字符")方法将集合的数据拼接成字符串。

示例代码如下:

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):去除字符串首尾的空白字符,如果结果为空字符串,则返回 null。
  • equals(String str1, String str2):比较两个字符串是否相等。
  • equalsIgnoreCase(String str1, String str2):比较两个字符串是否相等,忽略大小写。
  • startsWith(String str, String prefix):检查字符串是否以指定的前缀开头。
  • endsWith(String str, String suffix):检查字符串是否以指定的后缀结尾。
  • contains(String str, CharSequence seq):检查字符串是否包含指定的字符序列。
  • indexOf(String str, CharSequence seq):返回指定字符序列在字符串中首次出现的索引,如果没有找到,则返回 -1。
  • lastIndexOf(String str, CharSequence seq):返回指定字符序列在字符串中最后一次出现的索引,如果没有找到,则返回 -1。
  • substring(String str, int start, int end):截取字符串中指定范围的子串。
  • replace(String str, String searchString, String replacement):替换字符串中所有出现的搜索字符串为指定的替换字符串。
  • replaceAll(String str, String regex, String replacement):使用正则表达式替换字符串中所有匹配的部分。
  • join(Iterable<?> iterable, String separator):使用指定的分隔符将可迭代对象中的元素连接为一个字符串。
  • split(String str, String separator):使用指定的分隔符将字符串分割为一个字符串数组。
  • capitalize(String str):将字符串的第一个字符转换为大写。
  • uncapitalize(String str):将字符串的第一个字符转换为小写。

二、Hutool工具包

引入依赖:

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

1.类型转换

我们接收客户端传过来的数据的时候,通常我们需要把这些数据转换成我们需要的数据类型。

示例代码如下:

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));
        // 设置了默认值 输出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.日期时间转换

示例代码如下:

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 包括:

  • 获取构造方法
  • 获取字段
  • 获取字段值
  • 获取方法
  • 执行方法(对象方法和静态方法)

示例代码如下:

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("构造方法");
    }

    public void show() {
        System.out.println("普通方法");
    }


    public static void main(String[] args) throws IllegalAccessException {
        // 获取对象
        ReflectTest reflectTest = ReflectUtil.newInstance(ReflectTest.class);
        reflectTest.show();

        // 构造方法
        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());

        // 执行方法 输出普通方法
        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.字段验证器

验证客户端传过来的数据,比如手机号码、邮箱、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 封装的创建缓存的快捷工具类,可以创建不同的缓存对象:

  • FIFOCache:先入先出,元素不停的加入缓存直到缓存满为止,当缓存满时,清理过期缓存对象,清理后依旧满则删除先入的缓存。
  • LFUCache,最少使用,根据使用次数来判定对象是否被持续缓存,当缓存满时清理过期对象,清理后依旧满的情况下清除最少访问的对象并将其他对象的访问数减去这个最少访问数,以便新对象进入后可以公平计数。
  • LRUCache,最近最久未使用,根据使用时间来判定对象是否被持续缓存,当对象被访问时放入缓存,当缓存满了,最久未被使用的对象将被移除。

示例代码如下:

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");
        // 使用次数+1
        lfuCache.get("key1");
        lfuCache.put("key2", "b");
        lfuCache.put("key3", "c");
        lfuCache.put("key4", "d");
        // 由于缓存容量只有 3,当加入第 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,当加入第 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() {
        // 创建空集合
        List<Integer> emptyList = Lists.newArrayList();
        // 初始化集合
        List<Integer> initList = Lists.newArrayList(1, 2, 3);

        // 输出[]
        System.out.println(emptyList);
        // 输出[1, 2, 3]
        System.out.println(initList);
    }
}

到了这里,关于Java常用第三方工具类的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Flutter:第三方常用库整理

    随着Flutter的不断学习,接触了不少第三方的库。因此打算进行简单的整理。 简介 一个强大的Dart/FlutterHTTP客户端,支持全局配置, 拦截器、表单数据、请求取消、文件上传/下载、 超时和自定义适配器等。 官方地址 https://pub-web.flutter-io.cn/packages/dio 简单使用 flutter:网络请求

    2024年02月16日
    浏览(33)
  • spring boot整合第三方微信开发工具 weixin-java-miniapp 实现小程序微信登录

    有时候项目需要用到微信登录或获取用户的手机号码,weixin-java-miniapp是一个好用的第三方工具,不用我们自己写httpcline调用。 导入jar包 添加一个resource.properties文件,写上小程序的appid和secret 添加两个配置文件 WxMaProperties.java WxMaConfiguration.java 如何使用 小程序给微信发送消息

    2024年02月16日
    浏览(33)
  • 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日
    浏览(39)
  • SpringBoot集成常用第三方框架-RabbitMQ

    作者主页:编程指南针 作者简介:Java领域优质创作者、CSDN博客专家 、CSDN内容合伙人、掘金特邀作者、阿里云博客专家、51CTO特邀作者、多年架构师设计经验、腾讯课堂常驻讲师 主要内容:Java项目、Python项目、前端项目、人工智能与大数据、简历模板、学习资料、面试题库

    2024年01月17日
    浏览(35)
  • SpringBoot集成常用第三方框架-ES

    作者主页:编程指南针 作者简介:Java领域优质创作者、CSDN博客专家 、CSDN内容合伙人、掘金特邀作者、阿里云博客专家、51CTO特邀作者、多年架构师设计经验、腾讯课堂常驻讲师 主要内容:Java项目、Python项目、前端项目、人工智能与大数据、简历模板、学习资料、面试题库

    2024年02月03日
    浏览(38)
  • 吐血整理!Python常用第三方库,码住!!!

    Python作为一种编程语言近年来越来越受欢迎,它为什么这么火? 其中一个重要原因就是因为Python的库丰富——Python语言提供超过15万个第三方库,Python库之间广泛联系、逐层封装。几乎覆盖信息技术所有领域,下面简单介绍下数据分析与可视化、网络爬虫、自动化、WEB开发、

    2024年02月11日
    浏览(35)
  • Android常用的第三方库--.jar、.aar

    JAR(Java Archive,Java 归档文件)是与平台无关的文件格式,它允许将许多文件组合成一个压缩文 件。JAR是 Java 的一种文档格式,是一种与平台无关的文件格式,可将多个文件合成一个文件。只包含了class文件与清单文件 , 不包含资源文件,如图片等所有res中的文件 。 JAR的优

    2024年02月03日
    浏览(34)
  • 【python】(十九)python常用第三方库——urllib3

    官方文档:https://urllib3.readthedocs.io/en/stable/ Urllib3是一个功能强大,条理清晰,用于HTTP客户端的Python库,许多Python的原生系统已经开始使用urllib3。Urllib3提供了很多python标准库里所没有的重要特性: 线程安全 连接池管理 客户端 SSL/TLS 验证 支持 HTTP 和 SOCKS 代理 …… 通过 pip

    2024年02月13日
    浏览(36)
  • 避免使用第三方工具完成电脑环境检测

    在之前配置各种深度学习环境的时候经常需要先检测一下电脑的软硬件环境,其实整个过程比较重复和固定,所以我们是否有可能一键检测Python版本、PIP版本、Conda版本、CUDA版本、电脑系统、CPU核数、CPU频率、内存、硬盘等内容这是很多Deepper苦恼的。这里会从软件开始介绍,

    2024年02月10日
    浏览(49)
  • 京东数据分析工具推荐(京东第三方数据平台)

    京东平台的店铺众多,同行数不胜数。作为商家,如果连自己竞争对手的情况都不知道的话,很难在这个平台存活下去。 那么,这次鲸参谋就来重点说一下我们的京东数据分析工具里的“竞品分析”功能。 竞品分析,主要是对京东店铺运营期间竞争对手的市场经营状况与策略

    2024年02月04日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包