Advertisement
Guest User

Untitled

a guest
Dec 14th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1.  
  2. public class Pair<T, S> {
  3. //S and T are not necessarily different yypes, but they could be
  4. private T first;
  5. private S second;
  6.  
  7. public Pair(T first, S second) {
  8. this.first = first;
  9. this.second = second;
  10. }
  11. public T getFirst() {
  12. return first;
  13. }
  14. public S getSecond() {
  15. return second;
  16. }
  17.  
  18. public static void main(String[] args) {
  19. Pair<String, Integer> p = new Pair<>("hello", 5);
  20. System.out.println(p.getSecond());
  21. }
  22.  
  23. @Override public boolean equals(Object obj) {
  24. if(this == obj) {
  25. return true;
  26. }
  27. if(!(obj instanceof Pair)){
  28. return false;
  29. }
  30.  
  31. Pair<?, ?> other = (Pair<?, ?>) obj;
  32. if(this.first.equals(other.first) && this.second.equals(other.second)) {
  33. return true;
  34. }
  35. else{
  36. return false;
  37. }
  38. }
  39. @Override public int hashCode(){
  40. int result = 17;
  41. result = 37 * result + first.hashCode();
  42. result = 37 * result + second.hashCode();
  43.  
  44. return result;
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement