日期:2014-05-20 浏览次数:20899 次
package com.wanmei.test;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
class T2 {
private int id;
private String name;
public T2(int id, String name) {
super();
this.id = id;
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 17;
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;
T2 other = (T2) 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;
}
public String toString() {
return this.id + "-----------" + this.name;
}
}
public class T1 {
public static void main(String[] args) {
Set<T2> s = new HashSet<T2>();
s.add(new T2(1, "A"));
s.add(new T2(5, "c"));
s.add(new T2(3, "d"));
s.add(new T2(4, "e"));
s.add(new T2(4, "e"));
Iterator<T2> it = s.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
}
}