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;
public class JavaUtils {
@Test public void ListAppendUtils() { List<String> list = Arrays.asList( "a", "b", "c" );
String join = list.stream().collect( Collectors.joining( "," ) ); System.out.println( join );
String join2 = String.join( ",", list ); System.out.println( join2 ); }
@Test public 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 ); }
@Test public 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 ); }
@Test public void compareIgnoreCaseUtils() { String strA = "helloword"; String strB = "HELLOWORD"; if (strA.equalsIgnoreCase( strB )) { System.out.println( "strA相等strB" ); } }
@Test public void compareObjectUtils() { Object strA = null; Object strB = new JavaUtils();
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;
public class CommonsLangUtils {
@Test public void commonLang3Utils() { }
@Test public void commonStringUtils() { String str = "yyds"; String capitalize = StringUtils.capitalize( str ); System.out.println( capitalize );
String str1 = StringUtils.repeat( "ab", 6 ); System.out.println( str1 ); }
@Test public void DateFormatUtils() throws ParseException { String date = DateFormatUtils.format( new Date(), "yyyy-MM-dd HH:mm:ss" ); System.out.println( 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 ); }
@Test public void ObjectUtils() {
ImmutablePair<Integer, String> pair = ImmutablePair.of( 1, "jerry" ); System.out.println( pair.getLeft() + "," + pair.getRight() ); 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;
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;
public class CommonBeanUtils {
@Test public 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" ) ); System.out.println( user ); }
@Test public void beanMapUtils() throws Exception { User user = new User(); BeanUtils.setProperty( user, "id", 1 ); BeanUtils.setProperty( user, "name", "jerry" ); Map<String, String> map = BeanUtils.describe( user ); System.out.println( map ); User newUser = new User(); BeanUtils.populate( newUser, map ); System.out.println( newUser ); }
}
|
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;
public class CommonIOUtils {
@Test public void IOUtils() throws Exception { 读取文件 写入文件 复制文件 }
}
|
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.*;
public class GoogleGuavaUtils {
@Test public void createCollectionsUtils() { List<String> emptyList = Lists.newArrayList();
List<Integer> list = Lists.newArrayList( 1, 2, 3 ); System.out.println( list );
List<Integer> reverse = Lists.reverse( list ); System.out.println( reverse );
List<List<Integer>> partition = Lists.partition( list, 10 );
Map<String, String> map = Maps.newHashMap(); Set<String> set = Sets.newHashSet(); }
@Test public void multiMapUtils() { Multimap<String, Integer> map = ArrayListMultimap.create(); map.put( "key", 1 ); map.put( "key", 2 ); System.out.println( map );
Collection<Integer> values = map.get( "key" ); values.stream().forEach( integer -> System.out.println( integer ) );
Map<String, Collection<Integer>> collectionMap = map.asMap(); System.out.println( collectionMap.toString() ); }
@Test public void BiMapUtils() { BiMap<String, String> biMap = HashBiMap.create(); biMap.put( "key", "value" ); System.out.println( biMap ); BiMap<String, String> inverse = biMap.inverse(); System.out.println( inverse ); }
@Test public void tableMapUtils() { Table<Integer, String, String> table = HashBasedTable.create(); table.put( 18, "男", "jerry" ); table.put( 18, "女", "Lily" ); System.out.println( table.get( 18, "男" ) ); Map<String, String> row = table.row( 18 ); System.out.println( row ); Map<Integer, String> column = table.column( "男" ); System.out.println( column ); }
@Test public void setCountUtils() { Multiset<String> multiset = HashMultiset.create(); multiset.add( "apple" ); multiset.add( "apple" ); multiset.add( "orange" ); System.out.println( multiset.count( "apple" ) ); Set<String> set = multiset.elementSet(); System.out.println( set ); Iterator<String> iterator = multiset.iterator(); while (iterator.hasNext()) { System.out.println( iterator.next() ); } multiset.setCount( "apple", 5 ); } }
|
最喜欢的工具包,糊涂包,难得糊涂。
借作者的话来介绍下hutool。
Hutool是一个小而全的Java工具类库,通过静态方法封装,降低相关API的学习成本,提高工作效率,使Java拥有函数式语言般的优雅,让Java语言也可以"甜甜的"。
Hutool中的工具方法来自每个用户的精雕细琢,它涵盖了Java开发底层代码中的方方面面,它既是大型项目开发中解决小问题的利器,也是小型项目中的效率担当;
Hutool是项目中"util"包友好的替代,它节省了开发人员对项目中公用类和公用工具方法的封装时间,使开发专注于业务,同时可以最大限度的避免封装不完善带来的bug。
|
hutool官网地址:https://www.hutool.cn/
hutool文档地址:https://www.hutool.cn/docs/#/
包含组件官网截个图,真的很好用,特干净,极少依赖。
5. json处理工具
org.json
net.sf.json
json-simple
gson:最强大,对象,集合嵌套对象,对象与json之间转换毫无压力。
jackson:功能最全,不是最快的,但是相对稳定,功能齐全。
fastjson:问题最多,反序列化,解析还会有很多问题,只一味追求快。git上看看就呵呵了,issue1.5K!还在用说明你心真大。
https://github.com/alibaba/fastjson