Advertisement
Guest User

pair java

a guest
Jun 13th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package util;
  2.  
  3. public class Pair<A, B> {
  4.     private A first;
  5.     private B second;
  6.  
  7.     public Pair(A first, B second) {
  8.         super();
  9.         this.first = first;
  10.         this.second = second;
  11.     }
  12.  
  13.     public int hashCode() {
  14.         int hashFirst = first != null ? first.hashCode() : 0;
  15.         int hashSecond = second != null ? second.hashCode() : 0;
  16.  
  17.         return (hashFirst + hashSecond) * hashSecond + hashFirst;
  18.     }
  19.  
  20.     public boolean equals(Object other) {
  21.         if (other instanceof Pair) {
  22.             Pair otherPair = (Pair) other;
  23.             return
  24.                     ((this.first == otherPair.first ||
  25.                             (this.first != null && otherPair.first != null &&
  26.                                     this.first.equals(otherPair.first))) &&
  27.                             (this.second == otherPair.second ||
  28.                                     (this.second != null && otherPair.second != null &&
  29.                                             this.second.equals(otherPair.second))));
  30.         }
  31.  
  32.         return false;
  33.     }
  34.  
  35.     public String toString() {
  36.         return "(" + first + ", " + second + ")";
  37.     }
  38.  
  39.     public A getFirst() {
  40.         return first;
  41.     }
  42.  
  43.     public void setFirst(A first) {
  44.         this.first = first;
  45.     }
  46.  
  47.     public B getSecond() {
  48.         return second;
  49.     }
  50.  
  51.     public void setSecond(B second) {
  52.         this.second = second;
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement