这篇文章主要讲解了“如何理解Java中的Pair”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“如何理解Java中的Pair”吧!
1 Pair用法
2 Pair源码
3 ImmutablePair源码
4 MutablePair源码
5 疑问?
前言:
Java
中的Pair
在开发的过程中,无意中发现项目中有用到Pair
,对于我之前从来没有遇到过这个东西,觉得这个东西挺有意思,所以就记录下。
在我们写代码的时候,肯定会遇到要返回两个值,但是这两个值都有用到,所以我们一般都会用map集合进行key-value封装,或者写一个类来封装两个属性来返回,但是这两种方式虽然实现起来简单,但是感觉有点浪费类或者不美观,如果大量的出现这种,就大量创建类或者map
集合。为了解决这问题,强大的工具类-pair,这个类是在org.apache.commons.lang3.tuple
包下的。
我们先来看看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());
}
上面是比较简单的列子,下面我们看下打印的结果:
上面就是打印的结果,其中MutablePair
,ImmutablePair
是pair
的子类,这样子就很方便的使用,不需要另外定义map集合和类来封装了。
其实源码也是算比较简单的,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 "(" + 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
,可以被序列化。
我们看看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
。
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
不可变。
如果要求返参不止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这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。