Java中的Pair詳細(xì)
前言:
Java中的Pair在開發(fā)的過(guò)程中,無(wú)意中發(fā)現(xiàn)項(xiàng)目中有用到Pair,對(duì)于我之前從來(lái)沒(méi)有遇到過(guò)這個(gè)東西,覺(jué)得這個(gè)東西挺有意思,所以就記錄下。
在我們寫代碼的時(shí)候,肯定會(huì)遇到要返回兩個(gè)值,但是這兩個(gè)值都有用到,所以我們一般都會(huì)用map集合進(jìn)行key-value封裝,或者寫一個(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)槭遣豢勺兊闹担匀绻鹲et值的話直接拋異常
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加密配置文件的SQL賬號(hào)密碼方式
這篇文章主要介紹了SpringBoot加密配置文件的SQL賬號(hào)密碼方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Sa-Token不同模式實(shí)現(xiàn)單地登錄?多地登錄?同端互斥登錄
這篇文章主要為大家介紹了Sa-Token不同模式實(shí)現(xiàn)單地登錄?多地登錄?同端互斥登錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
java冷知識(shí):javac AbstractProcessor詳解
這篇文章主要介紹了java冷知識(shí):javac AbstractProcessor詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot統(tǒng)計(jì)接口調(diào)用耗時(shí)的三種方式
在實(shí)際開發(fā)中,了解項(xiàng)目中接口的響應(yīng)時(shí)間是必不可少的事情,SpringBoot 項(xiàng)目支持監(jiān)聽(tīng)接口的功能也不止一個(gè),接下來(lái)我們分別以 AOP、ApplicationListener、Tomcat 三個(gè)方面去實(shí)現(xiàn)三種不同的監(jiān)聽(tīng)接口響應(yīng)時(shí)間的操作,需要的朋友可以參考下2024-06-06
一次Spring無(wú)法啟動(dòng)的問(wèn)題排查實(shí)戰(zhàn)之字節(jié)碼篇
最近學(xué)習(xí)了spring相關(guān)知識(shí),公司項(xiàng)目也用到了spring,下面這篇文章主要給大家介紹了一次Spring無(wú)法啟動(dòng)的問(wèn)題排查實(shí)戰(zhàn)之字節(jié)碼篇的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-04-04
Springboot中的@ComponentScan注解使用解析
這篇文章主要介紹了Springboot中的@ComponentScan注解使用解析,@ComponentScan用于類或接口上主要是指定掃描路徑,spring會(huì)把指定路徑下帶有指定注解的類注冊(cè)到IOC容器中,需要的朋友可以參考下2024-01-01
關(guān)于spring循環(huán)依賴問(wèn)題及解決方案
這篇文章主要介紹了關(guān)于spring循環(huán)依賴問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06

