最近的面试题:
1.写 equals method
public class Label {
private String text;
private int size;
public boolean equals(Object obj){
if(Null!=obj) {
if(obj.getClass()==Label.class)
Label babel = (Label)obj;
if(NULl!=label.text)
return (label.text.equals(text) && label.size == size)
else
return (label.text==null && label.size ==size) // for label.text = null case
}
return false;
}
}
还有个思考就是,如果Label 还有一个variable :private Label label; 要怎么写?
记得考虑是NULL的情况。
2. Hashcode method return 一个常数有什么缺点?
good example :
http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java
//Written by K@stackoverflow
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Subash Adhikari",28));
people.add(new Person("K",28));
people.add(new Person("StackOverflow",4));
people.add(new Person("Subash Adhikari",28));
for (int i=0;i<people.size()-1;i++){
for (int y=i+1;y<=people.size()-1;y++){
System.out.println("-- " + people.get(i).getName() + " - VS - " + people.get(y).getName());
boolean check = people.get(i).equals(people.get(y));
System.out.println(check);
}
}
}
}
//written by K@stackoverflow
public class Person {
private String name;
private int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Person other = (Person) obj;
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (this.age != other.age) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 3;
hash = 53 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 53 * hash + this.age;
return hash;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
下面这篇文章不错:
http://tutorials.jenkov.com/java-collections/hashcode-equals.html
http://www.javapractices.com/topic/TopicAction.do?Id=17
1)The hashCode()
method of objects is used when you insert them into a HashTable
, HashMap
or HashSet
. If you do not know the theory of how a hashtable works internally, you can read about hastables on Wikipedia.org.
When inserting an object into a hastable you use a key. The hash code of this key is calculated, and used to determine where to store the object internally. When you need to lookup an object in a hashtable you also use a key. The hash code of this key is calculated and used to determine where to search for the object.
The hash code only points to a certain "area" (or list, bucket etc) internally. Since different key objects could potentially have the same hash code, the hash code itself is no guarantee that the right key is found. The hashtable then iterates this area (all keys with the same hash code) and uses the key's equals()
method to find the right key. Once the right key is found, the object stored for that key is returned.
So, as you can see, a combination of the hashCode()
and equals()
methods are used when storing and when looking up objects in a hashtable.
Here are two rules that are good to know about implementing the hashCode()
method in your own classes, if the hashtables in the Java Collections API are to work correctly:
- If object1 and object2 are equal according to their
equals()
method, they must also have the same hash code. - If object1 and object2 have the same hash code, they do NOT have to be equal too.
2)hashCode and equals are closely related:
a) if you override equals, you must override hashCode.
b) hashCode must generate equal values for equal objects.
c) equals and hashCode must depend on the same set of "significant" fields. You must use the same set of fields in both of these methods. You are not required to use all fields. For example, a calculated field that depends on others should very likely be omitted from equalsand hashCode.