工作多年,发现有很多工具库可以大大简化代码体积,提高开发效率,但是初级开发者不知道。而这些类库早已成为行业标准类库,也被大公司内部使用。如果有人在我刚刚工作的时候告诉我使用这些工具类库就好了!看看你用过哪些工具库。1.Java自带的工具和方法1.1List集合拼接成逗号分隔的字符串//如何将list集合拼接成逗号分隔的字符串a,b,cListlist=Arrays.asList("a","b","c");//第一种方法,可以使用流Stringjoin=list.stream().collect(Collectors.joining(","));System.out.println(加入);//输出a,b,c//第二种方法,其实String也有join方法来实现这个功能Stringjoin=String.join(",",list);System.out.println(加入);//输出a,b,c1.2比较两个字符串是否相等,忽略大小写if(strA.equalsIgnoreCase(strB)){System.out.println("equal");}1.3比较两个对象是否相等当我们使用equals比较两个对象是否相等时,还需要判断左边的对象是否为空,否则可能会报空指针异常。我们可以使用方法Objects.equals(strA,strB);源代码是这样的publicstaticbooleanequals(Objecta,Objectb){return(a==b)||(a!=null&&a.equals(b));}1.4取两个List集合IntersectionListlist1=newArrayList<>();list1.add("a");list1.add("b");list1.add("c");Listlist2=newArrayList<>();list2.add("a");list2.add("b");list2.add("d");list1.retainAll(list2);系统输出文件国际(清单1);//Output[a,b]2.Apachecommons工具库Apachecommons是功能最强大、使用最广泛的工具库,里面有很多子库。下面是一些最常用的2.1commons-lang和java.lang的增强版推荐使用commons-lang3,优化了一些API。原commons-lang已经停止更新Maven依赖:org.apache.commonscommons-lang33.12.02.1.1字符串判断空参数CharSequence类型是String、StringBuilder、StringBuffer的父类,可以直接使用下面的方法下面是源码:publicstaticbooleanisEmpty(finalCharSequencecs){returncs==null||cs.length()==0;}publicstaticbooleanisNotEmpty(finalCharSequencecs){return!isEmpty(cs);}//当为空时,会去除字符串中的空格、换行、制表符等空白字符publicstaticbooleanisBlank(finalCharSequencecs){finalintstrLen=length(cs);如果(strLen==0){返回真;}for(inti=0;ipair=ImmutablePair.of(1,"yideng");System.out.println(pair.getLeft()+","+pair.getRight());//output1,yideng//返回三个字段ImmutableTripletriple=ImmutableTriple.of(1,"yideng",newDate());System.out.println(triple.getLeft()+","+triple.getMiddle()+","+triple.getRight());//Output1,yideng,WedApr0723:30:00CST20212.2commons-collections采集工具的Maven依赖为:org.apache.commonscommons-collections44.42.2.1集合空检测封装了集合空检测的方法,下面是源码:publicstaticbooleanisEmpty(finalCollection>coll){return科尔==空||col.isEmpty();}publicstaticbooleanisNotEmpty(finalCollection>coll){return!isEmpty(coll);}//两个集合的交集Collectioncollection=CollectionUtils.retainAll(listA,listB);//两个集合的并集Collectioncollection=CollectionUtils.union(listA,listB);//两个集合的区别Collectioncollection=CollectionUtils.subtract(listA,清单B);2.3common-beanutils运行对象Maven依赖:commons-beanutilscommons-beanutils1.9.4publicclassUser{私有整数id;私有字符串名称;}设置对象属性Useruser=newUser();BeanUtils.setProperty(用户,"id",1);BeanUtils.setProperty(user,"name","yideng");System.out.println(BeanUtils.getProperty(user,"name"));//输出一灯System.out.println(user);//输出{"id":1,"name":"yideng"}对象与map相互转换//对象转换mapMapmap=BeanUtils.describe(user);System.out.println(地图);//输出{"id":"1","name":"yideng"}//映射到对象UsernewUser=newUser();BeanUtils.populate(newUser,地图);System.out.println(newUser);//output{"id":1,"name":"yideng"}2.4commons-io文件流处理Maven依赖:commons-iocommons-io2.8.0文件处理Filefile=newFile("demo1.txt");//读取文件Listlines=FileUtils.readLines(file,Charset.defaultCharset());//写入文件FileUtils.writeLines(newFile("demo2.txt"),lines);//复制文件FileUtils.copyFile(srcFile,destFile);3.GoogleGuava工具类库Maven依赖:com.google.guavaguava30.1.1-jre3.1创建一个集合Listlist=Lists.newArrayList();Listlist=Lists.newArrayList(1,2,3);//反向列表Listreverse=Lists.reverse(list);System.out.println(反转);//输出[3,2,1]//list集合中元素过多,可以分成几个集合,每个集合有10个元素List>partition=Lists.partition(list,10);Mapmap=Maps.newHashMap();Setset=Sets.newHashSet();3.2黑科技合集3.2.1MultimapHashMapMultimapmap=ArrayListMultimap.create();map.put("键",1);map.put("键",2);Collectionvalues=map.get("key");System.out.println(地图);//output{"key":[1,2]}//返回你之前使用的臃肿的MapMap>collectionMap=map.asMap();省事简单,省去创建Map3.2.2BiMap一种不可重复的值HashMapBiMapbiMap=HashBiMap.create();//如果value重复,put方法会抛出异常,除非使用forcePut方法biMap.put("key","value");System.out.println(biMap);//Output{"key":"value"}//既然value不能重复,为什么不实现一个翻转key/value的方法,已经BiMapinverse=biMap.inverse();系统.out.println(逆);//Output{"value":"key"}这个其实是一个双向映射,在某些场景下还是很实用的3.2.3TableAkindofHashMapwithtwokeys//一组用户,同时按年龄和性别分组Tabletable=HashBasedTable.create();table.put(18,"男","一灯");table.put(18,"女","百合");System.out.println(table.get(18,"男"));//输出yideng//这其实是一个二维的Map,可以查看行数据Maprow=table.row(18);System.out.println(行);//输出{"male":"yideng","female":"Lily"}//查看列数据Mapcolumn=table.column("male");System.out.println(列);//output{18:"yideng"}3.2.4使用MultisetSetMultisetcountmultiset=HashMultiset.create();multiset.add("苹果");multiset.add("苹果");multiset.add("橙色");System.out.println(multiset.count("苹果"));//输出2//查看去重元素Setset=multiset.elementSet();System.out.println(set);//输出["orange","apple"]//也可以查看没有去重的元素Iteratoriterator=multiset.iterator();while(iterator.hasNext()){System.out.println(iterator.next());}//你也可以手动设置一个元素出现次数multiset.setCount("apple",5);以上是个人经验,希望可以给大家一个参考,如有错误或考虑不周之处,请告知作者:一灯建筑来源:www.toutiao.com/i6943239541448917512近期热点文章推荐:1.1000+Java面试题及答案(2021最新版)2.终于从开源项目拿到IntelliJIDEA激活码,真香!3、阿里Mock工具正式开源,秒杀市面上所有Mock工具!4、SpringCloud2020.0.0正式发布,全新颠覆版本!5.《Java开发手册(嵩山版)》最新发布,赶快下载吧!感觉不错,别忘了点赞+转发!