Java中的Pair詳細
前言:
Java
中的Pair
在開發(fā)的過程中,無意中發(fā)現(xiàn)項目中有用到Pair
,對于我之前從來沒有遇到過這個東西,覺得這個東西挺有意思,所以就記錄下。
在我們寫代碼的時候,肯定會遇到要返回兩個值,但是這兩個值都有用到,所以我們一般都會用map集合進行key-value封裝,或者寫一個類來封裝兩個屬性來返回,但是這兩種方式雖然實現(xiàn)起來簡單,但是感覺有點浪費類或者不美觀,如果大量的出現(xiàn)這種,就大量創(chuàng)建類或者map
集合。為了解決這問題,強大的工具類-pair,這個類是在org.apache.commons.lang3.tuple
包下的。
1 Pair用法
我們先來看看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é)果:
上面就是打印的結(jié)果,其中MutablePair
,ImmutablePair
是pair
的子類,這樣子就很方便的使用,不需要另外定義map集合和類來封裝了。
2 Pair源碼
其實源碼也是算比較簡單的,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() { } // 默認用的是子類ImmutablePair, public static <L, R> Pair<L, R> of(L left, R right) { return new ImmutablePair(left, right); } // 定義了抽象方法,目的子類去實現(xiàn) public abstract L getLeft(); // 定義了抽象方法,目的子類去實現(xiàn) public abstract R getRight(); // 這里的獲取key其實就是獲取getLeft()方法的值 public final L getKey() { return this.getLeft(); } // 這里的獲取value 其實就是獲取getRight()方法的值 public R getValue() { return this.getRight(); } // 這里就是比較兩個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()); } }
上面的源碼就是簡單的定義了我們常規(guī)的方法,getLeft()
和getRight()
方法留給子類去實現(xiàn),父類默認采用的是ImmutablePair子類,Pair還實現(xiàn)了Entry<L,R>,可以使用getKey()
和getValue()
,其實它們都是調(diào)用了getLeft()
和getRight()
方法,繼承了Comparable
,可以比較兩個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; } // 因為是不可變的值,所以如果set值的話直接拋異常 public R setValue(R value) { throw new UnsupportedOperationException(); } }
ImmutablePair
源碼很簡答,只是變量加了final修飾,是不可變的,所以在調(diào)用setValue()
方法時,就會拋出異常: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值,會返回舊value值 public R setValue(R value) { R result = this.getRight(); this.setRight(value); return result; } }
上面的MutablePair
源碼跟ImmutablePair
源碼不同之處就是MutablePair
可變,ImmutablePair
不可變。
5 疑問?
如果要求返參不止2個,3個怎么辦???
沒問題,一樣滿足你,在這個org.apache.commons.lang3.tuple
包中提供了針對構(gòu)建三個元素的Triple類,類定義中abstract class Triple<L, M, R>。
定義了3個泛型同樣提供了ImmutableTriple
和MutableTriple
一對不可變和可變的實現(xiàn)類,源碼跟上面的差不多,只是多加了個變量屬性而已。
那如果4個范參,5個范參呢,那不好好意思,你只能通過定義bean封裝返回,或者map集合返回。
你知道的越多,你不知道的越多!我們下期再見!
到此這篇關(guān)于Java中的Pair詳細的文章就介紹到這了,更多相關(guān)Java中的Pair內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Sa-Token不同模式實現(xiàn)單地登錄?多地登錄?同端互斥登錄
這篇文章主要為大家介紹了Sa-Token不同模式實現(xiàn)單地登錄?多地登錄?同端互斥登錄,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07java冷知識:javac AbstractProcessor詳解
這篇文章主要介紹了java冷知識:javac AbstractProcessor詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11SpringBoot統(tǒng)計接口調(diào)用耗時的三種方式
在實際開發(fā)中,了解項目中接口的響應(yīng)時間是必不可少的事情,SpringBoot 項目支持監(jiān)聽接口的功能也不止一個,接下來我們分別以 AOP、ApplicationListener、Tomcat 三個方面去實現(xiàn)三種不同的監(jiān)聽接口響應(yīng)時間的操作,需要的朋友可以參考下2024-06-06一次Spring無法啟動的問題排查實戰(zhàn)之字節(jié)碼篇
最近學(xué)習(xí)了spring相關(guān)知識,公司項目也用到了spring,下面這篇文章主要給大家介紹了一次Spring無法啟動的問題排查實戰(zhàn)之字節(jié)碼篇的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04Springboot中的@ComponentScan注解使用解析
這篇文章主要介紹了Springboot中的@ComponentScan注解使用解析,@ComponentScan用于類或接口上主要是指定掃描路徑,spring會把指定路徑下帶有指定注解的類注冊到IOC容器中,需要的朋友可以參考下2024-01-01關(guān)于spring循環(huán)依賴問題及解決方案
這篇文章主要介紹了關(guān)于spring循環(huán)依賴問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06