fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. class HashKey {
  8.  
  9. private int keyPartOne;
  10. private int KeyPartTwo;
  11.  
  12. public HashKey(int keyPartOne, int keyPartTwo) {
  13. this.keyPartOne = keyPartOne;
  14. KeyPartTwo = keyPartTwo;
  15. }
  16.  
  17. public int getKeyPartOne() {
  18. return keyPartOne;
  19. }
  20.  
  21. public void setKeyPartOne(int keyPartOne) {
  22. this.keyPartOne = keyPartOne;
  23. }
  24.  
  25. public int getKeyPartTwo() {
  26. return KeyPartTwo;
  27. }
  28.  
  29. public void setKeyPartTwo(int keyPartTwo) {
  30. KeyPartTwo = keyPartTwo;
  31. }
  32.  
  33. @Override
  34. public boolean equals(Object o) {
  35. if (this == o) return true;
  36. if (o == null || getClass() != o.getClass()) return false;
  37.  
  38. HashKey hashKey = (HashKey) o;
  39.  
  40. if (keyPartOne != hashKey.keyPartOne) return false;
  41. return KeyPartTwo == hashKey.KeyPartTwo;
  42. }
  43.  
  44. @Override
  45. public int hashCode() {
  46. int result = keyPartOne;
  47. result = 31 * result + KeyPartTwo;
  48. return result;
  49. }
  50. }
  51.  
  52.  
  53. class Ideone
  54. {
  55.  
  56. public static void main (String[] args) throws java.lang.Exception
  57. {
  58. HashMap<HashKey, String> firstMap = new HashMap<>();
  59. HashKey firstHashKey = new HashKey(1, 1);
  60. firstMap.put(firstHashKey, "text 1");
  61. HashKey secondHashKey = new HashKey(1, 2);
  62. firstMap.put(secondHashKey, "text 2");
  63. System.out.println(firstMap.get(firstHashKey));
  64. secondHashKey.setKeyPartTwo(1);
  65. System.out.println(firstMap.get(secondHashKey));
  66.  
  67. System.out.printf("Map size: %d%n", firstMap.size());
  68. System.out.printf("First entry value: %s%n", firstMap.get(new HashKey(1, 1)));
  69. System.out.printf("Second entry value: %s%n", firstMap.get(new HashKey(1, 2)));
  70.  
  71. for(Map.Entry<HashKey, String> entry: firstMap.entrySet()) {
  72. System.out.printf("Key: %s, value: %s%n", entry.getKey(), entry.getValue());
  73. }
  74. }
  75. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
text 1
text 1
Map size: 2
First entry value: text 1
Second entry value: null
Key: HashKey@20, value: text 1
Key: HashKey@20, value: text 2