您好,欢迎访问一九零五行业门户网

【整理分享】8种开发工具,提升工作效率,再也不做加班人!

你还在因为加班熬夜而秃头吗?你还在因为奇葩需求而造轮子吗?那你找对人了!!本文切身感受程序员之痛苦,背后默默吐血整理了一篇文章,希望对大家有所帮助。冲冲冲!!
工欲善其事 必先利其器
身为一个程序员,每天面对的事情就是写代码和吹牛逼了。但是总是感觉自己这两个事情没有达到一个平衡点,总感觉每天写代码的时间太多了,都没有多少让自己吹的时间了。不知道大家有没有这些问题和疑惑呢?
我们已知程序员是最会偷懒的生物!哎!那么问题来了,那怎么摸鱼时间还是这么少呢?难道是我们太菜了吗?不不不,可不要小瞧自己,那会是啥原因嘞?
答案就是,当然是你还没看这篇文章呗,本文切身感受程序员之痛苦,背后默默吐血整理了一篇文章,现在分享给大家,希望对大家有所帮助。
目录整体预览图
json解析工具
http网络请求工具
字符串处理工具
集合处理工具
文件流处理工具
加解密工具
java bean 对象转换工具
缓存和限流工具
开始上手整体预览图本文会从图中分类触发,介绍相关工具包,并简单介绍使用。因为本文篇幅有限,所以只当做是一个引子。具体细节还是都得大家在写代码的时候慢慢体会。
json 解析工具json 解析工具在开发中有多常用相信不用我多说了吧,可以说是程序员天天用到的工具,这也是我将它放到第一个来说的原因,下面我们来一起看一下,概括和使用吧,go! 笔者我用的比较多的是 fastjson ,它是阿里开源的一款进行 json 解析的工具,用法也是相当简单。
1、maven 导入 pom 坐标
<dependency>    <groupid>com.alibaba</groupid>    <artifactid>fastjson</artifactid>    <version>1.2.83</version></dependency>
2、下面看怎么使用
json 字符串与实体对象互相转化// 字符串转对象studen student = json.parseobject({name:小明,age:18}, student.class);// 对象转字符串string str = json.tojsonstring(student);
json 字符串与 jsonobject 互相转化jsonobject只是一种数据结构,可以理解为json格式的数据结构(key-value 结构),可以使用put方法给json对象添加元素。jsonobject可以很方便的转换成字符串,也可以很方便的把其他对象转换成jsonobject对象
// 字符串转jsonobject对象jsonobject jsonobject = jsonobject.parseobject({name:小明,age:18});// jsonobject对象转字符串string str = jsonobject.tojsonstring();
json 字符串转化为 集合类// 定义解析字符串string studentliststr = [{name:小明,age:18},{name:小牛,age:24}];// 解析为 list<student>list<student> studentlist = json.parsearray(studentliststr, student.class);// 定义解析字符串string studentmapstr = {name:小明,age:18};// 解析为 map<string,string>map<string, string> stringstringmap = jsonobject.parseobject(studentmapstr, new typereference(){});
fastjson 就介绍到这里,这里只是介绍了简单的使用,更加详细的用法请参考官方的文档,里面还有更多的用法等着你的哦~~
http 网络请求工具除了 json 工具,作为一个优秀的互联网打工人,不学会网络请求,怎么能够在这个行业占有一席之地呢?http 网络请求工具你值得拥有~~ 根据我的个人意愿,我简单介绍 httpclient的用法,因为我对这个比较熟悉
1、maven 导入 pom 坐标
2、如何使用
get 请求(无参)/*** 无参的 get 请求*/public static void noargsgethttp() throws ioexception {    // 定义 httpclient    closeablehttpclient httpclient = httpclientbuilder.create().build();    // 创建 httpget    httpget httpget = new httpget(http://www.baidu.com);    // 定义返回结果    closeablehttpresponse execute = null;    // 发送执行    execute = httpclient.execute(httpget);    // 获取返回值    httpentity entity = execute.getentity();    system.out.println(响应状态为: + execute.getstatusline());    if (objects.nonnull(entity)) {        system.out.println(响应内容长度为: + entity.getcontentlength());        system.out.println(响应内容为: + entityutils.tostring(entity));    }    // 释放资源    if (httpclient != null) {        httpclient.close();    }    if (execute != null) {        execute.close();    }}
响应状态为:http/1.1 200 ok 响应内容长度为:-1 响应内容为:
get 请求(有参) /*** 有参的 get 请求*/public static void haveargsgethttp() throws ioexception, urisyntaxexception {    // 定义 httpclient    closeablehttpclient httpclient = httpclientbuilder.create().build();    // 创建参数列表    list<namevaluepair> valueparamslist = new arraylist<>();    valueparamslist.add(new basicnamevaluepair(studentid,1));    // 创建对应请求 uri    uri uri = new uribuilder()        .setscheme(http)        .sethost(localhost)        .setpath(/getstudentinfo)        .setparameters(valueparamslist)        .build();    // 根据 uri 创建 httpget    httpget httpget = new httpget(uri);    // 定义返回结果    closeablehttpresponse execute = null;    // 发送执行    execute = httpclient.execute(httpget);    // 获取返回值    httpentity entity = execute.getentity();    system.out.println(响应状态为: + execute.getstatusline());    if (objects.nonnull(entity)) {        system.out.println(响应内容长度为: + entity.getcontentlength());        system.out.println(响应内容为: + entityutils.tostring(entity));    }    // 释放资源    if (httpclient != null) {        httpclient.close();    }    if (execute != null) {        execute.close();    }}
post 请求(有参数)/*** post 有参数*/public static void haveargsposthttp() throws ioexception {    // 获得http客户端(可以理解为:你得先有一个浏览器;注意:实际上httpclient与浏览器是不一样的)    closeablehttpclient httpclient = httpclientbuilder.create().build();    // 创建post请求    httppost httppost = new httppost(http://localhost:12345/posttest);    jsonutil.student student = new jsonutil.student();    student.setname(潘晓婷);    student.setage(18);    string jsonstring = json.tojsonstring(student);    stringentity entity = new stringentity(jsonstring, utf-8);    // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中    httppost.setentity(entity);    httppost.setheader(content-type, application/json;charset=utf8);    // 响应模型    closeablehttpresponse response = httpclient.execute(httppost);    // 从响应模型中获取响应实体    httpentity responseentity = response.getentity();    system.out.println(响应状态为: + response.getstatusline());    if (responseentity != null) {        system.out.println(响应内容长度为: + responseentity.getcontentlength());        system.out.println(响应内容为: + entityutils.tostring(responseentity));    }    // 释放资源    if (httpclient != null) {        httpclient.close();    }    if (response != null) {        response.close();    }}
啊!这样一步一步总结下来好累啊,看到这里的小伙伴们,点一下手里的小赞。嘿嘿~~ 当然我只是简单介绍一下 httpclient 的使用,具体高深的使用方法和配置可以参考其他博主的详细介绍,让我们介绍下一个常用的工具吧。
字符串处理工具字符串使我们开发中最常见的类型,也是我们最常需要操作的类型了。如果不知道字符串的常用工具,那在写代码的时候简直就是场灾难!!!
字符串判空,截取字符串、转换大小写、分隔字符串、比较字符串、去掉多余空格、拼接字符串、使用正则表达式等等。
stringutils 给我们提供了非常丰富的选择。我们着重以本类来介绍使用。
1、导入maven坐标
<dependency>    <groupid>org.apache.commons</groupid>    <artifactid>commons-lang3</artifactid>    <version>3.12.0</version></dependency>
2、常用方法介绍
字符串判空 ⭐⭐⭐(isnotblank 和 isblank)
string str =  ;// 是否不为空boolean res1 = stringutils.isnotblank(str);// 是否为空boolean res2 = stringutils.isblank(str);
(isnotempty 和 isempty)
string str =  ;// 是否不为空boolean res1 = stringutils.isnotempty(str);// 是否为空boolean res2 = stringutils.isempty(str);
优先推荐使用 isblank 和 isnotblank 方法,因为 它会把空字符串也考虑进去。
分隔字符串 —— split —— ⭐⭐⭐string str2 = 1,2,3;string[] split = stringutils.split(str2);system.out.println(arrays.tostring(split));
相较于 string 的 split 方法来说,stringutils 的 split 方法不会有空指针异常
判断是否纯数字 —— isnumeric —— ⭐⭐给定一个字符串,判断它是否为纯数字 可以使用isnumeric方法
string str3 = 1;boolean numeric = stringutils.isnumeric(str3);system.out.println(numeric);
当然,这个工具类除了上面简单的三个方法之外们还有其他很多对于我们来说很使用的方法,但是这里就不一一举例了, 有兴趣的小伙伴们可以看源码统计一下
集合相关处理工具哦吼~~看完了字符串的常用工具,重中之重的集合它来了,如果说没有字符串,我们的程序就无法运行,那么没有集合,我们将会是每天在加班的中度过了,出去后将会自豪的说,老板的车轮胎我也是做了贡献的。
既然集合工具这么重要,那么当然要重点介绍。学会相关工具的使用,真的是能让我们事半功倍的,真的是能让摸鱼时间大大增加的,不信你看看。
collections它是 java 的集合工具包,内部包括了很多我们常用的方法,下图展示了其中一些,方便食用!
排序—— sort—— ⭐⭐⭐在我们日常开发工作中,经常会遇到一些集合排序的需求。sort 就可以很好的帮助我们做好这一点
list<integer> list = new arraylist<>();list.add(2);list.add(1);list.add(3);//升序collections.sort(list);system.out.println(list);//降序collections.reverse(list);system.out.println(list);
结果: [1, 2, 3] [3, 2, 1]
获取最大最小值 —— max / min ——⭐⭐⭐最大值最小值是我们操作集合最常见的方法了吧!!collections 就有现成的方法帮助我们实现
// 最大值最小值list<integer> intlist = new arraylist<>();intlist.add(1);intlist.add(2);intlist.add(3);integer max = collections.max(intlist);integer min = collections.min(intlist);system.out.println(集合元素最大值: + max);system.out.println(集合元素最小值: + min);
结果:集合元素最大值:3 集合元素最小值:1
转换线程安全集合—— synchronizedlist ——⭐⭐⭐在多线程状态下,普通集合会产生并发问题,比如 arraylist 并发添加会产生空值情况,这时我们又不想改动我们之前的集合怎么办? 我们简单的通过collections 的线程安全转化就可以做到了,简简单单一行代码就可以做到!是不是方便的很!
list<integer> synchronizedlist = collections.synchronizedlist(intlist);
当然,collections 还有很多有用和有趣的方法等着我们去探索,只是只是作为了一个抛转引玉的效果,就不过多的赘述了。
collectionutils我最常用的便是 collecionutils,它是 apache 开源的工具包。它的功能可以说是相当强大的,不信你可以往下看看,反正你能想到的集合操作基本上它都有。
集合判空—— isnotempty——⭐⭐⭐我们最常用的集合方法,没有之一,必须掌握它!!
list<string> stringlist = new arraylist<>();boolean notempty = collectionutils.isnotempty(stringlist);system.out.println(集合不是空的吗?+ notempty);
集合不是空的吗?false
交集/并集/补集/差集——⭐⭐⭐在开发中,经常需要将多集合进行交并补等的数学操作,不会还是傻傻的子集写循环处理吧!那样还能有摸鱼的时间吗?下面就是大大提升效率的工具!!!
list<integer> list1 = new arraylist<>();list1.add(1);list1.add(2);list1.add(3);list<integer> list2 = new arraylist<>();list2.add(3);list2.add(4);// 获取并集collection<integer> union = collectionutils.union(list1, list2);system.out.println(union);// 获取交集collection<integer> intersection = collectionutils.intersection(list1, list2);system.out.println(intersection);//获取交集的补集collection<integer> disjunctionlist = collectionutils.disjunction(list1, list2);system.out.println(disjunctionlist);// 获取差集collection<integer> subtract = collectionutils.subtract(list1, list2);system.out.println(subtract);
两集合并集:   [1, 2, 3, 4]
两集合交集: [3]
两集合交集的补集:[1, 2, 4]
两集合差集: [1, 2]
判断两集合是否相等——isequalcollection——⭐⭐⭐// 判断两集合是否相等list<integer> list3 = new arraylist<>();list1.add(3);list1.add(4);list<integer> list4 = new arraylist<>();list2.add(3);list2.add(4);boolean equalcollection = collectionutils.isequalcollection(list3, list4);system.out.println(两集合相等吗?: + equalcollection);
两集合相等吗?:true
lists最后在集合的工具类中再补充一个 google 官方的java包,里面有很多我们想不到的超级方便的小工具,既然是说集合,我们说一下它里面的 lists 类,也是超级好用的!
快速初始化集合——newarraylist——⭐⭐⭐相信大家在开发中,都有过初始化集合的需要吧!那么我们一般都是 新建一个 arraylist 然后一个一个 add 进去,现在告诉大家,不用这么麻烦,一个方法新建带初始化全搞定,真香警告!!
// 快速初始化集合arraylist<integer> integers1 = lists.newarraylist(1, 2, 3);system.out.println(integers1);
[1, 2, 3]
集合分页——partition——⭐⭐⭐有时候我们想将我们的大集合分散成为一些小集合,我们又不想手动操作怎么办?这些痛点肯定已经有人帮助我们想好了!!来来来,介绍一下!!
// 数组分页arraylist<integer> list7 = lists.newarraylist(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);list partition = lists.partition(list7, 5);system.out.println(partition);
[ [1, 2, 3, 4, 5], [6, 7, 8, 9, 10] ]
介绍完了 集合相关的操作类,下面我们也要介绍一下文件流相关操作,这个操作在开发中也是经常用到的。
文件流处理工具相信我们在工作已经厌烦了,经常写** io 流相关的类了吧。经常简单的读取和写入一个文件,我们需要大费周章的去定义一些 inputstream 和 outputstream **等,感觉有一种杀鸡用牛刀的错觉。 介绍一个文件操作工具类,省去大量操作和关闭行为,超级好用,平常我可是经常用的。
将信息写入文件——writebytearraytofile——⭐⭐⭐最最常用的操作,怎么样很简单吧,就只用一行代码,秒杀!
// 将 test 写入 test.txt 文件fileutils.writebytearraytofile(new file(c:\users\test.txt), test.getbytes());
从文件读取信息——readfiletobytearray——⭐⭐⭐知道了怎么往文件里写东西,他的好兄弟读取我们也得知道啊!
// 读取 test.txt 文件byte[] bytes = fileutils.readfiletobytearray(new file(d:\users\test.txt));system.out.println(读取到的文本为: + new string(bytes));
读取到的文本为:test
api 很多,我也不能一一为大家介绍,深入的了解还需要大家去,熟练地运用起来,并且是不是的去看看官方的文档,查漏补缺,相信你也可以见识到很多让你叹为观止的方法。
加解密工具类平常我们经常会遇到比如对用户的密码进行加密 (md5) ,校验接口的签名 (sha256) 加密等等 用到加密的场景虽然不是很多,但是有这样的工具何乐而不为呢?
因为常用的加密方法并不多,这里介绍两个方法给大家,想知道其他更多用法的伙伴们,去自己探索吧
// md5 加密string md5 = digestutils.md2hex(123);system.out.println(加密后的结果: + md5);//  sha(安全哈希算法) 加密string sha256hex = digestutils.sha256hex(123);system.out.println(sha256加密后: + sha256hex);
md5加密后的结果:ef1fedf5d32ead6b7aaf687de4ed1b71
sha256加密后:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e3
java bean 对象转换工具说到这里,我们来说一下对象转化的工具,在开发中有些规范,比如dto、do、vo等等,之间,如果我们需要转换,单纯的我们要一个一个的 set 值,真是一项苦逼的活。
beanutilsjava bean对象的相关转化,我在这里介绍两个 ,一个 是大家都非常熟悉的 beanutils,还有一个就是我平常在开发中经常使用的 mapstruct 和 beanutils 最常用的莫过于对象的的拷贝了 。 不过面对需要深拷贝的对象大家要注意了,这里并不推荐大家使用此工具去实现
student student = new student();student.setname(小明);student.setage(18);student newstudent = new student();beanutils.copyproperties(student,newstudent);system.out.println(newstudent);
student(name=小明, age=18)
mapstruct下面我们重点说一下 mapstruct 这个转化,beanutils 就是一个大老粗,只能同属性映射,或者在属性相同的情况下,允许被映射的对象属性少。
但当遇到被映射的属性数据类型被修改或者被映射的字段名被修改,则会导致映射失败。而 mapstruct 就是一个巧媳妇儿了。
她心思细腻,把我们可能会遇到的情况都给考虑到了(要是我也能找一个这样的媳妇儿该多好,内心笑出了猪声)
1、首先啥都不用想上来我们先把该导的包导进去
<dependency> <groupid>org.mapstruct</groupid> <!-- jdk8以下就使用mapstruct --> <artifactid>mapstruct-jdk8</artifactid> <version>1.2.0.final</version></dependency><dependency> <groupid>org.mapstruct</groupid> <artifactid>mapstruct-processor</artifactid> <version>1.2.0.final</version></dependency>
2、用一下
对象之间字段完全相同第一步:定义好我们的基础类
@data@allargsconstructor@noargsconstructorpublic class student{ private string name; private integer age ;}@data@allargsconstructor@noargsconstructorpublic class teacher{ private string name; private integer age ;}
第二步:接下来定义一个接口,但是注意不需要实现,他就呢能够帮我们转化很神奇的
@mapperpublic interface usercoverttoteacher {    usercoverttoteacher instance = mappers.getmapper(usercoverttoteacher.class);    teacher tocovert(student student);}
最后一步:在代码中调用,聪明的小伙伴看下面代码,一下就明白了,就是这么简单
student student = new student();student.setname(小明);student.setage(18);teacher teacher = usercoverttoteacher.instance.tocovert(student);system.out.println(teacher);
teacher(name=小明, age=18)
对象之间字段存在不相同情况我们介绍了完全两个类字段相同的情况,那么,更加令我们头疼的 有多个字段名字不同但是有对应关系应该怎么搞呢?
我们进阶介绍一下,如何处理这种参数名不同的对应关系 目前假设我们新定义一个微信类,我们的学生要注册到微信上,我们就要将学生对象转化为微信对象
@data@allargsconstructor@noargsconstructorstatic class weixin{ private string username; private integer age ;}
但是我们发现和我们上面的学生类,的名字参数名不同,怎么对应过去的? 答案就是在对方法上配置
@mapper public interface usercoverttoweinxin {    usercoverttoweinxin instance = mappers.getmapper(usercoverttoweinxin.class); // 配置字段映射规则    @mapping(source = name,target = username)    beanutiltest.weixin tocovert(beanutiltest.student student);}
student student = new student();student.setname(小明);student.setage(18);weixin weixin = usercoverttoweinxin.instance.tocovert(student);system.out.println(weixin);
weixin(username=小明, age=18)
这么简单的两个小例子可包含不了 mapstruct这么强大的功能,不管是日期格式化、还是表达式解析、还是深拷贝,都能一一搞定,只是限于本篇文章,无法跟大家说了。想想都很伤心呢! 但是还是那句话,抛砖引玉么!剩下的就交给聪明的小伙伴们了!
缓存和限流器工具最后一小节,我给大家带来我的珍藏,压箱底的东西要拿出来了,大家还不快快点赞收藏,记好,错过了,可就没有下家店了。
我也是在 guava 中发现了很多好用的工具的 首先介绍缓存工具,开发中,我们常用的内存缓存,也就常常是定义一个 map 去存放,但是单纯的 map 只能存和取,确无法做到,缓存过期、缓存淘汰,和相关通知等等复杂操作。
我们有必要学习掌握一种工具,能够 cover 上面所有情况的缓存工具,有需求就有工具类,永远记住。!!!,cache 登场了,还是老规矩,先看看它怎么用吧。
cache定义一个简单定时过期的缓存
cache<string, string> cache = cachebuilder.newbuilder()                .expireafterwrite(10, timeunit.seconds)                .build();// 放入缓存cache.put(小明,活着);thread.sleep(10000);// 从缓存拿取值system.out.println(cache.getifpresent(小明));
null
看到没,结果显而易见,超过了缓存时间,就会自己释放。嘿嘿。
定义一个缓存符合以下限制:
限制访问并发设置初始化容量限制缓存数量上限cache<string, string> cache = cachebuilder.newbuilder()、     // 最大容量,超过按照 lru 淘汰                .maximumsize(100)     // 初始容量                .initialcapacity(10)     // 并发等级 10                .concurrencylevel(10)                .expireafterwrite(10, timeunit.seconds)                .build();
两个小例子,大家看明白了没有,真正的干货,还不赶紧用起来。
除了这个,一个限流器也是常常需要的,我们总不能自记去写一个限流器吧,需要考虑的太多,性能还不行哎!那就用接下来介绍的这个工具
ratelimiter限流器大家在并发场景下经常会遇到,最简单的实现限流就是令牌桶算法,原理很简单,但是具体实现是很复杂的,ratelimiter 帮助我们解决这一点,只需要调用简单的 api 就能实现并发限流
定义限流器,每1秒钟通过 1 个请求
ratelimiter ratelimiter = ratelimiter.create(1,1,timeunit.seconds);
并发两个去获取,看一下结果吧,是不是符合我们的预期
new thread(()->{ system.out.println(线程 1 获取到执行权限了吗? + ratelimiter.tryacquire());}).start();new thread(()->{ system.out.println(线程 2 获取到执行权限了吗? + ratelimiter.tryacquire());}).start();
线程 1 获取到执行权限了吗?true
线程 2 获取到执行权限了吗?false
怎么样,是不是只能有一个通过,简单例子说明问题。 具体用法还得大家在实际开发中具体体会,笔者在这里就不多bb了!!留着时间大家赶快去练习吧。争取成为一个 api 调用高手。
修炼完成经过上面这么多的讲解、案例和对知识的思考,相信大家已经初步掌握了线程池的使用方法和细节,以及对原理运行流程的掌握, 如果你觉得本文对你有一定的启发,引起了你的思考。 点赞、转发、收藏,下次你就能很快的找到我喽!
(学习视频分享:编程基础视频)
以上就是【整理分享】8种开发工具,提升工作效率,再也不做加班人!的详细内容。
其它类似信息

推荐信息