偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

Java必會的工具庫,讓你的代碼量減少90%

開發(fā) 后端
工作很多年后,才發(fā)現(xiàn)有很多工具類庫,可以大大簡化代碼量,提升開發(fā)效率,初級開發(fā)者卻不知道。

 [[406781]]

工作很多年后,才發(fā)現(xiàn)有很多工具類庫,可以大大簡化代碼量,提升開發(fā)效率,初級開發(fā)者卻不知道。而這些類庫早就成為了業(yè)界標(biāo)準(zhǔn)類庫,大公司的內(nèi)部也都在使用,如果剛工作的時候就有人告訴我使用這些工具類庫,該多好!

一塊看一下有哪些工具類庫你也用過。

1. Java自帶工具方法

1.1 List集合拼接成以逗號分隔的字符串 

  1. // 如何把list集合拼接成以逗號分隔的字符串 a,b,c    
  2. List<String> list = Arrays.asList("a", "b", "c");   
  3. // 第一種方法,可以用stream流    
  4. String join = list.stream().collect(Collectors.joining(","));    
  5. System.out.println(join); // 輸出 a,b,c    
  6. // 第二種方法,其實String也有join方法可以實現(xiàn)這個功能    
  7. String join = String.join(",", list);    
  8. System.out.println(join); // 輸出 a,b,c   

1.2 比較兩個字符串是否相等,忽略大小寫 

  1. if (strA.equalsIgnoreCase(strB)) {   
  2.   System.out.println("相等");    
  3. }   

1.3 比較兩個對象是否相等

當(dāng)我們用equals比較兩個對象是否相等的時候,還需要對左邊的對象進(jìn)行判空,不然可能會報空指針異常,我們可以用java.util包下Objects封裝好的比較是否相等的方法

  1. Objects.equals(strA, strB);   

源碼是這樣的 

  1. public static boolean equals(Object a, Object b) {    
  2.     return (a == b) || (a != null && a.equals(b));    
  3. }   

1.4 兩個List集合取交集 

  1. List<String> list1 = new ArrayList<>();    
  2. list1.add("a");   
  3. list1.add("b");    
  4. list1.add("c");    
  5. List<String> list2 = new ArrayList<>();    
  6. list2.add("a");    
  7. list2.add("b");    
  8. list2.add("d");    
  9. list1.retainAll(list2);    
  10. System.out.println(list1); // 輸出[a, b]   

2. apache commons工具類庫

apache commons是最強(qiáng)大的,也是使用最廣泛的工具類庫,里面的子庫非常多,下面介紹幾個最常用的

2.1 commons-lang,java.lang的增強(qiáng)版

建議使用commons-lang3,優(yōu)化了一些api,原來的commons-lang已停止更新

Maven依賴是: 

  1. <dependency>    
  2.     <groupId>org.apache.commons</groupId>    
  3.     <artifactId>commons-lang3</artifactId>    
  4.     <version>3.12.0</version>    
  5. </dependency>   

2.1.1 字符串判空

傳參CharSequence類型是String、StringBuilder、StringBuffer的父類,都可以直接下面方法判空,以下是源碼: 

  1. public static boolean isEmpty(final CharSequence cs) {    
  2.     return cs == null || cs.length() == 0;    
  3. }      
  4. public static boolean isNotEmpty(final CharSequence cs) {    
  5.     return !isEmpty(cs);    
  6. }      
  7. // 判空的時候,會去除字符串中的空白字符,比如空格、換行、制表符    
  8. public static boolean isBlank(final CharSequence cs) {    
  9.     final int strLen = length(cs);    
  10.     if (strLen == 0) {    
  11.         return true;    
  12.     }    
  13.     for (int i = 0; i < strLen; i++) {    
  14.         if (!Character.isWhitespace(cs.charAt(i))) {    
  15.             return false;    
  16.         }    
  17.     }    
  18.     return true;    
  19. }     
  20. public static boolean isNotBlank(final CharSequence cs) {    
  21.     return !isBlank(cs);    
  22. }   

2.1.2 首字母轉(zhuǎn)成大寫 

  1. String str = "yideng";    
  2. String capitalize = StringUtils.capitalize(str);    
  3. System.out.println(capitalize); // 輸出Yideng   

2.1.3 重復(fù)拼接字符串 

  1. String str = StringUtils.repeat("ab", 2);    
  2. System.out.println(str); // 輸出abab   

2.1.4 格式化日期

再也不用手寫SimpleDateFormat格式化了 

  1. // Date類型轉(zhuǎn)String類型    
  2. String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");    
  3. System.out.println(date); // 輸出 2021-05-01 01:01:01      
  4. // String類型轉(zhuǎn)Date類型    
  5. Date date = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");    
  6.   // 計算一個小時后的日期    
  7. Date date = DateUtils.addHours(new Date(), 1);   

2.1.5 包裝臨時對象

當(dāng)一個方法需要返回兩個及以上字段時,我們一般會封裝成一個臨時對象返回,現(xiàn)在有了Pair和Triple就不需要了 

  1. // 返回兩個字段   
  2. ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng");    
  3. System.out.println(pair.getLeft() + "," + pair.getRight()); // 輸出 1,yideng    
  4. // 返回三個字段    
  5. ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "yideng", new Date());    
  6. System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight()); // 輸出 1,yideng,Wed Apr 07 23:30:00 CST 2021   

2.2 commons-collections 集合工具類

Maven依賴是: 

  1. <dependency>    
  2.     <groupId>org.apache.commons</groupId>    
  3.     <artifactId>commons-collections4</artifactId>    
  4.     <version>4.4</version>    
  5. </dependency>   

2.2.1 集合判空

封裝了集合判空的方法,以下是源碼: 

  1. public static boolean isEmpty(final Collection<?> coll) {    
  2.     return coll == null || coll.isEmpty();    
  3. }      
  4. public static boolean isNotEmpty(final Collection<?> coll) {    
  5.     return !isEmpty(coll);    
  6. }    
  7. // 兩個集合取交集    
  8. Collection<String> collection = CollectionUtils.retainAll(listA, listB);    
  9. // 兩個集合取并集    
  10. Collection<String> collection = CollectionUtils.union(listA, listB);    
  11. // 兩個集合取差集    
  12. Collection<String> collection = CollectionUtils.subtract(listA, listB); 

2.3 common-beanutils 操作對象

Maven依賴: 

  1. <dependency>   
  2.     <groupId>commons-beanutils</groupId>    
  3.     <artifactId>commons-beanutils</artifactId>    
  4.     <version>1.9.4</version>    
  5. </dependency>    
  6. public class User {    
  7.     private Integer id;    
  8.     private String name;    
  9. }   

設(shè)置對象屬性 

  1. User user = new User();    
  2. BeanUtils.setProperty(user, "id", 1);  
  3. BeanUtils.setProperty(user, "name", "yideng");    
  4. System.out.println(BeanUtils.getProperty(user, "name")); // 輸出 yideng    
  5. System.out.println(user); // 輸出 {"id":1,"name":"yideng"}   

對象和map互轉(zhuǎn) 

  1. // 對象轉(zhuǎn)map    
  2. Map<String, String> map = BeanUtils.describe(user);   
  3. System.out.println(map); // 輸出 {"id":"1","name":"yideng"}    
  4. // map轉(zhuǎn)對象    
  5. User newnewUser = new User();    
  6. BeanUtils.populate(newUser, map);    
  7. System.out.println(newUser); // 輸出 {"id":1,"name":"yideng"}   

2.4 commons-io 文件流處理

Maven依賴: 

  1. <dependency>    
  2.     <groupId>commons-io</groupId>    
  3.     <artifactId>commons-io</artifactId>  
  4.      <version>2.8.0</version>    
  5. </dependency>   

文件處理 

  1. File file = new File("demo1.txt");  
  2. // 讀取文件    
  3. List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());    
  4. // 寫入文件    
  5. FileUtils.writeLines(new File("demo2.txt"), lines);    
  6. // 復(fù)制文件    
  7. FileUtils.copyFile(srcFile, destFile);   

3. Google Guava 工具類庫

Maven依賴: 

  1. <dependency>    
  2.     <groupId>com.google.guava</groupId>    
  3.     <artifactId>guava</artifactId>    
  4.     <version>30.1.1-jre</version>    
  5. </dependency>   

3.1 創(chuàng)建集合 

  1. List<String> list = Lists.newArrayList();    
  2. List<Integer> list = Lists.newArrayList(1, 2, 3);   
  3. // 反轉(zhuǎn)list    
  4. List<Integer> reverse = Lists.reverse(list);   
  5. System.out.println(reverse); // 輸出 [3, 2, 1]   
  6.  // list集合元素太多,可以分成若干個集合,每個集合10個元素    
  7. List<List<Integer>> partition = Lists.partition(list, 10);    
  8. Map<String, String> map = Maps.newHashMap();    
  9. Set<String> set = Sets.newHashSet();   

3.2 黑科技集合

3.2.1 Multimap 一個key可以映射多個value的HashMap 

  1. Multimap<String, Integer> map = ArrayListMultimap.create();  
  2. map.put("key", 1);    
  3. map.put("key", 2);    
  4. Collection<Integer> values = map.get("key");   
  5. System.out.println(map); // 輸出 {"key":[1,2]}    
  6. // 還能返回你以前使用的臃腫的Map    
  7. Map<String, Collection<Integer>> collectionMap = map.asMap();   

多省事,多簡潔,省得你再創(chuàng)建 Map<String, List>

3.2.2 BiMap 一種連value也不能重復(fù)的HashMap 

  1. BiMap<String, String> biMap = HashBiMap.create();    
  2. // 如果value重復(fù),put方法會拋異常,除非用forcePut方法    
  3. biMap.put("key","value");    
  4. System.out.println(biMap); // 輸出 {"key":"value"}    
  5. // 既然value不能重復(fù),何不實現(xiàn)個翻轉(zhuǎn)key/value的方法,已經(jīng)有了    
  6. BiMap<String, String> inverse = biMap.inverse();    
  7. System.out.println(inverse); // 輸出 {"value":"key"}   

這其實是雙向映射,在某些場景還是很實用的。

3.2.3 Table 一種有兩個key的HashMap 

  1. // 一批用戶,同時按年齡和性別分組    
  2. Table<Integer, String, String> table = HashBasedTable.create();    
  3. table.put(18, "男", "yideng");    
  4. table.put(18, "女", "Lily");    
  5. System.out.println(table.get(18, "男")); // 輸出 yideng  
  6. // 這其實是一個二維的Map,可以查看行數(shù)據(jù)    
  7. Map<String, String> row = table.row(18);    
  8. System.out.println(row); // 輸出 {"男":"yideng","女":"Lily"}    
  9. // 查看列數(shù)據(jù)    
  10. Map<Integer, String> column = table.column("男");    
  11. System.out.println(column); // 輸出 {18:"yideng"}   

3.2.4 Multiset 一種用來計數(shù)的Set 

  1. Multiset<String> multiset = HashMultiset.create();    
  2. multiset.add("apple");    
  3. multiset.add("apple");    
  4. multiset.add("orange");    
  5. System.out.println(multiset.count("apple")); // 輸出 2    
  6. // 查看去重的元素    
  7. Set<String> set = multiset.elementSet();    
  8. System.out.println(set); // 輸出 ["orange","apple"]  
  9. // 還能查看沒有去重的元素    
  10. Iterator<String> iterator = multiset.iterator();    
  11. while (iterator.hasNext()) {    
  12.     System.out.println(iterator.next());    
  13. }    
  14. // 還能手動設(shè)置某個元素出現(xiàn)的次數(shù)    
  15. multiset.setCount("apple", 5); 

以上為個人經(jīng)驗,希望能給大家一個參考,如有錯誤或未考慮完全的地方,望不吝賜教。 

 

責(zé)任編輯:龐桂玉 來源: Java后端技術(shù)
相關(guān)推薦

2021-11-02 09:54:51

Java開發(fā)工具

2021-05-26 11:11:01

代碼Java工具庫

2022-03-08 14:02:35

GuavaMapjava

2025-06-27 08:34:19

2025-06-10 08:05:00

JavaScript代碼編程語言

2021-03-28 16:55:11

Python工具鏈代碼

2021-12-16 16:35:46

CSS代碼前端

2025-02-21 08:30:00

JavaScripES代碼

2021-11-22 11:30:37

JavaScript代碼瀏覽器

2015-07-23 10:25:27

android代碼質(zhì)量

2019-11-15 15:50:41

JS代碼React前端

2020-11-06 12:00:43

VSCode插件源代碼編輯器

2011-08-01 15:45:47

垃圾廣告電子郵箱安全

2025-03-18 07:20:00

JavaScript開發(fā)字符串

2018-03-30 10:02:08

代碼規(guī)范維護(hù)工程師

2020-06-01 09:30:25

代碼開發(fā)Kotlin

2025-03-11 08:30:00

Pythonretrying代碼

2025-03-28 01:03:46

高并發(fā)技術(shù)異步

2011-04-13 10:51:58

MATLAB

2025-03-05 07:30:00

CSS代碼JavaScrip
點(diǎn)贊
收藏

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