文章目录
Hutool2023
2306
- 列表工具-ListUtil
- 可以字符数组分段 ListUtil.split
// List 初始化
List<String> ceshi= Arrays.asList("12","34");
log.info(ceshi.toString());
// import cn.hutool.core.collection.ListUtil;
List<String> ceshi1= ListUtil.of("121", "34");
log.info(ceshi1.toString());
// Map初始化 通义灵码`搜索Map初始化`
// import com.google.common.collect.ImmutableMap;
Map<String, Integer> map1 = ImmutableMap.of("one", 1, "two", 2);
log.info(map1.toString());
// import cn.hutool.core.map.MapUtil;
Map<String, String> map = MapUtil.newHashMap();
map.put("a", "1");
map.put("b", "2");
map.put("c", "3");
map.put("d", "4");
log.info(map.toString());
Map<String, String> map2 = MapUtil.filter(map, (Filter<Entry<String, String>>) t -> Convert.toIn(t.getValue()) % 2 == 0); 过滤
优秀博客地址
优秀API
心得
- 感觉还是api更好一些,更全面一些,里面包含了很实用的方法,开发文档只是举例说明这个类的用途,但是举得例子未必是你需要的。
- hutool里面包含的类和方法是大而全的,你需要用到的可能只是里面的一小部分。所以把常用的找出来就可以了。
- 没有必要重复造轮子,一个人如果能够站的更高,看的更远,那么他一定是站在巨人的肩上。
- 他山之石,可以攻玉。
字符串工具-StrUtil
-
String fileName = StrUtil.removeSuffix(“pretty_girl.jpg”, “.jpg”) //fileName -> pretty_girl 去掉前缀
-
String str = “abcdefgh”;
String strSub1 = StrUtil.sub(str, 2, 3); //strSub1 -> c
String strSub2 = StrUtil.sub(str, 2, -3); //strSub2 -> cde
String strSub3 = StrUtil.sub(str, 3, 2); //strSub2 -> c -
String template = “{}爱{},就像老鼠爱大米”;
String str = StrUtil.format(template, “我”, “你”); //str -> 我爱你,就像老鼠爱大米 -
format(CharSequence template, Map<?,?> map)
格式化文本,使用 {varName} 占位
map = {a: “aValue”, b: “bValue”} format(“{a} and {b}”, map) —=》 aValue and bValue -
isBlankIfStr(Object obj)
如果对象是字符串是否为空白,空白的定义如下: -
str(Object obj, Charset charset)
将对象转为字符串 -
toString(Object obj)
调用对象的toString方法,null会返回“null” -
trim(String[] strs)
给定字符串数组全部做去首尾空格 -
uuid()
生成随机UUID -
utf8Str(Object obj)
将对象转为字符串集合串行流工具-CollStreamUtil
-
List list = CollStreamUtil.toList(null, Student::getName); 把对象的某个属性组成新的集合
-
List list = new ArrayList<>();
list.add(new Student(1, 1, 1, “张三”));
list.add(new Student(1, 2, 2, “李四”));
list.add(new Student(2, 1, 1, “擎天柱”));
list.add(new Student(2, 2, 2, “威震天”));
list.add(new Student(2, 3, 2, “霸天虎”));Map<Long, List> map = CollStreamUtil.groupByKey(list, Student::getClassId); 按照某个属性分组
-
Map<Long, String> map = map = CollStreamUtil.toMap(list, Student::getStudentId, Student::getName);
// 张三
map.get(1L); 从列表中,把2个属性组成map -
List list = new ArrayList<>();
list.add(new Student(1, 1, 1, “张三”));
list.add(new Student(1, 1, 2, “李四”));
list.add(new Student(1, 1, 3, “王五”));Map<Long, Student> map = CollStreamUtil.toIdentityMap(list, Student::getStudentId);
// 张三
map.get(1L).getName(); 按照列表的某一个属性,组成一个map
Map工具-MapUtil
-
Map<String, String> build = MapUtil.builder(new HashMap<String, String>())
.put(“key1”, “value1”)
.put(“key3”, “value3”)
.put(“key2”, “value2”).build();// key1value1key2value2key3value3
String join1 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false);
// key1value1key2value2key3value3123
String join2 = MapUtil.sortJoin(build, StrUtil.EMPTY, StrUtil.EMPTY, false, “123”); -
Map<String, String> map = MapUtil.newHashMap();
map.put(“a”, “1”);
map.put(“b”, “2”);
map.put(“c”, “3”);
map.put(“d”, “4”);Map<String, String> map2 = MapUtil.filter(map, (Filter<Entry<String, String>>) t -> Convert.toIn(t.getValue()) % 2 == 0);
-
Map<Object, Object> colorMap = MapUtil.of(new String[][] {
{“RED”, “#FF0000”},
{“GREEN”, “#00FF00”},
{“BLUE”, “#0000FF”}
}); -
filter(Map<K,V> map, K… keys)
过滤Map保留指定键值对,如果键不存在跳过 -
getAny(Map<K,V> map, K… keys)
获取Map的部分key生成新的Map -
getBool(Map<?,?> map, Object key)
获取Map指定key的值,并转换为Bool -
isEmpty(Map<?,?> map)
Map是否为空 -
partition(Map<K,V> map, int size)
将一个Map按照固定大小拆分成多个子Map -
sort(Map<K,V> map)
排序已有Map,Key有序的Map,使用默认Key排序方式(字母顺序)
列表工具-ListUtil
- List a = ListUtil.toLinkedList(“1”, “2”, “3”);
// 结果: [edit1, edit2, edit3]
List filter = ListUtil.filter(a, str -> “edit” + str); - List<List> lists = ListUtil.split(Arrays.asList(1, 2, 3, 4), 1);
List<List> lists = ListUtil.split(null, 3); - List a = ListUtil.toLinkedList(“1”, “2”, “3”);
final List filter = (List) CollUtil.edit(a, str -> “edit” + str);
// edit1
filter.get(0); - page(int pageNo, int pageSize, List list)
对指定List分页取值 - partition(List list, int size)
通过传入分区长度,将指定列表分区为不同的块,每块区域的长度相同(最后一块可能小于长度)
分区是在原List的基础上进行的,返回的分区是不可变的抽象列表,原列表元素变更,分区中元素也会变更。 - sort(List list, Comparator<? super T> c)
针对List排序,排序会修改原List - sortByProperty(List list, String property)
根据Bean的属性排序 - split(List list, int size)
对集合按照指定长度分段,每一个段为单独的集合,返回这个集合的列表 - toList(Collection collection)
新建一个ArrayList - toList(Iterator iterator)
新建一个ArrayList
提供的参数为null时返回空ArrayList - unmodifiable(List list)
将对应List转换为不可修改的List
Convert类 里面封装了针对Java常见类型的转换,用于简化类型转换。
日期时间工具-DateUtil
-
beginOfHour(Date date)获取某小时的开始时间
-
betweenDay(Date beginDate, Date endDate, boolean isReset) 判断两个日期相差的天数
-
createStopWatch(String id) 创建秒表StopWatch,用于对代码块的执行时间计数
-
dayOfMonth(Date date) 获得指定日期是这个日期所在月份的第几天
-
getChineseZodiac(int year) 计算生肖,只计算1900年后出生的人
-
getZodiac(int month, int day)通过生日计算星座
-
isIn(Date date, Date beginDate, Date endDate) 当前日期是否在日期指定范围内 起始日期和结束日期可以互换
-
isOverlap(Date realStartTime, Date realEndTime, Date startTime, Date endTime)
检查两个时间段是否有时间重叠
重叠指两个时间段是否有交集,注意此方法时间段重合时如: 此方法未纠正开始时间小于结束时间 当realStartTime和realEndTime或startTime和endTime相等时,退化为判断区间是否包含点 当realStartTime和realEndTime和startTime和endTime相等时,退化为判断点与点是否相等 See 准确的区间关系参考:艾伦区间代数 -
lengthOfMonth(int month, boolean isLeapYear) 获得指定月份的总天数
-
offsetDay(Date date, int offset)
w 偏移天 -
year(Date date)获得年的部分
常用工具文档地址
-
JWT
-
定时任务
-
Hutool-http
-
数据库(Hutool-db)
-
Hutool-json
-
线程和并发
-
文本操作
-
集合类
-
工具类
-
JavaBean