Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Javaequals, hashCode, toString                        2011 4    2                      twitter: @zaki50
• Effective Java       2  • ISBN:978-4-89471-499-1  • 3,780     (    )  •      8,       9,       10
•                  equals(Object)• equals(Object)     hashCode()• toString()
Equals
Equals   ==• ==                (  null)• equals
Map   put            get
override•                                       (   : Thread)•    (   :enum)•                                             ...
override    equals
Object                          equals•• public boolean equals(Object obj) {        return (this == obj);    }
equals• x.equals(x) == true          x != null && y != null                                    && z != null• x.equals(null...
private final int id;private final String name;@Overridepublic boolean equals(Object obj) {	 if (this == obj)	 	 return tr...
•                             equals                           equals•    (java.net.URL   ...)
hashCode
override • equals(Object)   hashCode()
hashCode• equals• x.equals(y)          x.hashCode() ==  y.hashCode()• !x.equals(y)  x.hashCode() != y.hashCode()          ...
private final int userId;private final String name;@Overridepublic int hashCode() {	 final int prime = 31;	 int result = 1...
ToString
ToString•••
private final int id;private final String name;@Overridepublic String toString() {	 return "GoodClass [userId=" + userId +...
• IDE• commons-lang   EqualsBuilder/ HashCodeBuilder/ToStringBuilder
IDE
@Override	   public int hashCode() {	   	   final int prime = 31;	   	   int result = 1;	   	   result = prime * result + ...
commons-lang• EqualsBuilder/HashCodeBuilder/  ToStringBuilder •                     reflection* •
•                  equals(Object)• equals(Object)     hashCode()• toString()
ぐだ生 Java入門第一回(equals hash code_tostring)
ぐだ生 Java入門第一回(equals hash code_tostring)
Upcoming SlideShare
Loading in …5
×

ぐだ生 Java入門第一回(equals hash code_tostring)

2,961 views

Published on

Published in: Technology
  • Be the first to comment

ぐだ生 Java入門第一回(equals hash code_tostring)

  1. 1. Javaequals, hashCode, toString 2011 4 2 twitter: @zaki50
  2. 2. • Effective Java 2 • ISBN:978-4-89471-499-1 • 3,780 ( ) • 8, 9, 10
  3. 3. • equals(Object)• equals(Object) hashCode()• toString()
  4. 4. Equals
  5. 5. Equals ==• == ( null)• equals
  6. 6. Map put get
  7. 7. override• ( : Thread)• ( :enum)• ( : Random)• ( :HashSet(AbstractSet ))• equals • AssertionError
  8. 8. override equals
  9. 9. Object equals•• public boolean equals(Object obj) { return (this == obj); }
  10. 10. equals• x.equals(x) == true x != null && y != null && z != null• x.equals(null) == false• x.equals(y) == y.equals(x)• x.equals(y) && y.equals(z) => x.equals(z)• setter
  11. 11. private final int id;private final String name;@Overridepublic boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Sample other = (Sample) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) equals(Object) return false; } else if (!name.equals(other.name)) return false; return true;}
  12. 12. • equals equals• (java.net.URL ...)
  13. 13. hashCode
  14. 14. override • equals(Object) hashCode()
  15. 15. hashCode• equals• x.equals(y) x.hashCode() == y.hashCode()• !x.equals(y) x.hashCode() != y.hashCode() x != null && y != null
  16. 16. private final int userId;private final String name;@Overridepublic int hashCode() { final int prime = 31; int result = 1; result = prime * result + userId; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result;} hashCode()
  17. 17. ToString
  18. 18. ToString•••
  19. 19. private final int id;private final String name;@Overridepublic String toString() { return "GoodClass [userId=" + userId + ", name=" + name + "]";}
  20. 20. • IDE• commons-lang EqualsBuilder/ HashCodeBuilder/ToStringBuilder
  21. 21. IDE
  22. 22. @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Sample other = (Sample) obj; if (id != other.id) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; }
  23. 23. commons-lang• EqualsBuilder/HashCodeBuilder/ ToStringBuilder • reflection* •
  24. 24. • equals(Object)• equals(Object) hashCode()• toString()

×