前言:
Java
中的Pair
在開發的過程中,無意中發現項目中有用到Pair
,對于我之前從來沒有遇到過這個東西,覺得這個東西挺有意思,所以就記錄下。
在我們寫代碼的時候,肯定會遇到要返回兩個值,但是這兩個值都有用到,所以我們一般都會用map集合進行key-value封裝,或者寫一個類來封裝兩個屬性來返回,但是這兩種方式雖然實現起來簡單,但是感覺有點浪費類或者不美觀,如果大量的出現這種,就大量創建類或者map
集合。為了解決這問題,強大的工具類-pair,這個類是在org.apache.commons.lang3.tuple
包下的。
1 Pair用法
我們先來看看Pair用法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
@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()); } |
上面是比較簡單的列子,下面我們看下打印的結果:
上面就是打印的結果,其中MutablePair
,ImmutablePair
是pair
的子類,這樣子就很方便的使用,不需要另外定義map集合和類來封裝了。
2 Pair源碼
其實源碼也是算比較簡單的,Pair源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
// // 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); } // 定義了抽象方法,目的子類去實現 public abstract L getLeft(); // 定義了抽象方法,目的子類去實現 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()); } } |
上面的源碼就是簡單的定義了我們常規的方法,getLeft()
和getRight()
方法留給子類去實現,父類默認采用的是ImmutablePair子類,Pair還實現了Entry<L,R>,可以使用getKey()
和getValue()
,其實它們都是調用了getLeft()
和getRight()
方法,繼承了Comparable
,可以比較兩個Pair。繼承了Serializable
,可以被序列化。
3 ImmutablePair源碼
我們看看ImmutablePair源碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
// // 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值設值之后是不可變 public final L left; // 這里用了final修飾,代表的right值設值之后是不可變 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修飾,是不可變的,所以在調用setValue()
方法時,就會拋出異常:UnsupportedOperationException
。
4 MutablePair源碼
MutablePair源碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
// // 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
包中提供了針對構建三個元素的Triple類,類定義中abstract class Triple<L, M, R>。
定義了3個泛型同樣提供了ImmutableTriple
和MutableTriple
一對不可變和可變的實現類,源碼跟上面的差不多,只是多加了個變量屬性而已。
那如果4個范參,5個范參呢,那不好好意思,你只能通過定義bean封裝返回,或者map集合返回。
你知道的越多,你不知道的越多!我們下期再見!
到此這篇關于Java中的Pair詳細的文章就介紹到這了,更多相關Java中的Pair內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://zhuanlan.zhihu.com/p/417151337