
欢迎大家关注个人公众号:
Java中的Pair工具类-Pairhttps://mp.weixin.qq.com/s/mE0jN6mP-XjMTdQe1X7Qpg
在开发的过程中,无意中发现项目中有用到Pair,对于我之前从来没有遇到过这个东西,觉得这个东西挺有意思,所以就记录下。
在我们写代码的时候,肯定会遇到要返回两个值,但是这两个值都有用到,所以我们一般都会用map集合进行key-value封装,或者写一个类来封装两个属性来返回,但是这两种方式虽然实现起来简单,但是感觉有点浪费类或者不美观,如果大量的出现这种,就大量创建类或者map集合。为了解决这问题,强大的工具类-pair,这个类是在org.apache.commons.lang3.tuple包下的。
1 Pair用法我们先来看看Pair用法:
@Test
public void TestPair() {
Pair 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 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 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源码如下:
// // 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 Pairimplements Entry , Comparable >, Serializable { private static final long serialVersionUID = 4954918890077093841L; public Pair() { } // 默认用的是子类ImmutablePair, public static Pair 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 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
我们看看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 ImmutablePairextends Pair { 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 ImmutablePair nullPair() { return NULL; } public static ImmutablePair 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 MutablePairextends Pair { private static final long serialVersionUID = 4954918890077093841L; public L left; public R right; public static MutablePair 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
那如果4个范参,5个范参呢,那不好好意思,你只能通过定义bean封装返回,或者map集合返回。
你知道的越多,你不知道的越多!我们下期再见!