java工具类

1、字符串工具 StringUtils

1.1 判空函数

  • 在校验一个String类型的变量是否为空时,通常存在3种情况:(1)null;(2)"";(3)" " (空格字符串);
  • 1.1.1 StringUtils.isEmpty(); 判断情况:(1)null;(2)"";
  • 1.1.2 StringUtils.isBlank(); 判断情况:(1)null;(2)"";(3)" " (空格);
1)StringUtils.isEmpty(String str)
2)StringUtils.isNotEmpty(String str)
3)StringUtils.isBlank(String str)
4)StringUtils.isNotBlank(String str) //单个判断
5)StringUtils.isAnyBlank(CharSequence… css) //多个字符串,有任意一个为null
6)StringUtils.isAnyEmpty(CharSequence… css)
7)StringUtils.isNoneBlank(CharSequence… css) //多个字符串,一个null都没有
8)StringUtils.isNoneEmpty(CharSequence… css)
9)StringUtils.isWhitespace(CharSequence cs) //是否有空格字符串 "  "
/* 1、isEmpty() 与 isBlank()的区别: */
	StringUtils.isEmpty("  ");//false
	StringUtils.isBlank("  ");//true
/* 2、isAnyBlank()和isAnyEmpty()是多维判空,存在一个blank或者empty既true */
//注意这两个区别
	StringUtils.isAnyEmpty("", "bar","foo");   //true
	StringUtils.isAnyEmpty("  ", "bar","foo"); //false
	StringUtils.isAnyBlank("", "bar","foo");  //true
	StringUtils.isAnyBlank("  ", "bar","foo");  //true
/* 3、isWhitespace()判断空白 */
	StringUtils.isWhitespace(null)  //false
	StringUtils.isWhitespace("")   // true
	StringUtils.isWhitespace("  ") // true

1.2 大小写函数

StringUtils.capitalize(String str) //首字母大写,其他不变
StringUtils.uncapitalize(String str) //首字母小写,其他不变
StringUtils.upperCase(String str) //upperCase全部转化为大写
StringUtils.upperCase(String str,Locale locale)
StringUtils.lowerCase(String str) //upperCase全部转化为小写
StringUtils.lowerCase(String str,Locale locale)
StringUtils.swapCase(String str) //swapCase大小写互转
StringUtils.isAllUpperCase(CharSequence cs) //isAllUpperCase是否全部大写
StringUtils.isAllLowerCase(CharSequence cs)//isAllLowerCase是否全部小写

1.3 删除函数

StringUtils.remove(String str, char remove)
StringUtils.remove(String str, String remove)
StringUtils.removeEnd(String str, String remove)
StringUtils.removeEndIgnoreCase(String str, String remove)
StringUtils.removePattern(String source, String regex)
StringUtils.removeStart(String str, String remove)
StringUtils.removeStartIgnoreCase(String str, String remove)
StringUtils.deleteWhitespace(String str)
	//删除字符
	StringUtils.remove("queued", 'u') // "qeed"
	//删除字符串
	StringUtils.remove("queued", "ue")// "qd"
	//删除结尾匹配的字符串     
	StringUtils.removeEnd("www.domain.com", ".com")   // "www.domain"
	//删除结尾匹配的字符串,找都不到返回原字符串
	StringUtils.removeEnd("www.domain.com", "domain") // "www.domain.com"
	//忽略大小写的
	StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") // "www.domain")
	//删除所有空白(好用)
	StringUtils.deleteWhitespace("abc")       // "abc"
	StringUtils.deleteWhitespace("   ab  c  ") // "abc"
	//删除字符
	StringUtils.remove("queued", 'u') // "qeed"
	//删除字符串
	StringUtils.remove("queued", "ue") // "qd"
	//删除结尾匹配的字符串     
	StringUtils.removeEnd("www.domain.com", ".com")   // "www.domain"
	//删除结尾匹配的字符串,找都不到返回原字符串
	StringUtils.removeEnd("www.domain.com", "domain") // "www.domain.com"
	//忽略大小写的
	StringUtils.removeEndIgnoreCase("www.domain.com", ".COM") // "www.domain")
	//删除所有空白(好用)
	StringUtils.deleteWhitespace("abc")        // "abc"
	StringUtils.deleteWhitespace("   ab  c  ") // "abc"

2、对象工具类

2.1 PropertyUtils

1、getProperty: 获取对象属性值
2、setProperty: 设置对象属性值
3、getPropertyDiscriptor: 获取属性描述器
4、isReadable: 检查属性是否可访问
5、copyProperties: 复制属性值,从一个对象到另一个对象
6、getPropertyDiscriptors: 获取所有属性描述器
7、isWriteable: 检查属性是否可写
8、getPropertyType: 获取对象属性类型

2.2 BeanUtils

1. public static void setProperty(Object bean, String name, Object value)
给指定对象bean的指定name属性赋值为指定值value。
//如果指定的属性不存在,则什么也不发生。
​
2.public static String getProperty(Object bean, String name)
获取指定对象bean指定name属性的值。
//如果指定的属性不存在,则会抛出异常。
注意:当属性的类型是数组类型时,获取到的值数组中的第一个值。
​
3.public static void copyProperties(Object dest, Object orig)    
将对象orig的属性值赋值给对象dest对象对应的属性
注意:只有属性名名相同且类型一致的才会赋值成功。
​
4. public static void populate(Object bean, Map<String, ? extends Object>
properties)
将一个Map集合中的数据封装到指定对象bean中
注意:对象bean的属性名和Map集合中键要相同。
5. ConvertUtils.register(Converter converter , ..),
当需要将String数据转换成引用数据类型(自定义数据类型时),需要使用此方法实现转换。

2.2 map

import com.whfc.fmam.entity.bo.FmamMatUnit;
import com.whfc.fmam.entity.bo.FmamUnit;
import com.whfc.fmam.entity.dto.FmamMatUnitDTO;
import com.whfc.fmam.entity.dto.SyncFmamUnitDTO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;
//定义,使用
@Mapper(componentModel = "spring")
public interface UnitMap {
    @Mappings({
            @Mapping(source = "id", target = "unitId"),
            @Mapping(source = "code", target = "unitCode"),
            @Mapping(source = "name", target = "unitName")
    })
    SyncFmamUnitDTO to(FmamUnit unit);
    FmamMatUnit to(FmamMatUnitDTO record);
}
//使用
private UnitMap unitMap ;
unitMap.to(SyncFmamUnitDTO);//FmamUnit 

3、数组工具

3.1 JDK Arrays

asList(将数组转换为一个固定的List对象)
sort(对数组排序)
binarySearch(二分法查找数组中的元素)
equals(比较两个数组是否相等)
fill(对数组中的指定位置填充相同的内容)
copyOf(数组拷贝)
hashCode(计算数组的哈希值)
toString(以特定格式输出数组)

3.2 org.apache.commons.lang.ArrayUtils

1、contains: 是否包含某字符串
2、addAll: 添加所有
3、clone :克隆一个数组
4、isEmpty :是否空数组
5、add: 向数组添加元素
6、subarray: 截取数组
7、indexOf: 查找下标
8、isEquals: 比较数组是否相等
9、toObject: 基础类型数据数组转换为对应的Object数组

4、集合工具

4.1 JDK Collections

(1)sort()排序方法
        函数定义:public static <T extends Comparable<? super T>> void sort(List<T> list) 根据元素的
        自然顺序对指定列表按升序进行排序。
        参数:要排序的列表。
        函数定义: public static <T> void sort(List<T> list,Comparator<? super T> c),根据指定比较器产生的顺序对指定列表进行排序。此列表内的所有元素都必须可使用指定比较器相互比较。
        参数:list-要排序的列表;c-确定列表顺序的比较器。
(2)binarySearch()二分查找方法
        函数定义:public static <T> int binarySearch(List<? extends Comparable<? super T>> list,T key)
        使用二分搜索法搜索指定列表,以获得指定对象,在进行此方法调用前比较要将列表元素按照升序排序,否则结果不确定,此方法会执行O(n)次链接遍历和O(log n)次元素比较。
        参数: list-要搜索的链表,key-要搜索的键。
        函数定义: public static <T> int binarySearch(List<? extends T> list, T key, Comparator<? super T> c) 根据指定的比较器对列表进行升序排序。
        参数:list-要搜索的列表,key-要搜索的键,c-排序列表的比较器。
(3)reverse()反转方法
         函数定义:public static void reverse(List<?> list),反转指定列表中元素的顺序,此方法以线性时间运行。
        参数:list-元素要被反转的列表
(4)shuffle()改组方法
       函数定义:public static void shuffle(List<?> list),使用默认随机源对指定列表进行置换,所有置换发生的可能性都是大致相等的。
        参数:list-要改组的列表
        函数定义:public static void shuffle(List<?> list,Random rnd),使用指定的随机源对指定列表进行置换。
    参数:list-要改组的列表,rnd-用来改组列表的随机源。
(5)swap()交换方法
        函数定义:public static void swap(List<?> list,int i,int j),在指定列表的指定位置处交换元素。
        参数:list-进行元素交换的列表,i-要交换的一个元素的索引,j-要交换的另一个元素的索引。
(6)fill()替换方法
        函数定义:public static <T> void fill(List<? super T> list,T obj),使用指定元素替换指定列表中的所有元素,线性时间运行。
        参数:list-使用指定元素填充的列表,obj-用来填充指定列表的元素。
(7)copy()复制方法
        函数定义:public static <T> void copy(List<? super T> dest,List<? extends T> src),将所有元素从一个列表复制到另一个列表。执行此操作后,目标列表中每个已复制元素的索引将等同于源列表中该元素的索引,目标列表的长度至少必须等于源列表。
        参数:dest-目标列表,src-源列表。
(8)min()最小值法
        函数定义:public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> coll),根据元素的自然顺序返回给定Collection的最小元素,Collection中的所有元素必须实现Comparable接口,此外,collection中的所有元素都必须是可相互比较的。
        参数:coll-将确定其最小元素的collection。
        函数定义:public static <T> T min(Collection<? extends T> coll,Comparator<? super T> comp),根据指定比较器产生的顺序,返回给定collection的最小元素。
        参数:coll-将确定其最小元素的collection,comp-用来确定最小元素的比较器。
(9)max()最大值方法
        函数定义:public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> coll),根据元素的自然顺序,返回给定collection的最大元素。
        参数:coll-将确定其最大元素的collection。
        函数定义:public static <T> T max(Collection<?extends T> coll,Comparator<? super T> comp),根据指定比较器产生的顺序,返回给定collection的最大元素。
        参数:coll-将确定其最大元素的collection,comp-用来确定最大元素的比较器
(10)rotate()轮换方法
        函数定义:public static void rotate(List<?> list,int distance),根据指定的距离轮转指定列表中的元素。
        参数:list-要轮换的列表,distance-列表轮换的距离,可以使0、负数或者大于list.size()的数。
(11)replaceAll()替换所有函数
        函数定义:public static <T> boolean replaceAll(List<T> list,T oldVal,T newVal),使用另一个值替换列表总出现的所有的某一指定值。
        参数:list-在其中进行替换的列表;oldVal-将被替换的原值;newVal-替换oldVald的新值。

4.2 org.apache.commons.collections.CollectionUtils

1、isEmpty: 判断集合是否为空
2、select: 根据条件筛选集合元素
3、transform: 根据指定方法处理集合元素,类似List的map()。
4、filter: 过滤元素,雷瑟List的filter()
5、find: 基本和select一样
6、collect 和transform 差不多一样,但是返回新数组
7、forAllDo: 调用每个元素的指定方法。
8、isEqualCollection: 判断两个集合是否一致

5、org.apache.commons.lang.StringEscapeUtils

支持编码转换html、java、JavaScript、sql、csv
escapeXml unEscapeXml
unescapeHtml4 ( )
escapeHtml4 ( )
escapeXml ( )
unescapeXml ( )
escapeJava ( )
escapeEcmaScript ( )
unescapeJava ( )
escapeJson ( )
escapeXml10 ( )

6、 org.apache.commons.io.FilenameUtils

getExtension ( )
getBaseName ( )
getName ( )
concat ( )
removeExtension ( )
normalize ( )
wildcardMatch ( )
separatorsToUnix ( )
getFullPath ( )
isExtension ( )

7、文件流对象工具类 org.apache.commons.io.FileUtils

删除:
1、deleteQuietly(File file):删除指定文件,从不引发异常
2、deleteDirectory(File directory)**: 递归删除文件,删除其包含文件及子目录文件
3、forceDelete(File file) :强制递归删除,如果是目录:则调用了FileUtils.deleteDirectory(File directory),如果是文件:则调用了file.delete()
4、cleanDirectory(File directory): 清除该目录下的文件及子目录文件而不删除该目录文件夹。
读取:
5、readLocalFile(File file): 读取文件为String。
6、readLines(final File file, final String encoding): 按照行读取文件内容,返回 List
复制:
7、 File(File srcFile, File destFile):复制文件到文件,如果复制后的文件已存在,将替换已存在文件的内容
8、File(File input, OutputStream output): 拷贝文件到字节输出流。
9、FileToDirectory(File srcFile, File destDir):拷贝文件到文件的目录,和原文件名一样
10、FileUtils.copyFileToDirectory(file, new File(destFilePath)): 拷贝文件到文件的目录保持文件的日期。
11、copyDirectory(File srcDir, File destDir): 复制目录文件到目录
写入:
12、writeStringToFile: 将String写入文件,如果文件不存在,则创建。write: 写一个字符串或字符/字节数组到文件里,参数(文件 ,数据 ,编码集 , 追加/覆盖)
其他:
13、listFiles(final File directory, final String[] extensions, final boolean recursive): 列出目录下的所有文件,(目录,过滤器如new String[]{“doc”}doc文件,是否递归)

8、org.apache.commons.io.IOUtils

closeQuietly ( )
toString ( )
copy ( )
toByteArray ( )
write ( )
toInputStream ( )
readLines ( )
copyLarge ( )
lineIterator ( )
readFully ( )

9、org.apache.http.util.EntityUtils

toString ( )
consume ( )
toByteArray ( )
consumeQuietly ( )
getContentCharSet ( )

10、org.apache.commons.codec.digest.DigestUtils

md5Hex ( )
shaHex ( )
sha256Hex ( )
sha1Hex ( )
sha ( )
md5 ( )
sha512Hex ( )
sha1 ( )

11、 org.apache.http.client.utils.URLEncodedUtils

format ( )
parse ( )
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值