Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if(this.getClass() =! other.getClass()) {
- return false; //inequal
- }
- public class Services {
- private static Map<String, Service> SERVICES = new HashMap<String, Service>();
- static interface Service {
- /** Services with the same name are considered equivalent */
- public String getName();
- }
- public static synchronized void installService(Service service) {
- SERVICES.put(service.getName(), service);
- }
- public static synchronized Service lookup(String name) {
- return SERVICES.get(name);
- }
- }
- @Override
- public boolean equals(Object obj) {
- if(this == obj) {
- return true;
- }
- // only do this if you are a subclass and care about equals of parent
- if(!super.equals(obj)) {
- return false;
- }
- if(obj == null || getClass() != obj.getClass()) {
- return false;
- }
- final YourTypeHere other = (YourTypeHere) obj;
- if(!instanceMember1.equals(other.instanceMember1)) {
- return false;
- }
- ... rest of instanceMembers in same pattern as above....
- return true;
- }
- Integer a = Integer.valueOf( 2 );
- Integer b = Integer.valueOf( 2 );
- a == b
- new Integer( 2 ) == new Integer( 2 )
- Adding duplicates to a map (bad):
- Result of map.get(bean1):first
- Result of map.get(bean2):second
- Result of map.get(new NameBean("Alice"): null
- Adding duplicates to a map (good):
- Result of map.get(bean1):second
- Result of map.get(bean2):second
- Result of map.get(new ImprovedNameBean("Alice"): second
- // This bean cannot safely be used as a key in a Map
- public class NameBean {
- private String name;
- public NameBean() {
- }
- public NameBean(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- @Override
- public String toString() {
- return name;
- }
- }
- // This bean can safely be used as a key in a Map
- public class ImprovedNameBean extends NameBean {
- public ImprovedNameBean(String name) {
- super(name);
- }
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if(obj == null || getClass() != obj.getClass()) {
- return false;
- }
- return this.getName().equals(((ImprovedNameBean)obj).getName());
- }
- @Override
- public int hashCode() {
- return getName().hashCode();
- }
- }
- public class MapDuplicateTest {
- public static void main(String[] args) {
- MapDuplicateTest test = new MapDuplicateTest();
- System.out.println("Adding duplicates to a map (bad):");
- test.withDuplicates();
- System.out.println("nAdding duplicates to a map (good):");
- test.withoutDuplicates();
- }
- public void withDuplicates() {
- NameBean bean1 = new NameBean("Alice");
- NameBean bean2 = new NameBean("Alice");
- java.util.Map<NameBean, String> map
- = new java.util.HashMap<NameBean, String>();
- map.put(bean1, "first");
- map.put(bean2, "second");
- System.out.println("Result of map.get(bean1):"+map.get(bean1));
- System.out.println("Result of map.get(bean2):"+map.get(bean2));
- System.out.println("Result of map.get(new NameBean("Alice"): "
- + map.get(new NameBean("Alice")));
- }
- public void withoutDuplicates() {
- ImprovedNameBean bean1 = new ImprovedNameBean("Alice");
- ImprovedNameBean bean2 = new ImprovedNameBean("Alice");
- java.util.Map<ImprovedNameBean, String> map
- = new java.util.HashMap<ImprovedNameBean, String>();
- map.put(bean1, "first");
- map.put(bean2, "second");
- System.out.println("Result of map.get(bean1):"+map.get(bean1));
- System.out.println("Result of map.get(bean2):"+map.get(bean2));
- System.out.println("Result of map.get(new ImprovedNameBean("Alice"): "
- + map.get(new ImprovedNameBean("Alice")));
- }
- }
Add Comment
Please, Sign In to add comment