前言:
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