Hutool工具集之常用工具类详解使用(二)

本文详细介绍了Hutool工具集中的一些关键工具类,如ObjectUtil的hasBlank、hasEmpty方法,StrUtil的removePrefix、removeSuffix及format方法,CollUtil的join、sortPageAll系列方法,以及MapUtil、BeanUtil和JSONUtil的实用功能。通过这些工具类,可以提升代码的简洁性和效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.序言

接着上一次没写完的Hutool工具集之常用工具类详解使用(一)继续补充,希望这些工具类用到自己的代码后,可以使得代码变得“甜甜的”!

2.常用工具类

4)对象工具-ObjectUtil

public static void main(String[] args) {
        //准备数据
        Person person1 = new Person();
        Person person2 = person1;
        String str1="";
        String str2="";

        /*比较两个对象是否相等,相等需满足以下条件之一:
        1.obj1 == null && obj2 == null
        2.obj1.equals(obj2)*/
        //equal1:true
        boolean equal1 = ObjectUtil.equal(str1, str2);
        //equal2:true
        boolean equal2 = ObjectUtil.equal(person1, person2);

        //准备数据
        StringBuilder builder = StrUtil.builder(5).append("d").append("e");
        ArrayList<Integer> list = CollUtil.newArrayList(1, 2, 3);
        Object[] array = ArrayUtil.newArray(3);
        HashMap<Object, Object> hashMap = CollUtil.newHashMap(6);
        /*计算对象长度,如果是字符串调用其length函数,集合类调用其size函数,数组调用其length属性,
        其他可遍历对象遍历计算长度,支持的类型包括: CharSequence Map Iterator Enumeration Array*/
        //length1=2
        int length1 = ObjectUtil.length(builder);
        //length2 = 3
        int length2 = ObjectUtil.length(list);
        //length3 = 3
        int length3 = ObjectUtil.length(array);
        //length4 = 0
        int length4 = ObjectUtil.length(hashMap);


        /*对象中是否包含元素。支持的对象类型包括: String Collection Map Iterator Enumeration Array*/
        //dFlag = true
        boolean dFlag = ObjectUtil.contains(builder.toString(), "d");
        //contains = false
        boolean contains = ObjectUtil.contains(list, 4);
        //arrayFlag = true
        boolean arrayFlag = ObjectUtil.contains(array, null);
        //mapFlag = false
        boolean mapFlag = ObjectUtil.contains(hashMap, null);


        /*判断是否为null*/
        //strFlag = false
        boolean strFlag = ObjectUtil.isNull(builder);
        //arrFlag = true
        boolean arrFlag = ObjectUtil.isNotNull(array);
        //notEmpty = true
        boolean notEmpty = ArrayUtil.isNotEmpty(array);

    }

5)字符串工具-StrUtil

常用的方法例如判断字符串是否为空isBlank、isNotBlank、isEmpty、isNotEmpty这些我就不做介绍了,一目了然的方法。

1. hasBlank、hasEmpty方法

就是给定一些字符串,如果一旦有空的就返回true,常用于判断好多字段是否有空的(例如web表单数据)。
这两个方法的区别是hasEmpty只判断是否为null或者空字符串(""),hasBlank则会把不可见字符也算做空,isEmpty和isBlank同理。

 public static void main(String[] args) {

        String s1 = " ";
        //strFlag1 = true
        boolean strFlag1 = StrUtil.hasBlank(s1);
        //strFlag2 = false
        boolean strFlag2 = StrUtil.hasEmpty(s1);
    }
2.removePrefix、removeSuffix方法

这两个是去掉字符串的前缀后缀的,例如去个文件名的扩展名啥。
还有忽略大小写的removePrefixIgnoreCase和removeSuffixIgnoreCase都比较实用。

//fileName -> pretty_girl
String fileName = StrUtil.removeSuffix("pretty_girl.jpg", ".jpg")  
3.sub方法

1.index的位置包头不包尾
2.index的位置可以是负数,-1表示最后一个字符
3.有自动修正的功能

String str = "abcdefgh";
//strSub1 -> c
String strSub1 = StrUtil.sub(str, 2, 3); 
//strSub2 -> cde
String strSub2 = StrUtil.sub(str, 2, -3); 
//strSub2 -> c
String strSub3 = StrUtil.sub(str, 3, 2); 
4.format方法
String template = "{}爱{},就像老鼠爱大米";
String str = StrUtil.format(template, "我", "你"); //str -> 我爱你,就像老鼠爱大米

参数定义成了Object类型,如果传别的类型的也可以,会自动调用toString()方法的。

6)集合工具 CollUtil

1. join 方法

将集合转换为字符串,这个方法还是挺常用,是StrUtil.split的反方法。这个方法的参数支持各种类型对象的集合,最后连接每个对象时候调用其toString()方法。栗子如下:

String[] col= new String[]{"a","b","c","d","e"};
List<String> colList = CollUtil.newArrayList(col);
//str -> a#b#c#d#e
String str = CollUtil.join(colList, "#"); 
2. sortPageAll、sortPageAll2方法

这个方法其实是一个真正的组合方法,功能是:将给定的多个集合放到一个列表(List)中,根据给定的Comparator对象排序,然后分页取数据。这个方法非常类似于数据库多表查询后排序分页,这个方法存在的意义也是在此。sortPageAll2功能和sortPageAll的使用方式和结果是 一样的,区别是sortPageAll2使用了BoundedPriorityQueue这个类来存储组合后的列表,不知道哪种性能更好一些,所以就都保留了。使用此方法,栗子如下:

public static void main(String[] args) {
        //Integer比较器
        Comparator<Integer> comparator = new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        };

        //新建三个列表,CollUtil.newArrayList方法表示新建ArrayList并填充元素
        List<Integer> list1 = CollUtil.newArrayList(1, 2, 3);
        List<Integer> list2 = CollUtil.newArrayList(4, 5, 6);
        List<Integer> list3 = CollUtil.newArrayList(7, 8, 9);

        //告诉编译器忽略 unchecked 警告信息,如使用List,ArrayList等未进行参数化产生的警告信息。
        @SuppressWarnings("unchecked")
        //参数表示把list1,list2,list3合并并按照从小到大排序后,取0~2个(包括第0个,不包括第2个),结果是[9,8]
        List<Integer> result = CollUtil.sortPageAll(0, 2, comparator, list1, list2, list3);
    }
3.newHashMap、newHashSet、newArrayList方法

这些方法是新建相应的数据结构,数据结构元素的类型取决于你变量的类型,栗子如下:

HashMap<String, String> map = CollUtil.newHashMap();
HashSet<String> set = CollUtil.newHashSet();
ArrayList<String> list = CollUtil.newArrayList();
4.range方法

这个方法来源于Python的一个语法糖,给定开始和结尾以及步进,就会生成一个等差数列(列表)

//[0,1,2,3,4,5]
int[] a1 = CollUtil.range(6);    
//[4,5,6]   
int[] a2 = CollUtil.range(4, 7);    
//[4,6,8]
int[] a3 = CollUtil.range(4, 9, 2); 
5.isEmpty、isNotEmpty方法

判断集合是否为空(包括null和没有元素的集合)。

6.zip方法

此方法也是来源于Python的一个语法糖,给定两个集合,然后两个集合中的元素一一对应,成为一个Map。此方法还有一个重载方法,可以传字符,然后给定分分隔符,字符串会被split成列表。栗子:

String[] keys = new String[]{"a", "b", "c"};
Integer[] values = new Integer[]{1, 2, 3};
// {b=2, c=3, a=1}
Map<String, Integer> map = CollUtil.zip(keys,values);

String a = "a,b,c";
String b = "1,2,3";
// {b=2, c=3, a=1}
Map<String, String> map2 = CollUtil.zip(a,b, ",");

7)Map工具-MapUtil

8)Bean工具-BeanUtil

9)JSONUtil

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值