标签:shc 对象 自定义类 als div span 重复 避免 实现类
public class TestTreeSet {
public static void main(String[] args) {
TreeSet t = new TreeSet();
t.add(new Person(12, "b"));
t.add(new Person(12, "d"));
t.add(new Person(11, "c"));
t.add(new Person(10, "b"));
System.out.println(t);// [Person [id=10, name=b],
// Person [id=11, name=c],
// Person [id=12, name=b],
// Person [id=12, name=d]]
}
}
class Person implements Comparable {
private Integer id;
private String name;
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Person() {
super();
}
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
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;
Person other = (Person) 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;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
@Override
public int compareTo(Object o) {
if (o instanceof Person) {
Person p = (Person) o;
int i = this.getId().compareTo(p.getId());
if (i == 0) {
return this.getName().compareTo(p.getName());
}
return i;
}
return 0;
}
}
public class TestTreeSet {
public static void main(String[] args) {
TreeSet t = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if (o1 instanceof Person && o2 instanceof Person) {
Person p1 = (Person) o1;
Person p2 = (Person) o2;
int i = p1.getId().compareTo(p2.getId());
if (i == 0) {
return p1.getName().compareTo(p2.getName());
}
return i;
}
return 0;
}
});
t.add(new Person(12, "b"));
t.add(new Person(12, "a"));
t.add(new Person(11, "c"));
t.add(new Person(10, "b"));
System.out.println(t);// [Person [id=10, name=b],
// Person [id=11, name=c],
// Person [id=12, name=a],
// Person [id=12, name=b]]
}
}
class Person {
private Integer id;
private String name;
public Person(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Person() {
super();
}
public Integer getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
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;
Person other = (Person) 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;
}
@Override
public String toString() {
return "Person [id=" + id + ", name=" + name + "]";
}
}
标签:shc 对象 自定义类 als div span 重复 避免 实现类
原文地址:http://www.cnblogs.com/chendifan/p/6535674.html