好程序員Java培訓分享Java程序員經常使用的工具類庫,Java世界有不少實用的工具類框架,今天介紹3個使用頻率最高的框架。有不少實用的工具類並無所有列出來,只列出了最基礎的一部分。java
Apache CommonsApache Commons有不少子項目,經常使用的項目以下:程序員
BeanUtilsredis
提供了一系列對java bean的操做,讀取和設置屬性值等。api
@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的互相轉換:
// bean->map
Map<String, String> map = BeanUtils.describe(user);
// map->bean
BeanUtils.populate(user, map);
咱們將對象放在緩存中一般用redis中的hash,以下:
# 設置用戶信息
hset student name test
hset student age 10
這種場景下map和bean的互相轉換的工具類就特別有用。
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);
// 從流中讀取內容,轉爲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");
// 文件複製
FileUtils.copyFile(srcFile, destFile);
Lang
StringUtils 如下斷言測試經過
// isEmpty的實現 cs == null || cs.length() == 0; returntrue
assertEquals(true, StringUtils.isEmpty(""));
assertEquals(true, StringUtils.isBlank(null));
assertEquals(true, StringUtils.isBlank(""));
// 空格
assertEquals(true, StringUtils.isBlank(" "));
// 回車
assertEquals(true, StringUtils.isBlank(" "));
Pair和Triple 當想返回2個或3個值,但這幾個值沒有相關性,沒有必要單獨封裝一個對象,就能夠用到以下數據結構,返回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
集合的建立
// 普通集合的建立List list = Lists.newArrayList();Set set = Sets.newHashSet();// 不可變集合的建立ImmutableList list = ImmutableList.of("a", "b", "c");ImmutableSet set = ImmutableSet.of("a", "b");
不可變集合是線程安全的,而且中途不可改變,由於add等方法是被聲明爲過時,而且會拋出異常。
// 普通集合的建立
List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
// 不可變集合的建立
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個鍵映射一個值
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
查看某段代碼的運行時間
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。