Advertisement
osipyonok

Pair java

Sep 24th, 2016
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.72 KB | None | 0 0
  1. public class Pair<T1 , T2> {
  2.     T1 x;
  3.     T2 y;
  4.     Pair(T1 _x , T2 _y){
  5.         x = _x;
  6.         y = _y;
  7.     }
  8.     T1 first(){
  9.         return x;
  10.     }
  11.     T2 second(){
  12.         return y;
  13.     }
  14.    
  15.     public boolean equals(Object other){
  16.         if (other instanceof Pair) {
  17.             Pair otherPair = (Pair) other;
  18.             return
  19.             ((  this.x == otherPair.x ||
  20.                 ( this.x != null && otherPair.x != null &&
  21.                   this.x.equals(otherPair.x))) &&
  22.              (  this.y == otherPair.y ||
  23.                 ( this.y != null && otherPair.y != null &&
  24.                   this.y.equals(otherPair.y))) );
  25.         }
  26.         return false;
  27.     }
  28.    
  29.     public int hashCode(){ 
  30.         int hash = 31;
  31.         int res = 1;
  32.         res = hash * res + x.hashCode();
  33.         res = hash * res + y.hashCode();
  34.         return res;
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement