Java中的Pair詳細(xì)
前言:
Java中的Pair在開(kāi)發(fā)的過(guò)程中,無(wú)意中發(fā)現(xiàn)項(xiàng)目中有用到Pair,對(duì)于我之前從來(lái)沒(méi)有遇到過(guò)這個(gè)東西,覺(jué)得這個(gè)東西挺有意思,所以就記錄下。
在我們寫(xiě)代碼的時(shí)候,肯定會(huì)遇到要返回兩個(gè)值,但是這兩個(gè)值都有用到,所以我們一般都會(huì)用map集合進(jìn)行key-value封裝,或者寫(xiě)一個(gè)類來(lái)封裝兩個(gè)屬性來(lái)返回,但是這兩種方式雖然實(shí)現(xiàn)起來(lái)簡(jiǎn)單,但是感覺(jué)有點(diǎn)浪費(fèi)類或者不美觀,如果大量的出現(xiàn)這種,就大量創(chuàng)建類或者map集合。為了解決這問(wèn)題,強(qiáng)大的工具類-pair,這個(gè)類是在org.apache.commons.lang3.tuple包下的。
1 Pair用法
我們先來(lái)看看Pair用法:
@Test
public void TestPair() {
Pair<String,String> pair = Pair.of("left","right");
System.out.println("left = " + pair.getLeft());
System.out.println("right = " + pair.getRight());
System.out.println("key = " + pair.getKey());
System.out.println("value = " + pair.getValue());
Pair<String,String> mutablePair = new MutablePair<>("left","right");
System.out.println("-----------------------mutablePair------------------------");
System.out.println("left = " + pair.getLeft());
System.out.println("right = " + pair.getRight());
System.out.println("key = " + pair.getKey());
System.out.println("value = " + pair.getValue());
Pair<String,String> immutablePair = new ImmutablePair<>("left","right");
System.out.println("-----------------------immutablePair------------------------");
System.out.println("left = " + pair.getLeft());
System.out.println("right = " + pair.getRight());
System.out.println("key = " + pair.getKey());
System.out.println("value = " + pair.getValue());
}
上面是比較簡(jiǎn)單的列子,下面我們看下打印的結(jié)果:

上面就是打印的結(jié)果,其中MutablePair,ImmutablePair是pair的子類,這樣子就很方便的使用,不需要另外定義map集合和類來(lái)封裝了。
2 Pair源碼
其實(shí)源碼也是算比較簡(jiǎn)單的,Pair源碼如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.apache.commons.lang3.tuple;
import java.io.Serializable;
import java.util.Objects;
import java.util.Map.Entry;
import org.apache.commons.lang3.builder.CompareToBuilder;
public abstract class Pair<L, R> implements Entry<L, R>, Comparable<Pair<L, R>>, Serializable {
private static final long serialVersionUID = 4954918890077093841L;
public Pair() {
}
// 默認(rèn)用的是子類ImmutablePair,
public static <L, R> Pair<L, R> of(L left, R right) {
return new ImmutablePair(left, right);
}
// 定義了抽象方法,目的子類去實(shí)現(xiàn)
public abstract L getLeft();
// 定義了抽象方法,目的子類去實(shí)現(xiàn)
public abstract R getRight();
// 這里的獲取key其實(shí)就是獲取getLeft()方法的值
public final L getKey() {
return this.getLeft();
}
// 這里的獲取value 其實(shí)就是獲取getRight()方法的值
public R getValue() {
return this.getRight();
}
// 這里就是比較兩個(gè)Pair
public int compareTo(Pair<L, R> other) {
return (new CompareToBuilder()).append(this.getLeft(), other.getLeft()).append(this.getRight(), other.getRight()).toComparison();
}
public boolean equals(Object obj) {
if (obj == this) {
return true;
} else if (!(obj instanceof Entry)) {
return false;
} else {
Entry<?, ?> other = (Entry)obj;
return Objects.equals(this.getKey(), other.getKey()) && Objects.equals(this.getValue(), other.getValue());
}
}
public int hashCode() {
return (this.getKey() == null ? 0 : this.getKey().hashCode()) ^ (this.getValue() == null ? 0 : this.getValue().hashCode());
}
public String toString() {
return "(" + this.getLeft() + ',' + this.getRight() + ')';
}
public String toString(String format) {
return String.format(format, this.getLeft(), this.getRight());
}
}
上面的源碼就是簡(jiǎn)單的定義了我們常規(guī)的方法,getLeft()和getRight()方法留給子類去實(shí)現(xiàn),父類默認(rèn)采用的是ImmutablePair子類,Pair還實(shí)現(xiàn)了Entry<L,R>,可以使用getKey()和getValue() ,其實(shí)它們都是調(diào)用了getLeft()和getRight()方法,繼承了Comparable,可以比較兩個(gè)Pair。繼承了Serializable,可以被序列化。
3 ImmutablePair源碼
我們看看ImmutablePair源碼:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.apache.commons.lang3.tuple;
// 繼承了Pair
public final class ImmutablePair<L, R> extends Pair<L, R> {
private static final ImmutablePair NULL = of((Object)null, (Object)null);
private static final long serialVersionUID = 4954918890077093841L;
// 這里用了final修飾,代表的left值設(shè)值之后是不可變
public final L left;
// 這里用了final修飾,代表的right值設(shè)值之后是不可變
public final R right;
public static <L, R> ImmutablePair<L, R> nullPair() {
return NULL;
}
public static <L, R> ImmutablePair<L, R> of(L left, R right) {
return new ImmutablePair(left, right);
}
public ImmutablePair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return this.left;
}
public R getRight() {
return this.right;
}
// 因?yàn)槭遣豢勺兊闹?,所以如果set值的話直接拋異常
public R setValue(R value) {
throw new UnsupportedOperationException();
}
}
ImmutablePair源碼很簡(jiǎn)答,只是變量加了final修飾,是不可變的,所以在調(diào)用setValue()方法時(shí),就會(huì)拋出異常:UnsupportedOperationException。
4 MutablePair源碼
MutablePair源碼如下:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package org.apache.commons.lang3.tuple;
public class MutablePair<L, R> extends Pair<L, R> {
private static final long serialVersionUID = 4954918890077093841L;
public L left;
public R right;
public static <L, R> MutablePair<L, R> of(L left, R right) {
return new MutablePair(left, right);
}
public MutablePair() {
}
public MutablePair(L left, R right) {
this.left = left;
this.right = right;
}
public L getLeft() {
return this.left;
}
public void setLeft(L left) {
this.left = left;
}
public R getRight() {
return this.right;
}
public void setRight(R right) {
this.right = right;
}
// 這里set value值,會(huì)返回舊value值
public R setValue(R value) {
R result = this.getRight();
this.setRight(value);
return result;
}
}
上面的MutablePair源碼跟ImmutablePair源碼不同之處就是MutablePair可變,ImmutablePair不可變。
5 疑問(wèn)?
如果要求返參不止2個(gè),3個(gè)怎么辦???
沒(méi)問(wèn)題,一樣滿足你,在這個(gè)org.apache.commons.lang3.tuple包中提供了針對(duì)構(gòu)建三個(gè)元素的Triple類,類定義中abstract class Triple<L, M, R>。定義了3個(gè)泛型同樣提供了ImmutableTriple和MutableTriple一對(duì)不可變和可變的實(shí)現(xiàn)類,源碼跟上面的差不多,只是多加了個(gè)變量屬性而已。
那如果4個(gè)范參,5個(gè)范參呢,那不好好意思,你只能通過(guò)定義bean封裝返回,或者map集合返回。
你知道的越多,你不知道的越多!我們下期再見(jiàn)!
到此這篇關(guān)于Java中的Pair詳細(xì)的文章就介紹到這了,更多相關(guān)Java中的Pair內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot打包jar中沒(méi)有主清單屬性問(wèn)題
這篇文章主要介紹了springboot打包jar中沒(méi)有主清單屬性問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
總結(jié)十個(gè)實(shí)用但偏執(zhí)的Java編程技術(shù)
Java是世界上最流行的程序語(yǔ)言,從1995年問(wèn)世以來(lái),Java的生態(tài)系統(tǒng)在一直在蓬勃的發(fā)展著。下面這篇文章主要總結(jié)了十個(gè)實(shí)用但偏執(zhí)的Java編程技術(shù),需要的朋友可以參考借鑒,下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-01-01
Java?8中讀取文件內(nèi)容?Files.lines()方法使用示例
這篇文章主要介紹了Java?8中讀取文件內(nèi)容Files.lines()方法如何使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
話說(shuō)Spring Security權(quán)限管理(源碼詳解)
本篇文章主要介紹了話說(shuō)Spring Security權(quán)限管理(源碼詳解) ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
線程池之newCachedThreadPool可緩存線程池的實(shí)例
這篇文章主要介紹了線程池之newCachedThreadPool可緩存線程池的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
Java數(shù)據(jù)結(jié)構(gòu)之二叉搜索樹(shù)詳解
二叉搜索樹(shù)作為一個(gè)經(jīng)典的數(shù)據(jù)結(jié)構(gòu),具有鏈表的快速插入與刪除的特點(diǎn),同時(shí)查詢效率也很優(yōu)秀,所以應(yīng)用十分廣泛。本文將詳細(xì)講講二叉搜索樹(shù)的原理與實(shí)現(xiàn),需要的可以參考一下2022-06-06

