code_junkie

What are the alternatives to comparing the equality of two objects?

Nov 12th, 2011
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. if(this.getClass() =! other.getClass()) {
  2. return false; //inequal
  3. }
  4.  
  5. public class Services {
  6.  
  7. private static Map<String, Service> SERVICES = new HashMap<String, Service>();
  8.  
  9. static interface Service {
  10. /** Services with the same name are considered equivalent */
  11. public String getName();
  12. }
  13.  
  14. public static synchronized void installService(Service service) {
  15. SERVICES.put(service.getName(), service);
  16. }
  17.  
  18. public static synchronized Service lookup(String name) {
  19. return SERVICES.get(name);
  20. }
  21.  
  22. }
  23.  
  24. @Override
  25. public boolean equals(Object obj) {
  26. if(this == obj) {
  27. return true;
  28. }
  29.  
  30. // only do this if you are a subclass and care about equals of parent
  31. if(!super.equals(obj)) {
  32. return false;
  33. }
  34. if(obj == null || getClass() != obj.getClass()) {
  35. return false;
  36. }
  37. final YourTypeHere other = (YourTypeHere) obj;
  38. if(!instanceMember1.equals(other.instanceMember1)) {
  39. return false;
  40. }
  41. ... rest of instanceMembers in same pattern as above....
  42. return true;
  43. }
  44.  
  45. Integer a = Integer.valueOf( 2 );
  46. Integer b = Integer.valueOf( 2 );
  47.  
  48. a == b
  49.  
  50. new Integer( 2 ) == new Integer( 2 )
  51.  
  52. Adding duplicates to a map (bad):
  53. Result of map.get(bean1):first
  54. Result of map.get(bean2):second
  55. Result of map.get(new NameBean("Alice"): null
  56.  
  57. Adding duplicates to a map (good):
  58. Result of map.get(bean1):second
  59. Result of map.get(bean2):second
  60. Result of map.get(new ImprovedNameBean("Alice"): second
  61.  
  62. // This bean cannot safely be used as a key in a Map
  63. public class NameBean {
  64. private String name;
  65. public NameBean() {
  66. }
  67. public NameBean(String name) {
  68. this.name = name;
  69. }
  70. public String getName() {
  71. return name;
  72. }
  73. public void setName(String name) {
  74. this.name = name;
  75. }
  76. @Override
  77. public String toString() {
  78. return name;
  79. }
  80. }
  81.  
  82. // This bean can safely be used as a key in a Map
  83. public class ImprovedNameBean extends NameBean {
  84. public ImprovedNameBean(String name) {
  85. super(name);
  86. }
  87. @Override
  88. public boolean equals(Object obj) {
  89. if (this == obj) {
  90. return true;
  91. }
  92. if(obj == null || getClass() != obj.getClass()) {
  93. return false;
  94. }
  95. return this.getName().equals(((ImprovedNameBean)obj).getName());
  96. }
  97. @Override
  98. public int hashCode() {
  99. return getName().hashCode();
  100. }
  101. }
  102.  
  103. public class MapDuplicateTest {
  104. public static void main(String[] args) {
  105. MapDuplicateTest test = new MapDuplicateTest();
  106. System.out.println("Adding duplicates to a map (bad):");
  107. test.withDuplicates();
  108. System.out.println("nAdding duplicates to a map (good):");
  109. test.withoutDuplicates();
  110. }
  111. public void withDuplicates() {
  112. NameBean bean1 = new NameBean("Alice");
  113. NameBean bean2 = new NameBean("Alice");
  114.  
  115. java.util.Map<NameBean, String> map
  116. = new java.util.HashMap<NameBean, String>();
  117. map.put(bean1, "first");
  118. map.put(bean2, "second");
  119. System.out.println("Result of map.get(bean1):"+map.get(bean1));
  120. System.out.println("Result of map.get(bean2):"+map.get(bean2));
  121. System.out.println("Result of map.get(new NameBean("Alice"): "
  122. + map.get(new NameBean("Alice")));
  123. }
  124. public void withoutDuplicates() {
  125. ImprovedNameBean bean1 = new ImprovedNameBean("Alice");
  126. ImprovedNameBean bean2 = new ImprovedNameBean("Alice");
  127.  
  128. java.util.Map<ImprovedNameBean, String> map
  129. = new java.util.HashMap<ImprovedNameBean, String>();
  130. map.put(bean1, "first");
  131. map.put(bean2, "second");
  132. System.out.println("Result of map.get(bean1):"+map.get(bean1));
  133. System.out.println("Result of map.get(bean2):"+map.get(bean2));
  134. System.out.println("Result of map.get(new ImprovedNameBean("Alice"): "
  135. + map.get(new ImprovedNameBean("Alice")));
  136. }
  137. }
Add Comment
Please, Sign In to add comment