commons-lang3的tuple包介绍

问题

有时候我们调用方法的时候返回值有可能不止1个,比如说我们返回name = "张三" age = 20这两个值,那么这个时候方法的返回值怎么写呢。在Scala和python中都有Tuple使用,在java中我们怎么办。很容易我们就想到了一个方式:可以构造一个User类来封装这两个属性。
但是如果返回的两个值并没有任何关联关系,或者说每一个方法返回的参数都不同,那么我们就得为每一个方法的返回类型去创建对应的类来取包装,或许还有其他的解决方式,比如说返回一个map,返回一个List,返回一个array都可以。
使用map作为返回值的话调用方在不清楚map中具体有什么内容的时候需要去遍历keySet或entrySet,而list和array也是同样的问题,不知道哪一个参数存放在哪里。

解决方式

其实前人已经为我们解决了这个问题。
如果你经常使用apache的commons-lang3包来处理String的null判断,那么你依旧可以从这个包中找到我们想要的一个所谓的元组
org.apache.commons.lang3.tuple.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

/**
* <p>A pair consisting of two elements.</p>
*
* <p>This class is an abstract implementation defining the basic API.
* It refers to the elements as 'left' and 'right'. It also implements the
* {@code Map.Entry} interface where the key is 'left' and the value is 'right'.</p>
*
* <p>Subclass implementations may be mutable or immutable.
* However, there is no restriction on the type of the stored objects that may be stored.
* If mutable objects are stored in the pair, then the pair itself effectively becomes mutable.</p>
*
* @param <L> the left element type
* @param <R> the right element type
*
* @since Lang 3.0
*/
public abstract class Pair<L, R> implements Map.Entry<L, R>, Comparable<Pair<L, R>>, Serializable {

/** Serialization version */
private static final long serialVersionUID = 4954918890077093841L;

/**
* <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
*
* <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p>
*
* @param <L> the left element type
* @param <R> the right element type
* @param left the left element, may be null
* @param right the right element, may be null
* @return a pair formed from the two parameters, not null
*/
public static <L, R> Pair<L, R> of(final L left, final R right) {
return new ImmutablePair<>(left, right);
}

//-----------------------------------------------------------------------
/**
* <p>Gets the left element from this pair.</p>
*
* <p>When treated as a key-value pair, this is the key.</p>
*
* @return the left element, may be null
*/
public abstract L getLeft();

/**
* <p>Gets the right element from this pair.</p>
*
* <p>When treated as a key-value pair, this is the value.</p>
*
* @return the right element, may be null
*/
public abstract R getRight();

/**
* <p>Gets the key from this pair.</p>
*
* <p>This method implements the {@code Map.Entry} interface returning the
* left element as the key.</p>
*
* @return the left element as the key, may be null
*/
@Override
public final L getKey() {
return getLeft();
}

/**
* <p>Gets the value from this pair.</p>
*
* <p>This method implements the {@code Map.Entry} interface returning the
* right element as the value.</p>
*
* @return the right element as the value, may be null
*/
@Override
public R getValue() {
return getRight();
}

//-----------------------------------------------------------------------
/**
* <p>Compares the pair based on the left element followed by the right element.
* The types must be {@code Comparable}.</p>
*
* @param other the other pair, not null
* @return negative if this is less, zero if equal, positive if greater
*/
@Override
public int compareTo(final Pair<L, R> other) {
return new CompareToBuilder().append(getLeft(), other.getLeft())
.append(getRight(), other.getRight()).toComparison();
}

/**
* <p>Compares this pair to another based on the two elements.</p>
*
* @param obj the object to compare to, null returns false
* @return true if the elements of the pair are equal
*/
@Override
public boolean equals(final Object obj) {
if (obj == this) {
return true;
}
if (obj instanceof Map.Entry<?, ?>) {
final Map.Entry<?, ?> other = (Map.Entry<?, ?>) obj;
return Objects.equals(getKey(), other.getKey())
&& Objects.equals(getValue(), other.getValue());
}
return false;
}

/**
* <p>Returns a suitable hash code.
* The hash code follows the definition in {@code Map.Entry}.</p>
*
* @return the hash code
*/
@Override
public int hashCode() {
// see Map.Entry API specification
return (getKey() == null ? 0 : getKey().hashCode()) ^
(getValue() == null ? 0 : getValue().hashCode());
}

/**
* <p>Returns a String representation of this pair using the format {@code ($left,$right)}.</p>
*
* @return a string describing this object, not null
*/
@Override
public String toString() {
return new StringBuilder().append('(').append(getLeft()).append(',').append(getRight()).append(')').toString();
}

/**
* <p>Formats the receiver using the given format.</p>
*
* <p>This uses {@link java.util.Formattable} to perform the formatting. Two variables may
* be used to embed the left and right elements. Use {@code %1$s} for the left
* element (key) and {@code %2$s} for the right element (value).
* The default format used by {@code toString()} is {@code (%1$s,%2$s)}.</p>
*
* @param format the format string, optionally containing {@code %1$s} and {@code %2$s}, not null
* @return the formatted string, not null
*/
public String toString(final String format) {
return String.format(format, getLeft(), getRight());
}

}

这个类构建了一个由两个元素组成的一个元组leftright两个元素。
继承了Map.Entry,可以使用getKey()getValue()方法,从源码中我们也发现其实是调用了他本身的getLeft()getRight()方法,而这两个方法是需要子类去实现,我们后面说。
继承了Comparable,可以比较两个Pair。
继承了Serializable,可以被序列化。
比较重要的就是getKey()getValue(),下面我们看这个类的两个实现ImmutablePairMutablePair

ImmutablePair很好理解,不可变的Pair,相对的MutablePair就是可变的Pair

先看ImmutablePair.class

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

/**
* <p>An immutable pair consisting of two {@code Object} elements.</p>
*
* <p>Although the implementation is immutable, there is no restriction on the objects
* that may be stored. If mutable objects are stored in the pair, then the pair
* itself effectively becomes mutable. The class is also {@code final}, so a subclass
* can not add undesirable behaviour.</p>
*
* <p>#ThreadSafe# if both paired objects are thread-safe</p>
*
* @param <L> the left element type
* @param <R> the right element type
*
* @since Lang 3.0
*/
public final class ImmutablePair<L, R> extends Pair<L, R> {

/**
* An immutable pair of nulls.
*/
// This is not defined with generics to avoid warnings in call sites.
@SuppressWarnings("rawtypes")
private static final ImmutablePair NULL = ImmutablePair.of(null, null);

/** Serialization version */
private static final long serialVersionUID = 4954918890077093841L;

/**
* Returns an immutable pair of nulls.
*
* @param <L> the left element of this pair. Value is {@code null}.
* @param <R> the right element of this pair. Value is {@code null}.
* @return an immutable pair of nulls.
* @since 3.6
*/
@SuppressWarnings("unchecked")
public static <L, R> ImmutablePair<L, R> nullPair() {
return NULL;
}

/** Left object */
public final L left;
/** Right object */
public final R right;

/**
* <p>Obtains an immutable pair of from two objects inferring the generic types.</p>
*
* <p>This factory allows the pair to be created using inference to
* obtain the generic types.</p>
*
* @param <L> the left element type
* @param <R> the right element type
* @param left the left element, may be null
* @param right the right element, may be null
* @return a pair formed from the two parameters, not null
*/
public static <L, R> ImmutablePair<L, R> of(final L left, final R right) {
return new ImmutablePair<>(left, right);
}

/**
* Create a new pair instance.
*
* @param left the left value, may be null
* @param right the right value, may be null
*/
public ImmutablePair(final L left, final R right) {
super();
this.left = left;
this.right = right;
}

//-----------------------------------------------------------------------
/**
* {@inheritDoc}
*/
@Override
public L getLeft() {
return left;
}

/**
* {@inheritDoc}
*/
@Override
public R getRight() {
return right;
}

/**
* <p>Throws {@code UnsupportedOperationException}.</p>
*
* <p>This pair is immutable, so this operation is not supported.</p>
*
* @param value the value to set
* @return never
* @throws UnsupportedOperationException as this operation is not supported
*/
@Override
public R setValue(final R value) {
throw new UnsupportedOperationException();
}

}

public final L left;public final R right;两个不可变的值注定了这个类是不可变的。
在构造方法ImmutablePair(final L left, final R right)中初始化这两个值。
本类实现了 getLeft()getRight()方法,返回leftright
当我们试图调用setValue()方法的时候会抛出UnsupportedOperationException异常,因为这一对值是不可变的。

ImmutablePair.class

1
2
3
4
5
6
7
8
9
10
11
 public void setRight(final R right) {
this.right = right;
}
public void setLeft(final L left) {
this.left = left;
}
public R setValue(final R value) {
final R result = getRight();
setRight(value);
return result;
}

MutablePairImmutablePair类基本一样,只是在定义public L left;public R right;没有使用final类型的。注定了可以修改这两个值。并且提供了setter方法可以供修改这一对值,并且提供了setValuef()方法修改right值。

现在我们可以愉快的使用这个类来作为返回两个参数的方法的返回值了。

提问

如果你想问,我返回的参数不是2个,是3个怎么办?

没问题,一样满足你,在这个org.apache.commons.lang3.tuple包中提供了针对构建三个元素的Triple类,类定义中abstract class Triple<L, M, R>。定义了3个泛型同样提供了ImmutableTripleMutableTriple一对不可变和可变的实现类。

你过你还问我,我返回的参数是4个怎么办,还有什么类可以供我使用吗?

抱歉,或许你需要定义java bean 去实现你的复杂返回值:)。