新聞中心
Java世界有很多實用的工具類框架,今天介紹3個使用頻率最高的框架。有很多實用的工具類并沒有全部列出來,只列出了最基礎(chǔ)的一部分,感興趣的小伙伴,可以看官方的api進(jìn)行更深入的學(xué)習(xí)。

創(chuàng)新互聯(lián)秉承實現(xiàn)全網(wǎng)價值營銷的理念,以專業(yè)定制企業(yè)官網(wǎng),網(wǎng)站設(shè)計制作、成都網(wǎng)站建設(shè),成都微信小程序,網(wǎng)頁設(shè)計制作,成都手機(jī)網(wǎng)站制作,營銷型網(wǎng)站幫助傳統(tǒng)企業(yè)實現(xiàn)“互聯(lián)網(wǎng)+”轉(zhuǎn)型升級專業(yè)定制企業(yè)官網(wǎng),公司注重人才、技術(shù)和管理,匯聚了一批優(yōu)秀的互聯(lián)網(wǎng)技術(shù)人才,對客戶都以感恩的心態(tài)奉獻(xiàn)自己的專業(yè)和所長。
Apache CommonsApache Commons有很多子項目,常用的項目如下:
BeanUtils
提供了一系列對java bean的操作,讀取和設(shè)置屬性值等。
- @Data
- public class User {
- private String username;
- private String password;
- }
- User user = new User();
- BeanUtils.setProperty(user, "username", "li");
- BeanUtils.getProperty(user, "username");
map和bean的互相轉(zhuǎn)換:
- // bean->map
- Map
map = BeanUtils.describe(user); - // map->bean
- BeanUtils.populate(user, map);
我們將對象放在緩存中通常用redis中的hash,如下:
- # 設(shè)置用戶信息
- hset student name test
- hset student age 10
這種場景下map和bean的互相轉(zhuǎn)換的工具類就特別有用。
Codec
常見的編碼,解碼方法封裝:
- // Base64
- Base64.encodeBase64String(byte[] binaryData)
- Base64.decodeBase64(String base64String)
- // MD5
- DigestUtils.md5Hex(String data)
- // URL
- URLCodec.decodeUrl(byte[] bytes);
- URLCodec.encodeUrl(BitSet urlsafe, byte[] bytes);
Collections
交并差等操作:
- // 判空
- CollectionUtils.isEmpty(collA);
- // 交集
- CollectionUtils.retainAll(collA, collB);
- // 并集
- CollectionUtils.union(collA, collB);
- // 差集
- CollectionUtils.subtract(collA, collB);
- // 判等
- CollectionUtils.isEqualCollection(collA, collB);
I/O
IOUtils對IO操作的封裝
- // 拷貝流
- IOUtils.copy(InputStream input, OutputStream output);
- // 從流中讀取內(nèi)容,轉(zhuǎn)為list
- List
line = IOUtils.readLines(InputStream input, Charset encoding);
FileUtils
對文件操作類的封裝
- File file = new File("/show/data.text");
- // 按行讀取文件
- List
lines = FileUtils.readLines(file, "UTF-8"); - // 將字符串寫入文件
- FileUtils.writeStringToFile(file, "test", "UTF-8");
- // 文件復(fù)制
- FileUtils.copyFile(srcFile, destFile);
Lang
StringUtils 以下斷言測試通過
- // isEmpty的實現(xiàn) cs == null || cs.length() == 0; return true
- assertEquals(true, StringUtils.isEmpty(""));
- assertEquals(true, StringUtils.isBlank(null));
- assertEquals(true, StringUtils.isBlank(""));
- // 空格
- assertEquals(true, StringUtils.isBlank(" "));
- // 回車
- assertEquals(true, StringUtils.isBlank(" "));
Pair和Triple 當(dāng)想返回2個或3個值,但這幾個值沒有相關(guān)性,沒有必要單獨封裝一個對象,就可以用到如下數(shù)據(jù)結(jié)構(gòu),返回Pair或Triple對象
- Pair
pair = new ImmutablePair<>(1, 2); - // 1 2
- System.out.println(pair.getLeft() + " " + pair.getRight());
- Triple
triple = new ImmutableTriple<>(1,2,3); - // 1 2 3
- System.out.println(triple.getLeft() + " " + triple.getMiddle() + " " + triple.getRight());
Google Guava
集合的創(chuàng)建
// 普通集合的創(chuàng)建List list = Lists.newArrayList();Set set = Sets.newHashSet();// 不可變集合的創(chuàng)建ImmutableList list = ImmutableList.of("a", "b", "c");ImmutableSet set = ImmutableSet.of("a", "b");
不可變集合是線程安全的,并且中途不可改變,因為add等方法是被聲明為過期,并且會拋出異常。
- // 普通集合的創(chuàng)建
- List
list = Lists.newArrayList(); - Set
set = Sets.newHashSet(); - // 不可變集合的創(chuàng)建
- ImmutableList
list = ImmutableList.of("a", "b", "c"); - ImmutableSet
set = ImmutableSet.of("a", "b");
各種黑科技集合
- // use java
- Map
> map = new HashMap >(); - // use guava
- Multimap
map = ArrayListMultimap.create(); - map.put("key1", 1);
- map.put("key1", 2);
- // [1, 2]
- System.out.println(map.get("key1"));
2個鍵映射一個值
- Table
table = HashBasedTable.create(); - table.put("a", "a", 1);
- table.put("a", "b", 2);
- // 2
- System.out.println(table.get("a", "b"));
還有很多其他各種類型的集合,不再介紹。
stop watch
查看某段代碼的運行時間
- Stopwatch stopwatch = Stopwatch.createStarted();
- // do something
- long second = stopwatch.elapsed(TimeUnit.SECONDS);
TimeUnit 可以指定時間精度
Joda Time
jdk1.8之前,日期操作類常用的只有java.util.Date和java.util.Calendar,但是這2個類的易用性實在太差了,SimpleDateFormat不是線程安全的 。這就逼迫用戶去選擇第三方的日期操作類,Joda Time就是其中的佼佼者。
2者的api很相似,如果公司的jdk版本在1.8以上推薦使用jdk1.8新推出的日期類,如果在1.8以下推薦使用Joda Time。
文章標(biāo)題:Java世界常用的工具類庫
鏈接地址:http://m.5511xx.com/article/cdjesei.html


咨詢
建站咨詢
