您的位置:首页 > 编程语言 > Java开发

【Hibernate学习笔记】Java集合类

2014-04-22 15:53 489 查看


package mypack2;

public class Customer implements Comparable<Customer> {

private Long id;
private String name;
private int age;

public Customer(String name, int age) {
super();
this.name = name;
this.age = age;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

@Override
public int compareTo(Customer customer)
{
// 先按照name属性排序
if (this.name.compareTo(customer.getName()) > 0)
{
return 1;
}
if (this.name.compareTo(customer.getName()) < 0)
{
return -1;
}

// 再按照age属性排序
if (this.age > customer.getAge())
{
return 1;
}
if (this.age < customer.getAge())
{
return -1;
}
return 0;
}

@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (!(obj instanceof Customer))
{
return false;
}
final Customer customer = (Customer) obj;
if (this.name.equals(customer.getName()) && this.age == customer.getAge())
{
return true;
}
else
{
return false;
}
}

@Override
public int hashCode()
{
int result;
result = (name==null?0:name.hashCode());
result = 29 * result + age;
return result;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: