8000 修改Tuple2部分实现,使得类更加符合可变性的要求。 · ssjssh/javaalgorithm@7fefc21 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7fefc21

Browse files
author
shengshijun
committed
修改Tuple2部分实现,使得类更加符合可变性的要求。
1 parent 1db3449 commit 7fefc21

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

src/main/java/ssj/algorithm/lang/Tuple2.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
/**
44
* Created by shenshijun on 15/2/14.
55
*/
6-
public class Tuple2<F, S> {
7-
private F first;
8-
private S second;
6+
final public class Tuple2<F, S> {
7+
final private F first;
8+
final private S second;
9+
private Integer hash_code = null;
10+
private String to_string = null;
911

1012
public F getFirst() {
1113
return first;
@@ -17,11 +19,14 @@ public S getSecond() {
1719

1820
@Override
1921
public String toString() {
20-
final StringBuilder sb = new StringBuilder("Tuple2{");
21-
sb.append("first=").append(first);
22-
sb.append(", second=").append(second);
23-
sb.append('}');
24-
return sb.toString();
22+
if (to_string == null) {
23+
final StringBuilder sb = new StringBuilder("Tuple2{");
24+
sb.append("first=").append(first);
25+
sb.append(", second=").append(second);
26+
sb.append('}');
27+
to_string = sb.toString();
28+
}
29+
return to_string;
2530
}
2631

2732
@Override
@@ -39,14 +44,16 @@ public boolean equals(Object o) {
3944

4045
@Override
4146
public int hashCode() {
42-
int result = first != null ? first.hashCode() : 0;
43-
result = 31 * result + (second != null ? second.hashCode() : 0);
44-
return result;
47+
if (hash_code == null) {
48+
int result = first != null ? first.hashCode() : 0;
49+
result = 31 * result + (second != null ? second.hashCode() : 0);
50+
hash_code = result;
51+
}
52+
return hash_code;
4553
}
4654

4755
public Tuple2(F first, S second) {
4856
this.first = first;
4957
this.second = second;
50-
5158
}
5259
}

0 commit comments

Comments
 (0)
0