自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Java世界常用的工具類庫

開發(fā) 后端
Java世界有很多實用的工具類框架,今天介紹3個使用頻率最高的框架。有很多實用的工具類并沒有全部列出來,只列出了最基礎(chǔ)的一部分,感興趣的小伙伴,可以看官方的api進行更深入的學習。

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

[[282919]]

Apache CommonsApache Commons有很多子項目,常用的項目如下:

BeanUtils

提供了一系列對java bean的操作,讀取和設(shè)置屬性值等。

  1. @Data 
  2. public class User { 
  3.     private String username; 
  4.     private String password
  1. User user = new User(); 
  2. BeanUtils.setProperty(user"username""li"); 
  3. BeanUtils.getProperty(user"username"); 

map和bean的互相轉(zhuǎn)換:

  1. // bean->map 
  2. Map<String, String> map = BeanUtils.describe(user); 
  3. // map->bean 
  4. BeanUtils.populate(user, map); 

我們將對象放在緩存中通常用redis中的hash,如下:

  1. # 設(shè)置用戶信息 
  2. hset student name test 
  3. hset student age 10 

這種場景下map和bean的互相轉(zhuǎn)換的工具類就特別有用。

Codec

常見的編碼,解碼方法封裝:

  1. // Base64 
  2. Base64.encodeBase64String(byte[] binaryData) 
  3. Base64.decodeBase64(String base64String) 
  4.  
  5. // MD5 
  6. DigestUtils.md5Hex(String data) 
  7.  
  8. // URL 
  9. URLCodec.decodeUrl(byte[] bytes); 
  10. URLCodec.encodeUrl(BitSet urlsafe, byte[] bytes); 

Collections

交并差等操作:

  1. // 判空 
  2. CollectionUtils.isEmpty(collA); 
  3. // 交集 
  4. CollectionUtils.retainAll(collA, collB); 
  5. // 并集 
  6. CollectionUtils.union(collA, collB); 
  7. // 差集 
  8. CollectionUtils.subtract(collA, collB); 
  9. // 判等 
  10. CollectionUtils.isEqualCollection(collA, collB); 

I/O

IOUtils對IO操作的封裝

  1. // 拷貝流 
  2. IOUtils.copy(InputStream input, OutputStream output); 
  3. // 從流中讀取內(nèi)容,轉(zhuǎn)為list 
  4. List<String> line = IOUtils.readLines(InputStream input, Charset encoding); 

FileUtils

對文件操作類的封裝

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

Lang

StringUtils 以下斷言測試通過

  1. // isEmpty的實現(xiàn) cs == null || cs.length() == 0; return true 
  2. assertEquals(true, StringUtils.isEmpty("")); 
  3.  
  4. assertEquals(true, StringUtils.isBlank(null)); 
  5. assertEquals(true, StringUtils.isBlank("")); 
  6. // 空格 
  7. assertEquals(true, StringUtils.isBlank(" ")); 
  8. // 回車 
  9. assertEquals(true, StringUtils.isBlank("    ")); 

Pair和Triple 當想返回2個或3個值,但這幾個值沒有相關(guān)性,沒有必要單獨封裝一個對象,就可以用到如下數(shù)據(jù)結(jié)構(gòu),返回Pair或Triple對象

  1. Pair<IntegerInteger> pair = new ImmutablePair<>(1, 2); 
  2. // 1 2 
  3. System.out.println(pair.getLeft() + " " + pair.getRight()); 
  4.  
  5. Triple<IntegerIntegerInteger> triple = new ImmutableTriple<>(1,2,3); 
  6. // 1 2 3 
  7. 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等方法是被聲明為過期,并且會拋出異常。

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

各種黑科技集合

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

2個鍵映射一個值

  1. Table<String, String, Integertable = HashBasedTable.create(); 
  2. table.put("a""a", 1); 
  3. table.put("a""b", 2); 
  4. // 2 
  5. System.out.println(table.get("a""b")); 

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

stop watch

查看某段代碼的運行時間

  1. Stopwatch stopwatch = Stopwatch.createStarted(); 
  2. // do something 
  3. 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。

 

責任編輯:武曉燕 來源: Java識堂
相關(guān)推薦

2023-04-10 09:11:27

HutoolJava工具

2009-01-04 11:55:09

Java數(shù)組Java常用工具Java類

2018-05-10 16:02:48

Android程序贈工具

2018-01-30 18:49:16

前端JavascriptCSS

2010-01-25 17:28:18

Android類庫

2020-06-29 07:52:17

Java工具類開發(fā)

2020-06-27 09:01:53

Java包裝類編程語言

2021-08-05 09:55:54

云計算

2022-11-15 15:14:05

2022-05-07 09:02:27

數(shù)據(jù)可視化工具庫

2014-10-21 15:11:29

Android工具類源碼

2010-02-04 16:58:29

C++類庫

2017-07-18 18:06:00

JavaScript框架類庫

2020-07-08 07:56:08

Java工具類包裝類

2011-07-10 00:02:39

PHP

2022-12-05 14:39:33

Javascript工具

2021-09-13 19:28:42

JavaNetty開發(fā)

2023-10-16 08:27:17

java工具類

2012-05-09 10:13:54

2019-02-26 09:55:52

Java開發(fā)工具
點贊
收藏

51CTO技術(shù)棧公眾號