2

大家相互分享下 Java 中常用的工具类心得

 1 year ago
source link: https://www.v2ex.com/t/918851
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

V2EX  ›  Java

大家相互分享下 Java 中常用的工具类心得

  awesomePower · 15 小时 21 分钟前 · 1859 次点击

不少开发者在开发过程中想要使用一个工具类,常常不确定该实现是否已经存在了,因此在会当场手撸一个,但有些功能其实 jdk 或者依赖的第三方库中已经有了。

这里我分享下我了解的比较好用的工具类方法:

// Objects
// 两个对象是否相等,里面实现了 null 的判断
Objects.equals(obj1, obj2);

// Function
// 等同于 a -> a
Function.identity();
  • commons-lang3
// 整个库都非常有用,这里我举两个例子

// 单词首字母大写
StringUtils.capitalize("xx");

// 取字符串固定长度的子串(从左往右,不担心 null 和整体长度)
StringUtils.left("aa", 2); // right 是从右往左
  • commons-io
// 关于 io 的操作,如果有相关需求,不妨去里面找找

// 下载链接到 File
 FileUtils.copyURLToFile();
  • Spring 的 util: org.springframework.util.*

Spring 基本上做 Web 的项目是必带的,没事可以去它的工具包里翘翘,或许可省下一些功夫。

大家有什么常用的小技巧和小心得可以相互分享下。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK