3
3
/**
4
4
* Created by shenshijun on 15/2/14.
5
5
*/
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 ;
9
11
10
12
public F getFirst () {
11
13
return first ;
@@ -17,11 +19,14 @@ public S getSecond() {
17
19
18
20
@ Override
19
21
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 ;
25
30
}
26
31
27
32
@ Override
@@ -39,14 +44,16 @@ public boolean equals(Object o) {
39
44
40
45
@ Override
41
46
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 ;
45
53
}
46
54
47
55
public Tuple2 (F first , S second ) {
48
56
this .first = first ;
49
57
this .second = second ;
50
-
51
58
}
52
59
}
0 commit comments