• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

equals(Object) of String

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is equals method in a class Foo, will it run faster if I use the hashcode of String at line return obj.equals(this.id) ?

Public class Foo {
private String id = "";

public boolean equals(Object obj) {
if(obj == this) return true;
if(obj == null || !(obj instanceof(Foo))) {
return obj.equals(this.id);
}
}
}
[ March 07, 2005: Message edited by: Yu Chen ]
 
Yu Chen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So the answer is "NO". Here is a test program, any comments welcome.

/*
* TestString.java
* test | int | str eauals/hashcode ==
* 1 | 100,000 | 10/10 , 9/8
* 2 | 1,000,000 | 34/33
* 3 | 10,000,000 | 285/277, 265/260, 264/303, 263/259
*
* Created on March 7, 2005, 9:38 AM
*/

package examples.colorpicker;

public class TestString {
private String id = "This is a test.";

/** Creates a new instance of TestString */
public TestString() {
}

public boolean equals(Object obj) {
if(obj == this) return true;
if(obj == null || !(obj instanceof TestString)) {
return false;
}
TestString ts = (TestString) obj;
//return ts.id.equals(this.id); //compares str length, chars, or nullity.
return ts.id.hashCode() == this.id.hashCode();
}

public static void main(String[] args) {
TestString ts = new TestString();
String sample = "This is a test.";
long start = System.currentTimeMillis();
for(int i = 0; i<10000000; i++) {
ts.equals(sample);
}
long end = System.currentTimeMillis();

System.out.println("Programs runs for " + (end - start));
}
}
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The line

if(obj == null || !(obj instanceof(Foo)))

can just be

if(!(obj instanceof Foo))

instanceof will return false if obj is null.
 
Yu Chen
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, I also tested, it runs a little bit faster w/o the nullity check. Null is not an instance of anything, so all instanceof tests with a null reference produce false.

Thanks!
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if I'm wrong, but it is possible that 2 String objects return the same hash code but is not meaningfully equivalent. Hence, it is inappropriate/wrong to replace the equals() with hashCode() in your situation.

Since what you're doing wasn't correct, it won't be a fair test to say that the replacement did not result in a better performance.

Just my thoughts
reply
    Bookmark Topic Watch Topic
  • New Topic