Customer类:package com.fuyou;public class Customer {
private String name;
private int age;
public Customer() {
}
public Customer(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
}
public boolean equals(Object o){
if(this==o) return true;
if(!(o instanceof Customer)) return false;
final Customer other = (Customer)o;
if(this.name.equals(other.getName())&&this.age==other.getAge())
return true;
else
return false;
}
}
测试:package com.fuyou;import java.util.HashSet;
import java.util.Set;public class MapTester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Customer> set = new HashSet<Customer>();
Customer ct1 = new Customer("fuyou",22);
Customer ct2 = new Customer("fuyou",22);
System.out.println(ct1.equals(ct2)); //true
set.add(ct1);
set.add(ct2); //
System.out.println(set.size()); //打出2
}}
public boolean add(E e)如果此 set 中尚未包含指定元素,则添加指定元素。更确切地讲,如果此 set 没有包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则向此 set 添加指定的元素 e。如果此 set 已包含该元素,则该调用不更改 set 并返回 false。
api上写是根据equals方法判断是否相等,但本例中为什么会加入两个,我看了源代码: public boolean add(E o) {
return map.put(o, PRESENT)==null;
}
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, key, value, i);
return null;
}
源代码上根据hashCode来判断,为什么api,书上都写是equasl判断?难道是因为重写了equasl 方法,就必须也重写hashCode,以保证用equals返回true ,它们的hashCode也必须相等
如果是这样:那不是可也可是这样理解:在HastSet add方法时,只要hashCode相同,就不会再加入到集合里!
private String name;
private int age;
public Customer() {
}
public Customer(String name, int age) {
super();
this.name = name;
this.age = age;
} 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;
}
public boolean equals(Object o){
if(this==o) return true;
if(!(o instanceof Customer)) return false;
final Customer other = (Customer)o;
if(this.name.equals(other.getName())&&this.age==other.getAge())
return true;
else
return false;
}
}
测试:package com.fuyou;import java.util.HashSet;
import java.util.Set;public class MapTester { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Set<Customer> set = new HashSet<Customer>();
Customer ct1 = new Customer("fuyou",22);
Customer ct2 = new Customer("fuyou",22);
System.out.println(ct1.equals(ct2)); //true
set.add(ct1);
set.add(ct2); //
System.out.println(set.size()); //打出2
}}
public boolean add(E e)如果此 set 中尚未包含指定元素,则添加指定元素。更确切地讲,如果此 set 没有包含满足 (e==null ? e2==null : e.equals(e2)) 的元素 e2,则向此 set 添加指定的元素 e。如果此 set 已包含该元素,则该调用不更改 set 并返回 false。
api上写是根据equals方法判断是否相等,但本例中为什么会加入两个,我看了源代码: public boolean add(E o) {
return map.put(o, PRESENT)==null;
}
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, key, value, i);
return null;
}
源代码上根据hashCode来判断,为什么api,书上都写是equasl判断?难道是因为重写了equasl 方法,就必须也重写hashCode,以保证用equals返回true ,它们的hashCode也必须相等
如果是这样:那不是可也可是这样理解:在HastSet add方法时,只要hashCode相同,就不会再加入到集合里!
return (name+age).hashCode();
}