Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
沉默王二
Jmx Java
提交
939608a3
J
Jmx Java
项目概览
沉默王二
/
Jmx Java
大约 1 年 前同步成功
通知
160
Star
18
Fork
2
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
J
Jmx Java
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
939608a3
编写于
3月 30, 2023
作者:
沉默王二
💬
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
常用工具类
上级
b618893e
变更
9
展开全部
隐藏空白更改
内联
并排
Showing
9 changed file
with
326 addition
and
249 deletion
+326
-249
README.md
README.md
+2
-1
docs/common-tool/Objects.md
docs/common-tool/Objects.md
+1
-1
docs/common-tool/StringUtils.md
docs/common-tool/StringUtils.md
+188
-0
docs/common-tool/collections.md
docs/common-tool/collections.md
+88
-1
docs/common-tool/guava.md
docs/common-tool/guava.md
+2
-2
docs/common-tool/hutool.md
docs/common-tool/hutool.md
+1
-1
docs/common-tool/utils.md
docs/common-tool/utils.md
+42
-242
docs/home.md
docs/home.md
+2
-1
二哥的 Java 进阶之路.md
二哥的 Java 进阶之路.md
+0
-0
未找到文件。
README.md
浏览文件 @
939608a3
...
...
@@ -197,11 +197,12 @@
-
[
Java Scanner:扫描控制台输入的工具类
](
docs/common-tool/scanner.md
)
-
[
Java Arrays:专为数组而生的工具类
](
docs/common-tool/arrays.md
)
-
[
Apache StringUtils:专为Java字符串而生的工具类
](
docs/common-tool/StringUtils.md
)
-
[
Objects:专为操作Java对象而生的工具类
](
docs/common-tool/Objects.md
)
-
[
Java Collections:专为集合而生的工具类
](
docs/common-tool/collections.md
)
-
[
Hutool:国产良心工具包,让你的Java变得更甜
](
docs/common-tool/hutool.md
)
-
[
Guava:Google开源的Java工具库,太强大了
](
docs/common-tool/guava.md
)
-
[
其他常用Java工具类:IpUtil、
CollectionUtils、StringUtils、
MDC、ClassUtils、BeanUtils、ReflectionUtils
](
docs/common-tool/utils.md
)
-
[
其他常用Java工具类:IpUtil、MDC、ClassUtils、BeanUtils、ReflectionUtils
](
docs/common-tool/utils.md
)
## Java新特性
...
...
docs/common-tool/Objects.md
浏览文件 @
939608a3
...
...
@@ -12,7 +12,7 @@ head:
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Objects,java objects
---
# 9.
3
Objects
# 9.
4
Objects
Java 的 Objects 类是一个实用工具类,包含了一系列静态方法,用于处理对象。它位于 java.util 包中,自 Java 7 引入。Objects 类的主要目的是降低代码中的
[
空指针异常
](
https://tobebetterjavaer.com/exception/npe.html
)
(
NullPointerException
)
风险,同时提供一些非常实用的方法供我们使用。
...
...
docs/common-tool/StringUtils.md
0 → 100644
浏览文件 @
939608a3
---
title
:
Apache StringUtils:专为Java字符串而生的工具类
shortTitle
:
Apache StringUtils
category
:
-
Java核心
tag
:
-
常用工具类
description
:
Java程序员进阶之路,小白的零基础Java教程,从入门到进阶,Java Arrays工具类的10大常用方法
head
:
-
-
meta
-
name
:
keywords
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Apache StringUtils,java StringUtils
---
# 9.3 Apache StringUtils
`字符串`
(
[
String
](
https://tobebetterjavaer.com/string/immutable.html
)
)在我们的日常工作中,用得非常非常非常多。
在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、
[
分隔字符串
](
https://tobebetterjavaer.com/string/split.html
)
、
[
比较字符串
](
https://tobebetterjavaer.com/string/equals.html
)
、去掉多余空格、
[
拼接字符串
](
https://tobebetterjavaer.com/string/join.html
)
、使用正则表达式等等。
如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。
现在有个好消息是:
`org.apache.commons.lang3`
包下的
`StringUtils`
工具类,给我们提供了非常丰富的选择。
Maven 坐标:
```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
```
StringUtils 提供了非常多实用的方法,大概有下图的四页到五页,我只截了两页,实在是太多了。
![](
https://cdn.tobebetterjavaer.com/stutymore/StringUtils-20230330111122.png
)
接下来,我们来拿一些常用的方法举例说明。
### 字符串判空
其实空字符串,不只是 null 一种,还有""," ","null"等等,多种情况。
StringUtils 给我们提供了多个判空的静态方法,例如:
```
java
String
str1
=
null
;
String
str2
=
""
;
String
str3
=
" "
;
String
str4
=
"abc"
;
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
(
"====="
);
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
(
"====="
);
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
(
"====="
);
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str1
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str2
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str3
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str4
));
```
执行结果:
```
java
true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true
```
示例中的:
`isEmpty`
、
`isNotEmpty`
、
`isBlank`
和
`isNotBlank`
,这 4 个判空方法你们可以根据实际情况使用。
优先推荐使用
`isBlank`
和
`isNotBlank`
方法,因为它会把
`" "`
也考虑进去。
### 分隔字符串
分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。
```
java
String
str1
=
null
;
System
.
out
.
println
(
StringUtils
.
split
(
str1
,
","
));
System
.
out
.
println
(
str1
.
split
(
","
));
```
执行结果:
```
java
null
Exception
in
thread
"main"
java
.
lang
.
NullPointerException
\
tat
com
.
sue
.
jump
.
service
.
test1
.
UtilTest
.
main
(
UtilTest
.
java
:
21
)
```
使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。
### 判断是否纯数字
给定一个字符串,判断它是否为纯数字,可以使用
`isNumeric`
方法。例如:
```
java
String
str1
=
"123"
;
String
str2
=
"123q"
;
String
str3
=
"0.33"
;
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str1
));
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str2
));
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str3
));
```
执行结果:
```
java
true
false
false
```
### 将集合拼接成字符串
有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用
`join`
方法。例如:
```
java
List
<
String
>
list
=
Lists
.
newArrayList
(
"a"
,
"b"
,
"c"
);
List
<
Integer
>
list2
=
Lists
.
newArrayList
(
1
,
2
,
3
);
System
.
out
.
println
(
StringUtils
.
join
(
list
,
","
));
System
.
out
.
println
(
StringUtils
.
join
(
list2
,
" "
));
```
执行结果:
```
java
a
,
b
,
c
1
2
3
```
### 其他方法
这里再列举一些,其他的方法可以自己去研究一下。
-
`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)`
:将字符串的第一个字符转换为小写。
----
最近整理了一份牛逼的学习资料,包括但不限于Java基础部分(JVM、Java集合框架、多线程),还囊括了
**数据库、计算机网络、算法与数据结构、设计模式、框架类Spring、Netty、微服务(Dubbo,消息队列) 网关**
等等等等……详情戳:
[
可以说是2022年全网最全的学习和找工作的PDF资源了
](
https://tobebetterjavaer.com/pdf/programmer-111.html
)
微信搜
**沉默王二**
或扫描下方二维码关注二哥的原创公众号沉默王二,回复
**111**
即可免费领取。
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/gongzhonghao.png
)
\ No newline at end of file
docs/common-tool/collections.md
浏览文件 @
939608a3
...
...
@@ -12,7 +12,7 @@ head:
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Collections,集合框架,java Collections
---
# 9.
4
Collections
# 9.
5
Collections
Collections 是 JDK 提供的一个工具类,位于 java.util 包下,提供了一系列的静态方法,方便我们对集合进行各种骚操作,算是集合框架的一个大管家。
...
...
@@ -250,6 +250,93 @@ addAll 后:[沉默王九, 沉默王十, 沉默王二]
是否没有交集:否
```
### 06、CollectionUtils:Spring 和 Apache 都有提供的集合工具类
对集合操作,除了前面说的 JDK 原生
`Collections`
工具类,
`CollectionUtils`
工具类也很常用。
目前比较主流的是
`Spring`
的
`org.springframework.util`
包下的 CollectionUtils 工具类。
![](
https://cdn.tobebetterjavaer.com/stutymore/utils-20230330101919.png
)
和
`Apache`
的
`org.apache.commons.collections`
包下的 CollectionUtils 工具类。
![](
https://cdn.tobebetterjavaer.com/stutymore/utils-20230330103825.png
)
Maven 坐标如下:
```
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
```
Apache 的方法比 Spring 的更多一些,我们就以 Apache 的为例,来介绍一下常用的方法。
#### 集合判空
通过 CollectionUtils 工具类的
`isEmpty`
方法可以轻松判断集合是否为空,
`isNotEmpty`
方法判断集合不为空。
```
java
List
<
Integer
>
list
=
new
ArrayList
<>();
list
.
add
(
2
);
list
.
add
(
1
);
list
.
add
(
3
);
if
(
CollectionUtils
.
isEmpty
(
list
))
{
System
.
out
.
println
(
"集合为空"
);
}
if
(
CollectionUtils
.
isNotEmpty
(
list
))
{
System
.
out
.
println
(
"集合不为空"
);
}
```
#### 对两个集合进行操作
有时候我们需要对已有的两个集合进行操作,比如取交集或者并集等。
```
java
List
<
Integer
>
list
=
new
ArrayList
<>();
list
.
add
(
2
);
list
.
add
(
1
);
list
.
add
(
3
);
List
<
Integer
>
list2
=
new
ArrayList
<>();
list2
.
add
(
2
);
list2
.
add
(
4
);
//获取并集
Collection
<
Integer
>
unionList
=
CollectionUtils
.
union
(
list
,
list2
);
System
.
out
.
println
(
unionList
);
//获取交集
Collection
<
Integer
>
intersectionList
=
CollectionUtils
.
intersection
(
list
,
list2
);
System
.
out
.
println
(
intersectionList
);
//获取交集的补集
Collection
<
Integer
>
disjunctionList
=
CollectionUtils
.
disjunction
(
list
,
list2
);
System
.
out
.
println
(
disjunctionList
);
//获取差集
Collection
<
Integer
>
subtractList
=
CollectionUtils
.
subtract
(
list
,
list2
);
System
.
out
.
println
(
subtractList
);
```
执行结果:
```
java
[
1
,
2
,
3
,
4
]
[
2
]
[
1
,
3
,
4
]
[
1
,
3
]
```
说句实话,对两个集合的操作,在实际工作中用得挺多的,特别是很多批量的场景中。以前我们需要写一堆代码,但没想到有现成的轮子。
### 07、小结
整体上,Collections 工具类作为集合框架的大管家,提供了一些非常便利的方法供我们调用,也非常容易掌握,没什么难点,看看方法的注释就能大致明白干嘛的。
不过,工具就放在那里,用是一回事,为什么要这么用就是另外一回事了。能不能提高自己的编码水平,很大程度上取决于你到底有没有去钻一钻源码,看这些设计 JDK 的大师们是如何写代码的,学会一招半式,在工作当中还是能很快脱颖而出的。
...
...
docs/common-tool/guava.md
浏览文件 @
939608a3
...
...
@@ -12,7 +12,7 @@ head:
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Guava,java guava,google guava
---
# 9.
6
Guava
# 9.
7
Guava
### 01、前世今生
...
...
@@ -329,7 +329,7 @@ Lists还有其他的好用的工具,我在这里只是抛砖引玉,有兴趣
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/guava-4b962b06-a626-4707-9fe9-f5729536d9c5.jpg
)
### 0
7
、尾声
### 0
8
、尾声
上面介绍了我认为最常用的功能,作为 Google 公司开源的 Java 开发核心库,个人觉得实用性还是很高的(不然呢?嘿嘿嘿)。引入到你的项目后不仅能快速的实现一些开发中常用的功能,而且还可以让代码更加的优雅简洁。
...
...
docs/common-tool/hutool.md
浏览文件 @
939608a3
...
...
@@ -12,7 +12,7 @@ head:
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,Hutool,java hutool
---
# 9.
5
Hutool
# 9.
6
Hutool
读者群里有个小伙伴感慨说,“Hutool 这款开源类库太厉害了,基本上该有该的工具类,它里面都有。”讲真的,我平常工作中也经常用 Hutool,它确实可以帮助我们简化每一行代码,使 Java 拥有函数式语言般的优雅,让 Java 语言变得“甜甜的”。
...
...
docs/common-tool/utils.md
浏览文件 @
939608a3
---
title
:
其他常用Java工具类:I
pUtil、CollectionUtils、String
Utils、MDC、ClassUtils、BeanUtils、ReflectionUtils
title
:
其他常用Java工具类:I
PUtil、Collection
Utils、MDC、ClassUtils、BeanUtils、ReflectionUtils
shortTitle
:
其他常用Java工具类
category
:
-
Java核心
...
...
@@ -9,10 +9,10 @@ description: Java程序员进阶之路,小白的零基础Java教程,从入
head
:
-
-
meta
-
name
:
keywords
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,工具类,轮子,java 工具类
content
:
Java,Java SE,Java基础,Java教程,Java程序员进阶之路,Java进阶之路,Java入门,教程,java,工具类,轮子,java 工具类
,java IPUtil,java CollectionUtils,
---
# 9.
7 其他常用Java
工具类
# 9.
8 其他常用 Java
工具类
除了我们前面提到的 Java 原生工具类,比如说
[
Arrays
](
https://tobebetterjavaer.com/common-tool/arrays.html
)
、
[
Objects
](
https://tobebetterjavaer.com/common-tool/Objects.html
)
、
[
Collections
](
https://tobebetterjavaer.com/common-tool/collections.html
)
、
[
Scanner
](
https://tobebetterjavaer.com/common-tool/scanner.html
)
等,还有一些第三方的工具类,比如说
[
Hutool
](
https://tobebetterjavaer.com/common-tool/hutool.html
)
、
[
Guava
](
https://tobebetterjavaer.com/common-tool/guava.html
)
等,以及我们今天介绍的 IpUtil、CollectionUtils、StringUtils、MDC、ClassUtils、BeanUtils、ReflectionUtils 等等,在很大程度上能够提高我们的生产效率。
...
...
@@ -42,52 +42,53 @@ public static String getLocalIP() {
本机执行后截图如下:
![](
https://cdn.tobebetterjavaer.com/
tobebetterjavaer/images/common-tool/IpUtil-f35dc96f-b8ac-43d3-9393-0ff565e85fb9.jp
g
)
![](
https://cdn.tobebetterjavaer.com/
stutymore/utils-20230330093633.pn
g
)
阿里云机器执行后截图如下:
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-f50b0de2-cf0d-4e9b-8f10-838ea4b47fd8.jpg
)
再问一句,那是否就真的没有问题了呢?在某些情况下,可能返回的是
`127.0.0.1`
在虚拟机中执行时,就可能遇到这个问题,截图如下
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/IpUtil-7c14024b-57d1-4086-9f51-d7bf312b5fbf.jpg
)
![](
https://cdn.tobebetterjavaer.com/stutymore/utils-20230330095801.png
)
#### 2. 进阶版
做一点简单的改动,获取 I
p
V4 的地址,源码如下
做一点简单的改动,获取 I
P
V4 的地址,源码如下
```
java
/**
* 直接根据第一个网卡地址作为其内网ipv4地址,避免返回 127.0.0.1
*
* @return
*/
public
static
String
getLocalIpByNetcard
()
{
try
{
// 枚举所有的网络接口
for
(
Enumeration
<
NetworkInterface
>
e
=
NetworkInterface
.
getNetworkInterfaces
();
e
.
hasMoreElements
();
)
{
// 获取当前网络接口
NetworkInterface
item
=
e
.
nextElement
();
// 遍历当前网络接口的所有地址
for
(
InterfaceAddress
address
:
item
.
getInterfaceAddresses
())
{
// 忽略回环地址和未启用的网络接口
if
(
item
.
isLoopback
()
||
!
item
.
isUp
())
{
continue
;
}
// 如果当前地址是 IPv4 地址,则返回其字符串表示
if
(
address
.
getAddress
()
instanceof
Inet4Address
)
{
Inet4Address
inet4Address
=
(
Inet4Address
)
address
.
getAddress
();
return
inet4Address
.
getHostAddress
();
}
}
}
// 如果没有找到任何 IPv4 地址,则返回本地主机地址
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
SocketException
|
UnknownHostException
e
)
{
// 抛出运行时异常
throw
new
RuntimeException
(
e
);
}
}
```
需要注意的是,这段代码只返回本机的 IPv4 地址,并且只返回第一个符合条件的地址。如果本机有多个网络接口或者每个接口有多个地址,则可能无法返回预期的地址。此外,如果找不到任何 IPv4 地址,则会返回本地主机地址。
再次测试,输出如下
![](
https://cdn.tobebetterjavaer.com/
tobebetterjavaer/images/common-tool/IpUtil-cd2f2acb-a6ea-4675-82a8-95a7e05c8498.jp
g
)
![](
https://cdn.tobebetterjavaer.com/
stutymore/utils-20230330100334.pn
g
)
#### 3. 完整工具类
...
...
@@ -95,34 +96,45 @@ public static String getLocalIpByNetcard() {
import
java.net.*
;
import
java.util.Enumeration
;
public
class
I
p
Util
{
public
class
I
P
Util
{
public
static
final
String
DEFAULT_IP
=
"127.0.0.1"
;
/**
* 直接根据第一个网卡地址作为其内网ipv4地址,避免返回 127.0.0.1
*
* @return
* @return
第一个符合条件的内网 IPv4 地址
*/
public
static
String
getLocalIpByNetcard
()
{
try
{
// 枚举所有的网络接口
for
(
Enumeration
<
NetworkInterface
>
e
=
NetworkInterface
.
getNetworkInterfaces
();
e
.
hasMoreElements
();
)
{
// 获取当前网络接口
NetworkInterface
item
=
e
.
nextElement
();
// 遍历当前网络接口的所有地址
for
(
InterfaceAddress
address
:
item
.
getInterfaceAddresses
())
{
// 忽略回环地址和未启用的网络接口
if
(
item
.
isLoopback
()
||
!
item
.
isUp
())
{
continue
;
}
// 如果当前地址是 IPv4 地址,则返回其字符串表示
if
(
address
.
getAddress
()
instanceof
Inet4Address
)
{
Inet4Address
inet4Address
=
(
Inet4Address
)
address
.
getAddress
();
return
inet4Address
.
getHostAddress
();
}
}
}
// 如果没有找到符合条件的地址,则返回本地主机地址
return
InetAddress
.
getLocalHost
().
getHostAddress
();
}
catch
(
SocketException
|
UnknownHostException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
/**
* 获取本地主机地址
*
* @return 本地主机地址
*/
public
static
String
getLocalIP
()
{
try
{
return
InetAddress
.
getLocalHost
().
getHostAddress
();
...
...
@@ -133,227 +145,13 @@ public class IpUtil {
}
```
### CollectionUtils:Spring 和 Apache 都有提供的集合工具类
对集合操作,除了前面说的
`Collections`
工具类之后,
`CollectionUtils`
工具类也非常常用。
目前比较主流的是
`spring`
的
`org.springframework.util`
包下的 CollectionUtils 工具类。
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-3433117c-4ab2-4ac4-bf5b-4b729d87fc9a.jpg
)
和
`apache`
的
`org.apache.commons.collections`
包下的 CollectionUtils 工具类。
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-1bc7dfe9-f459-47bb-ae4b-2a25d4be96c1.jpg
)
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/CollectionUtils-2b8630a3-141b-4f18-9f54-5a37fc818420.jpg
)
> 我个人更推荐使用 apache 的包下的 CollectionUtils 工具类,因为它的工具更多更全面。
举个简单的例子,
`spring`
的 CollectionUtils 工具类没有判断集合不为空的方法。而
`apache`
的 CollectionUtils 工具类却有。
下面我们以
`apache`
的 CollectionUtils 工具类为例,介绍一下常用方法。
#### 集合判空
通过 CollectionUtils 工具类的
`isEmpty`
方法可以轻松判断集合是否为空,
`isNotEmpty`
方法判断集合不为空。
```
java
List
<
Integer
>
list
=
new
ArrayList
<>();
list
.
add
(
2
);
list
.
add
(
1
);
list
.
add
(
3
);
if
(
CollectionUtils
.
isEmpty
(
list
))
{
System
.
out
.
println
(
"集合为空"
);
}
if
(
CollectionUtils
.
isNotEmpty
(
list
))
{
System
.
out
.
println
(
"集合不为空"
);
}
```
#### 对两个集合进行操作
有时候我们需要对已有的两个集合进行操作,比如取交集或者并集等。
```
java
List
<
Integer
>
list
=
new
ArrayList
<>();
list
.
add
(
2
);
list
.
add
(
1
);
list
.
add
(
3
);
List
<
Integer
>
list2
=
new
ArrayList
<>();
list2
.
add
(
2
);
list2
.
add
(
4
);
//获取并集
Collection
<
Integer
>
unionList
=
CollectionUtils
.
union
(
list
,
list2
);
System
.
out
.
println
(
unionList
);
//获取交集
Collection
<
Integer
>
intersectionList
=
CollectionUtils
.
intersection
(
list
,
list2
);
System
.
out
.
println
(
intersectionList
);
//获取交集的补集
Collection
<
Integer
>
disjunctionList
=
CollectionUtils
.
disjunction
(
list
,
list2
);
System
.
out
.
println
(
disjunctionList
);
//获取差集
Collection
<
Integer
>
subtractList
=
CollectionUtils
.
subtract
(
list
,
list2
);
System
.
out
.
println
(
subtractList
);
```
执行结果:
```
java
[
1
,
2
,
3
,
4
]
[
2
]
[
1
,
3
,
4
]
[
1
,
3
]
```
说句实话,对两个集合的操作,在实际工作中用得挺多的,特别是很多批量的场景中。以前我们需要写一堆代码,但没想到有现成的轮子。
### StringUtils:专为 Java 字符串而生的工具类
`字符串`
(String)在我们的日常工作中,用得非常非常非常多。
在我们的代码中经常需要对字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。
如果只用 String 类提供的那些方法,我们需要手写大量的额外代码,不然容易出现各种异常。
现在有个好消息是:
`org.apache.commons.lang3`
包下的
`StringUtils`
工具类,给我们提供了非常丰富的选择。
#### 字符串判空
其实空字符串,不只是 null 一种,还有""," ","null"等等,多种情况。
StringUtils 给我们提供了多个判空的静态方法,例如:
```
java
String
str1
=
null
;
String
str2
=
""
;
String
str3
=
" "
;
String
str4
=
"abc"
;
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
(
"====="
);
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
(
"====="
);
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
(
"====="
);
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str1
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str2
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str3
));
System
.
out
.
println
(
StringUtils
.
isNotBlank
(
str4
));
```
执行结果:
```
java
true
true
false
false
=====
false
false
true
true
=====
true
true
true
false
=====
false
false
false
true
```
示例中的:
`isEmpty`
、
`isNotEmpty`
、
`isBlank`
和
`isNotBlank`
,这 4 个判空方法你们可以根据实际情况使用。
> 优先推荐使用`isBlank`和`isNotBlank`方法,因为它会把`" "`也考虑进去。
#### 分隔字符串
分隔字符串是常见需求,如果直接使用 String 类的 split 方法,就可能会出现空指针异常。
```
java
String
str1
=
null
;
System
.
out
.
println
(
StringUtils
.
split
(
str1
,
","
));
System
.
out
.
println
(
str1
.
split
(
","
));
```
执行结果:
```
java
null
Exception
in
thread
"main"
java
.
lang
.
NullPointerException
\
tat
com
.
sue
.
jump
.
service
.
test1
.
UtilTest
.
main
(
UtilTest
.
java
:
21
)
```
使用 StringUtils 的 split 方法会返回 null,而使用 String 的 split 方法会报指针异常。
#### 判断是否纯数字
给定一个字符串,判断它是否为纯数字,可以使用
`isNumeric`
方法。例如:
```
java
String
str1
=
"123"
;
String
str2
=
"123q"
;
String
str3
=
"0.33"
;
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str1
));
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str2
));
System
.
out
.
println
(
StringUtils
.
isNumeric
(
str3
));
```
执行结果:
```
java
true
false
false
```
#### 将集合拼接成字符串
有时候,我们需要将某个集合的内容,拼接成一个字符串,然后输出,这时可以使用
`join`
方法。例如:
```
java
List
<
String
>
list
=
Lists
.
newArrayList
(
"a"
,
"b"
,
"c"
);
List
<
Integer
>
list2
=
Lists
.
newArrayList
(
1
,
2
,
3
);
System
.
out
.
println
(
StringUtils
.
join
(
list
,
","
));
System
.
out
.
println
(
StringUtils
.
join
(
list2
,
" "
));
```
执行结果:
```
java
a
,
b
,
c
1
2
3
```
当然还有很多实用的方法,我在这里就不一一介绍了。
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-68f94af9-d2ea-46c2-81b4-7d7e08891550.jpg
)
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-7314260e-4e85-4110-a50d-3bedcbbeb616.jpg
)
IPUtil 类中定义了两个方法,分别是
`getLocalIpByNetcard()`
和
`getLocalIP()`
。前者是获取本机的内网 IPv4 地址,避免了返回 127.0.0.1 的问题。后者是获取本地主机地址,如果本机有多个 IP 地址,则可能返回其中的任意一个。
### MDC:一个线程安全的参数传递工具类
`MDC`
是
`org.slf4j`
包下的一个类,它的全称是 Mapped Diagnostic Context,我们可以认为它是一个线程安全的存放诊断日志的容器。
`MDC`
是
[
`org.slf4j`
](
https://tobebetterjavaer.com/gongju/slf4j.html
)
包下的一个类,它的全称是 Mapped Diagnostic Context,我们可以认为它是一个线程安全的存放诊断日志的容器。
MDC 的底层是用了
`ThreadLocal`
来保存数据的。
MDC 的底层是用了
[
`ThreadLocal`
](
https://tobebetterjavaer.com/thread/ThreadLocal.html
)
来保存数据的。
我们可以用它传递参数。
...
...
@@ -478,13 +276,13 @@ System.out.println(ClassUtils.isInnerClass(User.class));
System
.
out
.
println
(
ClassUtils
.
isCglibProxy
(
new
User
()));
```
ClassUtils 还有很多有用的方法,等待着你去发掘。感兴趣的
朋友,可以看看下面
内容:
ClassUtils 还有很多有用的方法,等待着你去发掘。感兴趣的
小伙伴,可以看看下面的
内容:
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-c58920ac-cf04-4d95-ad29-90339a086569.jpg
)
### BeanUtils
s
pring 给我们提供了一个
`JavaBean`
的工具类,它在
`org.springframework.beans`
包下面,它的名字叫做:
`BeanUtils`
。
S
pring 给我们提供了一个
`JavaBean`
的工具类,它在
`org.springframework.beans`
包下面,它的名字叫做:
`BeanUtils`
。
让我们一起看看这个工具可以带给我们哪些惊喜。
...
...
@@ -495,8 +293,8 @@ spring 给我们提供了一个`JavaBean`的工具类,它在`org.springframewo
```
java
User
user1
=
new
User
();
user1
.
setId
(
1L
);
user1
.
setName
(
"
苏三说技术
"
);
user1
.
setAddress
(
"
成都
"
);
user1
.
setName
(
"
沉默王二
"
);
user1
.
setAddress
(
"
中国
"
);
User
user2
=
new
User
();
BeanUtils
.
copyProperties
(
user1
,
user2
);
...
...
@@ -539,7 +337,7 @@ System.out.println(propertyForMethod.getName());
有时候,我们需要在项目中使用
`反射`
功能,如果使用最原始的方法来开发,代码量会非常多,而且很麻烦,它需要处理一大堆异常以及访问权限等问题。
好消息是
s
pring 给我们提供了一个
`ReflectionUtils`
工具,它在
`org.springframework.util`
包下面。
好消息是
S
pring 给我们提供了一个
`ReflectionUtils`
工具,它在
`org.springframework.util`
包下面。
#### 获取方法
...
...
@@ -587,6 +385,8 @@ System.out.println(ReflectionUtils.isEqualsMethod(method));
![](
https://cdn.tobebetterjavaer.com/tobebetterjavaer/images/common-tool/utils-0a4ecb9c-b9d2-4090-a7b7-c626a0672b94.jpg
)
>参考链接:[https://juejin.cn/post/7102418518599008286](https://juejin.cn/post/7102418518599008286) 作者:苏三,编辑:沉默王二
---
最近整理了一份牛逼的学习资料,包括但不限于 Java 基础部分(JVM、Java 集合框架、多线程),还囊括了
**数据库、计算机网络、算法与数据结构、设计模式、框架类 Spring、Netty、微服务(Dubbo,消息队列) 网关**
等等等等……详情戳:
[
可以说是 2022 年全网最全的学习和找工作的 PDF 资源了
](
https://tobebetterjavaer.com/pdf/programmer-111.html
)
...
...
docs/home.md
浏览文件 @
939608a3
...
...
@@ -208,11 +208,12 @@ head:
-
[
Java Scanner:扫描控制台输入的工具类
](
common-tool/scanner.md
)
-
[
Java Arrays:专为数组而生的工具类
](
common-tool/arrays.md
)
-
[
Apache StringUtils:专为Java字符串而生的工具类
](
common-tool/StringUtils.md
)
-
[
Objects:专为操作Java对象而生的工具类
](
common-tool/Objects.md
)
-
[
Java Collections:专为集合而生的工具类
](
common-tool/collections.md
)
-
[
Hutool:国产良心工具包,让你的Java变得更甜
](
common-tool/hutool.md
)
-
[
Guava:Google开源的Java工具库,太强大了
](
common-tool/guava.md
)
-
[
其他常用Java工具类:IpUtil、
CollectionUtils、StringUtils、
MDC、ClassUtils、BeanUtils、ReflectionUtils
](
common-tool/utils.md
)
-
[
其他常用Java工具类:IpUtil、MDC、ClassUtils、BeanUtils、ReflectionUtils
](
common-tool/utils.md
)
### Java新特性
...
...
二哥的 Java 进阶之路.md
浏览文件 @
939608a3
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录