溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java世界常用的工具類庫有哪些

發(fā)布時(shí)間:2021-12-09 13:51:42 來源:億速云 閱讀:138 作者:小新 欄目:編程語言

小編給大家分享一下Java世界常用的工具類庫有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

Apache CommonsApache Commons有很多子項(xiàng)目,常用的項(xiàng)目如下:

Java世界常用的工具類庫有哪些

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<String, String> 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<String> line = IOUtils.readLines(InputStream input, Charset encoding);

FileUtils

對文件操作類的封裝

File file = new File("/show/data.text"); // 按行讀取文件 List<String> lines = FileUtils.readLines(file, "UTF-8"); // 將字符串寫入文件 FileUtils.writeStringToFile(file, "test", "UTF-8"); // 文件復(fù)制 FileUtils.copyFile(srcFile, destFile);

Lang

StringUtils 以下斷言測試通過

// isEmpty的實(shí)現(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個(gè)或3個(gè)值,但這幾個(gè)值沒有相關(guān)性,沒有必要單獨(dú)封裝一個(gè)對象,就可以用到如下數(shù)據(jù)結(jié)構(gòu),返回Pair或Triple對象

Pair<Integer, Integer> pair = new ImmutablePair<>(1, 2); // 1 2 System.out.println(pair.getLeft() + " " + pair.getRight());  Triple<Integer, Integer, Integer> triple = new ImmutableTriple<>(1,2,3); // 1 2 3 System.out.println(triple.getLeft() + " " + triple.getMiddle() + " " + triple.getRight());

Google Guava

集合的創(chuàng)建

// 普通集合的創(chuàng)建Listlist = Lists.newArrayList();Setset =  Sets.newHashSet();// 不可變集合的創(chuàng)建ImmutableListlist = ImmutableList.of("a",  "b", "c");ImmutableSetset = ImmutableSet.of("a", "b");

不可變集合是線程安全的,并且中途不可改變,因?yàn)閍dd等方法是被聲明為過期,并且會拋出異常。

// 普通集合的創(chuàng)建 List<String> list = Lists.newArrayList(); Set<String> set = Sets.newHashSet();  // 不可變集合的創(chuàng)建 ImmutableList<String> list = ImmutableList.of("a", "b", "c"); ImmutableSet<String> set = ImmutableSet.of("a", "b");

各種黑科技集合

// use java Map<String, List<Integer>> map = new HashMap<String, List<Integer>>(); // use guava Multimap<String, Integer> map = ArrayListMultimap.create(); map.put("key1", 1); map.put("key1", 2); // [1, 2] System.out.println(map.get("key1"));

2個(gè)鍵映射一個(gè)值

Table<String, String, Integer> table = HashBasedTable.create(); table.put("a", "a", 1); table.put("a", "b", 2); // 2 System.out.println(table.get("a", "b"));

還有很多其他各種類型的集合,不再介紹。

stop watch

查看某段代碼的運(yùn)行時(shí)間

Stopwatch stopwatch = Stopwatch.createStarted(); // do something long second = stopwatch.elapsed(TimeUnit.SECONDS);

TimeUnit 可以指定時(shí)間精度

Joda  Time

jdk1.8之前,日期操作類常用的只有java.util.Date和java.util.Calendar,但是這2個(gè)類的易用性實(shí)在太差了,SimpleDateFormat不是線程安全的  。這就逼迫用戶去選擇第三方的日期操作類,Joda Time就是其中的佼佼者。

2者的api很相似,如果公司的jdk版本在1.8以上推薦使用jdk1.8新推出的日期類,如果在1.8以下推薦使用Joda Time。

以上是“Java世界常用的工具類庫有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI