|
欢迎大家关注个人公众号:
在开发的过程中,无意中发现项目中有用到Pair,对于我之前从来没有遇到过这个东西,觉得这个东西挺有意思,所以就记录下。
Java中的Pair在开发的过程中,无意中发现项目中有用到Pair,对于我之前从来没有遇到过这个东西,觉得这个东西挺有意思,所以就记录下。
在我们写代码的时候,肯定会遇到要返回两个值,但是这两个值都有用到,所以我们一般都会用map集合进行key-value封装,或者写一个类来封装两个属性来返回,但是这两种方式虽然实现起来简单,但是感觉有点浪费类或者不美观,如果大量的出现这种,就大量创建类或者map集合。为了解决这问题,强大的工具类-pair,这个类是在org.apache.commons.lang3.tuple包下的。
1 Pair用法
我们先来看看Pair用法:
@Test
public void TestPair() {
Pair<String,String> pair = Pair.of(&#34;left&#34;,&#34;right&#34;);
System.out.println(&#34;left = &#34; + pair.getLeft());
System.out.println(&#34;right = &#34; + pair.getRight());
System.out.println(&#34;key = &#34; + pair.getKey());
System.out.println(&#34;value = &#34; + pair.getValue());
Pair<String,String> mutablePair = new MutablePair<>(&#34;left&#34;,&#34;right&#34;);
System.out.println(&#34;-----------------------mutablePair------------------------&#34;);
System.out.println(&#34;left = &#34; + pair.getLeft());
System.out.println(&#34;right = &#34; + pair.getRight());
System.out.println(&#34;key = &#34; + pair.getKey());
System.out.println(&#34;value = &#34; + pair.getValue());
Pair<String,String> immutablePair = new ImmutablePair<>(&#34;left&#34;,&#34;right&#34;);
System.out.println(&#34;-----------------------immutablePair------------------------&#34;);
System.out.println(&#34;left = &#34; + pair.getLeft());
System.out.println(&#34;right = &#34; + pair.getRight());
System.out.println(&#34;key = &#34; + pair.getKey());
System.out.println(&#34;value = &#34; + pair.getValue());
}
上面是比较简单的列子,下面我们看下打印的结果:

上面就是打印的结果,其中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);
}
// 定义了抽象方法,目的子类去实现
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 &#34;(&#34; + this.getLeft() + &#39;,&#39; + this.getRight() + &#39;)&#39;;
}
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源码:
//
// 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源码如下:
//
// 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集合返回。
你知道的越多,你不知道的越多!我们下期再见! |
|