Java常用工具类库

  • 1. java自带工具方法
  • 2. apache commons工具类库
    • 2.1 commons-lang,java.lang的增强版
    • 2.2 commons-collections 集合工具类
    • 2.3 common-beanutils 操作对象
    • 2.4 commons-io 文件流处理
  • 3. Google guava工具类库
  • 4. Hutool工具类库
  • 5. json处理工具

1. java自带工具方法

jdk本身自带很多工具库,比如util包下,rt扩展下的,collectios带s的集合工具类,string自身的substring,isEmpty等。

package com.zrj.tool.utils;import org.junit.Test;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;/*** java自带utils* jdk自带util工具类有很多* Collections 集合工具类* String的substring等 ** Date类型** @author zrj* @date 2021/7/1* @since V1.0**/
public class JavaUtils {/*** List集合测试工具* <p>* List集合转换成以指定字符拼接的字符串*/@Testpublic void ListAppendUtils() {// 如何把list集合拼接成以逗号分隔的字符串 a,b,cList<String> list = Arrays.asList( "a", "b", "c" );// 第一种方法,可以用stream流String join = list.stream().collect( Collectors.joining( "," ) );System.out.println( join ); // 输出 a,b,c// 第二种方法,其实String也有join方法可以实现这个功能String join2 = String.join( ",", list );System.out.println( join2 ); // 输出 a,b,c}/*** 两个List集合取交集*/@Testpublic void ListIntersectionUtils() {List<String> list1 = new ArrayList<>();list1.add( "a" );list1.add( "b" );list1.add( "c" );List<String> list2 = new ArrayList<>();list2.add( "a" );list2.add( "b" );list2.add( "d" );// 取交集list1.retainAll( list2 );System.out.println( list1 ); // 输出[a, b]}/*** 两个List集合合并,会重复*/@Testpublic void ListUnionUtils() {List<String> list1 = new ArrayList<>();list1.add( "a" );list1.add( "b" );list1.add( "c" );List<String> list2 = new ArrayList<>();list2.add( "a" );list2.add( "b" );list2.add( "d" );// 取交集list1.addAll( list2 );System.out.println( list1 ); // 输出[a, b, c, a, b, d]}/*** 比较两个字符串是否相等,忽略大小写*/@Testpublic void compareIgnoreCaseUtils() {String strA = "helloword";String strB = "HELLOWORD";if (strA.equalsIgnoreCase( strB )) {System.out.println( "strA相等strB" );}}/*** 比较两个对象是否相等*/@Testpublic void compareObjectUtils() {Object strA = null;Object strB = new JavaUtils();// 如果strA为null就会报空指针,NullPointerException//if (strA.equals( strB )) {//    System.out.println( "strA 等于 strB" );//}// 这种方式可以避免空指针问题boolean result = Objects.equals( strA, strB );System.out.println( result );}}

2. apache commons工具类库

apache commons是最强大的,也是使用最广泛的工具类库,里面的子库非常多,下面介绍几个最常用的。

2.1 commons-lang,java.lang的增强版

建议使用commons-lang3,优化了一些api,原来的commons-lang已停止更新。Maven依赖是:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.12.0</version>
</dependency>
package com.zrj.tool.utils;import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.junit.Test;import java.text.ParseException;
import java.util.Date;/*** apache commons工具类库* commons-lang,java.lang的增强版* 建议使用commons-lang3,优化了一些api,原来的commons-lang已停止更新** @author zrj* @date 2021/7/1* @since V1.0**/
public class CommonsLangUtils {/*** 传参CharSequence类型是String、StringBuilder、StringBuffer的父类,都可以直接下面方法判空.*/@Testpublic void commonLang3Utils() {}/*** commonStringUtils*/@Testpublic void commonStringUtils() {//首字母转成大写String str = "yyds";String capitalize = StringUtils.capitalize( str );System.out.println( capitalize ); // 输出 Yyds//重复拼接字符串String str1 = StringUtils.repeat( "ab", 6 );System.out.println( str1 ); // 输出abab}/*** 格式化日期*/@Testpublic void DateFormatUtils() throws ParseException {// Date类型转String类型String date = DateFormatUtils.format( new Date(), "yyyy-MM-dd HH:mm:ss" );System.out.println( date ); // 输出 2021-05-01 01:01:01// String类型转Date类型Date date1 = DateUtils.parseDate( "2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss" );System.out.println( date1 );// 计算一个小时后的日期Date date2 = DateUtils.addHours( new Date(), 1 );System.out.println( date2 );}/*** 包装临时对象* 当一个方法需要返回两个及以上字段时,我们一般会封装成一个临时对象返回,现在有了Pair和Triple就不需要了*/@Testpublic void ObjectUtils() {// 返回两个字段ImmutablePair<Integer, String> pair = ImmutablePair.of( 1, "jerry" );System.out.println( pair.getLeft() + "," + pair.getRight() ); // 输出 1,jerry// 返回三个字段ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of( 1, "jerry", new Date() );System.out.println( triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight() );}
}

2.2 commons-collections 集合工具类

Maven依赖是:

<dependency><groupId>org.apache.commons</groupId><artifactId>commons-collections4</artifactId><version>4.4</version>
</dependency>
package com.zrj.tool.utils;import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.ImmutableTriple;
import org.junit.Test;import java.text.ParseException;
import java.util.Collection;
import java.util.Date;/*** apache commons工具类库* commons-collections 集合工具类** @author zrj* @date 2021/7/1* @since V1.0**/
public class CommonsCollectionsUtils {/*** 集合判空*/public static boolean isEmpty(final Collection<?> coll) {return coll == null || coll.isEmpty();}public static boolean isNotEmpty(final Collection<?> coll) {return !isEmpty( coll );}private Collection<String> listA;private Collection<String> listB;// 两个集合取交集Collection<String> collection1 = CollectionUtils.retainAll( listA, listB );// 两个集合取并集Collection<String> collection2 = CollectionUtils.union( listA, listB );// 两个集合取差集Collection<String> collection3 = CollectionUtils.subtract( listA, listB );
}

2.3 common-beanutils 操作对象

Maven依赖是:

<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.9.4</version>
</dependency>
package com.zrj.tool.utils;import com.zrj.tool.entity.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.Test;import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Map;/*** apache commons工具类库* common-beanutils 操作对象** @author zrj* @date 2021/7/1* @since V1.0**/
public class CommonBeanUtils {/*** 设置对象属性*/@Testpublic void beanUtils() throws Exception {User user = new User();BeanUtils.setProperty( user, "id", 1 );BeanUtils.setProperty( user, "name", "jerry" );System.out.println( BeanUtils.getProperty( user, "name" ) ); // 输出 jerrySystem.out.println( user ); // 输出 {"id":1,"name":"jerry"}}/*** 对象和map互转*/@Testpublic void beanMapUtils() throws Exception {User user = new User();BeanUtils.setProperty( user, "id", 1 );BeanUtils.setProperty( user, "name", "jerry" );// 对象转mapMap<String, String> map = BeanUtils.describe( user );System.out.println( map ); // 输出 {"id":"1","name":"jerry"}// map转对象User newUser = new User();BeanUtils.populate( newUser, map );System.out.println( newUser ); // 输出 {"id":1,"name":"jerry"}}}

2.4 commons-io 文件流处理

Maven依赖是:

<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.8.0</version>
</dependency>
package com.zrj.tool.utils;import org.apache.tomcat.util.http.fileupload.FileUtils;
import org.junit.Test;import java.io.File;
import java.nio.charset.Charset;
import java.util.List;/*** apache commons工具类库* commons-io 文件流处理** @author zrj* @date 2021/7/1* @since V1.0**/
public class CommonIOUtils {/*** 设置对象属性*/@Testpublic void IOUtils() throws Exception {//File file = new File( "demo1.txt" ); 读取文件//List<String> lines = FileUtils.readLines( file, Charset.defaultCharset() ); 写入文件//FileUtils.writeLines( new File( "demo2.txt" ), lines ); 复制文件//FileUtils.copyFile( srcFile, destFile );}}

3. Google guava工具类库

Maven依赖是:

<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>30.1.1-jre</version>
</dependency>
package com.zrj.tool.utils;import com.google.common.collect.*;
import org.junit.Test;import java.util.*;/*** Google guava工具类库** @author zrj* @date 2021/7/1* @since V1.0**/
public class GoogleGuavaUtils {/*** 创建集合*/@Testpublic void createCollectionsUtils() {List<String> emptyList = Lists.newArrayList();List<Integer> list = Lists.newArrayList( 1, 2, 3 );System.out.println( list );// 反转listList<Integer> reverse = Lists.reverse( list );System.out.println( reverse ); // 输出 [3, 2, 1]// list集合元素太多,可以分成若干个集合,每个集合10个元素List<List<Integer>> partition = Lists.partition( list, 10 );// 创建mapMap<String, String> map = Maps.newHashMap();Set<String> set = Sets.newHashSet();}/*** 黑科技集合* Multimap 一个key可以映射多个value的HashMap*/@Testpublic void multiMapUtils() {Multimap<String, Integer> map = ArrayListMultimap.create();map.put( "key", 1 );map.put( "key", 2 );System.out.println( map ); // 输出 {"key":[1,2]}Collection<Integer> values = map.get( "key" );values.stream().forEach( integer -> System.out.println( integer ) );// 还能返回你以前使用的臃肿的MapMap<String, Collection<Integer>> collectionMap = map.asMap();System.out.println( collectionMap.toString() );}/*** 黑科技集合*  BiMap 一种连value也不能重复的HashMap*/@Testpublic void BiMapUtils() {BiMap<String, String> biMap = HashBiMap.create();// 如果value重复,put方法会抛异常,除非用forcePut方法biMap.put( "key", "value" );System.out.println( biMap ); // 输出 {"key":"value"}// 既然value不能重复,何不实现个翻转key/value的方法,已经有了BiMap<String, String> inverse = biMap.inverse();System.out.println( inverse ); // 输出 {"value":"key"}}/*** 黑科技集合*   Table 一种有两个key的HashMap*/@Testpublic void tableMapUtils() {// 一批用户,同时按年龄和性别分组Table<Integer, String, String> table = HashBasedTable.create();table.put( 18, "男", "jerry" );table.put( 18, "女", "Lily" );System.out.println( table.get( 18, "男" ) ); // 输出 jerry// 这其实是一个二维的Map,可以查看行数据Map<String, String> row = table.row( 18 );System.out.println( row ); // 输出 {"男":"jerry","女":"Lily"}// 查看列数据Map<Integer, String> column = table.column( "男" );System.out.println( column ); // 输出 {18:"jerry"}}/*** 黑科技集合*   Multiset 一种用来计数的Set*/@Testpublic void setCountUtils() {Multiset<String> multiset = HashMultiset.create();multiset.add( "apple" );multiset.add( "apple" );multiset.add( "orange" );System.out.println( multiset.count( "apple" ) ); // 输出 2// 查看去重的元素Set<String> set = multiset.elementSet();System.out.println( set ); // 输出 ["orange","apple"]// 还能查看没有去重的元素Iterator<String> iterator = multiset.iterator();while (iterator.hasNext()) {System.out.println( iterator.next() );}// 还能手动设置某个元素出现的次数multiset.setCount( "apple", 5 );}
}

4. Hutool工具类库

最喜欢的工具包,糊涂包,难得糊涂。
借作者的话来介绍下hutool。

Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以“甜甜的”。Hutool中的工具方法来自每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;Hutool是项目中“util”包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。

hutool官网地址:https://www.hutool.cn/

hutool文档地址:https://www.hutool.cn/docs/#/

包含组件官网截个图,真的很好用,特干净,极少依赖。

5. json处理工具

  1. org.json

  2. net.sf.json

  3. json-simple

  4. gson:最强大,对象,集合嵌套对象,对象与json之间转换毫无压力。

  5. jackson:功能最全,不是最快的,但是相对稳定,功能齐全。

  6. fastjson:问题最多,反序列化,解析还会有很多问题,只一味追求快。git上看看就呵呵了,issue1.5K!还在用说明你心真大。
    https://github.com/alibaba/fastjson

Java常用工具类库相关推荐

  1. JAVA常用工具类(实用高效)

    JAVA常用工具类(根据GITHUB代码统计) 从Google你能搜索到大量的关于Struts,Spring,Hibernate,iBatis等比较大的框架的资料,但是很少有人去关注一些小的工具包,但 ...

  2. Java常用工具类StringUtils的常用方法

    Java常用工具类StringUtils的常用方法 1.该工具类是用于操作Java.lang.String类的. 2.StringUtils类在操作字符串是安全的,不会报空指针异常,也正因此,在操作字 ...

  3. java常用工具类和Hutool常用的工具类整理

    java常用工具类和Hutool常用的工具类整理 1.java常用工具类 1.1 Scanner类 /*** Scanner 类*/@Testpublic void testScanner() {Sc ...

  4. Java常用工具类JsonUtils

    Java常用工具类JsonUtils 一.项目添加pom文件 <dependency><groupId>com.google.code.gson</groupId> ...

  5. 【转】Java程序员常用工具类库 - 目录

    原文地址:http://rensanning.iteye.com/blog/1553076 有人说当你开始学习Java的时候,你就走上了一条不归路,在Java世界里,包罗万象,从J2SE,J2ME,J ...

  6. Hutool Java常用工具类汇总

    简介 Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以"甜甜的". Hu ...

  7. Java常用工具类之异常、包装类、字符串处理类、集合框架实现类、输入输出流、多线程

    集合.多线程和I/O流等 介绍6种常用工具类: 1.如何应用异常处理程序中的问题?2.如何通过包装器类实现基本数据类型的对象化处理?3.字符串处理类String.StringBuilder是如何进行字 ...

  8. Java 常用工具类整理

    目录 第一部分:常用的16个工具类 第二部分:java开发常用工具类(正则校验) 第一部分:常用的16个工具类 一.org.apache.commons.io.IOUtils 1.closeQuiet ...

  9. Java 常用工具类 Collections 源码分析

    文章出处 文章出自:安卓进阶学习指南 作者:shixinzhang 完稿日期:2017.10.25 Collections 和 Arrays 是 JDK 为我们提供的常用工具类,方便我们操作集合和数组 ...

最新文章

  1. Python——制作深度学习数据集批量重命名图片文件名解决方案
  2. MFC使用OpenCV在文档窗口中显示图像(支持多图片格式)
  3. 【转】R语言 RStudio快捷键
  4. BMC之ipmitool 命令收集
  5. 人体塑造教程+源文件+录象教程
  6. osi七层模型 与Linux的一些常用命令和权限管理 继承上篇
  7. 190404每日一句
  8. 关于php print_r
  9. php html转ubb,php实现转换ubb代码的方法
  10. 分子量-算法竞赛习题3-2:给出一种物质的分子式(不带括号),求分子量。本题中的分子式只包含4种原子,分别为C, H, O, N,原子量分别为12.01, 1.008, 16.00, 14.01。
  11. jmeter做秒杀活动测试
  12. 基于android的个人理财软件 android stu_Android聊天软件开发(基于网易云IM即时通讯)——注册账号(二)...
  13. 数据库范式 1NF, 2NF, 3NF的问题与细解
  14. 用一条SQL 语句 查询出每门课都大于80 分的学生姓名
  15. 数据表中常见的数据类型
  16. 后台多条sql查询,json传前台,前台处理多条sql数据实例
  17. 机器学习之数学基础 一 .导数
  18. 《信号与系统》第一章 信号与系统概述
  19. 移动平均线SMA/EMA/SMMA/LWMA
  20. 数字化营销工具有哪些?数字化营销功能有哪些?

热门文章

  1. 网络设计与集成 实验二 - 设备远程管理、VLAN 配置
  2. ajax访问不到外部的变量,解决ajax方法内部不能给方法外部变量赋值的问题
  3. PMP备考大全:经典题库(敏捷管理第16期)
  4. mac IntelliJ IDEA 快捷键总结
  5. charles的web端教程
  6. 我的世界游戏动态壁纸
  7. 关于Linux下.bin格式文件的安装
  8. 海岸鸿蒙质检质控样浓度一览表,质控样浓度一览表.pdf
  9. 基于layui的自适应模板开发
  10. 卷!中科大软院考研分数“炸穿地心”,均分超380,400+也要考虑调剂