Java開發(fā)利器Commons Lang之元組

標(biāo)準(zhǔn)Java庫(kù)沒(méi)有提供足夠的方法來(lái)操作其核心類,Apache Commons Lang提供了這些額外的方法。

Apache Commons Lang為java提供了大量的幫助工具。lang API,特別是String操作方法、基本數(shù)值方法、對(duì)象反射、并發(fā)、創(chuàng)建和序列化以及System屬性。此外,它還包含對(duì)java.util.Date的基本增強(qiáng),以及一系列專門用于幫助構(gòu)建方法的實(shí)用工具,如hashCode、toString和equals。

-- 來(lái)自官方簡(jiǎn)介http://commons.apache.org/proper/commons-lang/index.html

什么是元組?

元組(tuple)是關(guān)系數(shù)據(jù)庫(kù)中的基本概念,關(guān)系是一張表,表中的每行(即數(shù)據(jù)庫(kù)中的每條記錄)就是一個(gè)元組,每列就是一個(gè)屬性。 在二維表里,元組也稱為行。 -- 摘自百度百科

如果從日常開發(fā)中我們可能經(jīng)常遇到如下情形:我們寫了某個(gè)方法,這個(gè)發(fā)放希望返回最大值和最小值兩個(gè)值,這個(gè)時(shí)候怎么做呢?封裝一個(gè)返回對(duì)象?或者返回一個(gè)json對(duì)象或者list,然后解析?這樣確實(shí)有點(diǎn)難看!

那么好在Commons Lang給我們提供了一個(gè)tuple元組相關(guān)數(shù)據(jù)結(jié)構(gòu)的實(shí)現(xiàn),就可以幫助我們解決這種問(wèn)題。

Commons Lang Tuple

tuple相關(guān)的類位于org.apache.commons.lang3.tuple下,整個(gè)元組從是否可變的維度上分為可變?cè)M和不可變?cè)M;從元組元素?cái)?shù)量的維度上分為二元組和三元組;然后兩兩組和,那么總共就有2x2種元組:可變二元組、不可變二元組、可變?nèi)M、不可變?nèi)M

先來(lái)看下元組整個(gè)類圖:


image.png
image.png

從類圖上我們大致可以看到整個(gè)類之間的關(guān)系,所有二元組的基類是Pair,而Pair是繼承自了Map.Entry;而三元組的基類是Triple。接下來(lái)進(jìn)入實(shí)戰(zhàn)環(huán)節(jié)。

可變二元組 MutablePair

@Test
public void testMutablePair() {
    MutablePair<String, Boolean> mutablePair = MutablePair.of("test1", true);
    System.out.println("左值:" + mutablePair.getLeft() + " 右值:" + mutablePair.getRight());

    mutablePair.setLeft("test2");
    mutablePair.setRight(false);
    System.out.println("左值:" + mutablePair.getLeft() + " 右值:" + mutablePair.getRight());

輸出:
左值:test1 右值:true
左值:test2 右值:false
}

不可變二元組 ImmutablePair

在不可變?cè)M中,存儲(chǔ)值的屬性均被final修飾,因而不可變。

@Test
public void testImmutablePair() {
    ImmutablePair<String, Boolean> immutablePair = ImmutablePair.of("test1", true);
    System.out.println("左值:" + immutablePair.getLeft() + " 右值:" + immutablePair.getRight());
}

輸出:
左值:test1 右值:true

可變?nèi)M

@Test
public void testMutableTriple() {
    MutableTriple<String, Boolean, Integer> mutableTriple = MutableTriple.of("test1", true, 1);
    System.out.println(
        "左值:" + mutableTriple.getLeft() + " 中間值:" + mutableTriple.getMiddle() + " 右值:" + mutableTriple.getRight());

    mutableTriple.setLeft("test2");
    mutableTriple.setMiddle(false);
    mutableTriple.setRight(2);
    System.out.println(
        "左值:" + mutableTriple.getLeft() + " 中間值:" + mutableTriple.getMiddle() + " 右值:" + mutableTriple.getRight());
}

輸出:
左值:test1 中間值:true 右值:1
左值:test2 中間值:false 右值:2

不可變?nèi)M

不可變的原理同二元組,通過(guò)final修飾屬性。

@Test
public void testImmutableTriple() {
    ImmutableTriple<String, Boolean, Integer> immutableTriple = ImmutableTriple.of("test1", true, 1);
    System.out.println(
        "左值:" + immutableTriple.getLeft() + " 中間值:" + immutableTriple.getMiddle() + " 右值:" + immutableTriple.getRight());
}

輸出:
左值:test1 中間值:true 右值:1
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容