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

java集合TreeSet的两种排序方式

2015-09-13 20:27 585 查看

java集合TreeSet的两种排序方式:

自然排序:①要求添加进TreeSet中的元素所在的类implements Comparable接口

②重写compareTo(Object obj),在此方法内指明按照元素的哪个属性进行排序

③向TreeSet中添加元素即可。若不实现此接口,会报运行时异常

定制排序: ①创建一个实现Comparator接口的实现类的对象。在实现类中重写Comparator的compare(Object o1,Object o2)方法

②在此compare()方法中指明按照元素所在类的哪个属性进行排序

③将此实现Comparator接口的实现类的对象作为形参传递给TreeSet的构造器中

④向TreeSet中添加元素即可。若不实现此接口,会报运行时异常

>要求重写的compareTo()或者compare()方法与equals()和hashCode()方法保持一致。

任务:

定义一个Employee类,

该类包含:private成员变量name,age,birthday,其中 birthday 为 MyDate 类的对象;

并为每一个属性定义 getter, setter 方法;

并重写 toString 方法输出 name, age, birthday

MyDate类包含:

private成员变量month,day,year;并为每一个属性定义 getter, setter 方法;

创建该类的 5 个对象,并把这些对象放入 TreeSet 集合中(下一章:TreeSet 需使用泛型来定义)

分别按以下两种方式对集合中的元素进行排序,并遍历输出:

1). 使Employee实现Comparable 接口,并按 name 排序

2). 创建TreeSet 时传入 Comparator对象,按生日日期的先后排序。

提示:Employee类是否需要重写equals()方法?MyDate类呢?

public class Employee implements Comparable{
private String name;
private Integer age;
private MyDate birthday;

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

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

public MyDate getBirthday() {
return birthday;
}

public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}

public Employee(String name, Integer age, MyDate myDate) {
super();
this.name = name;
this.age = age;
this.birthday = myDate;
}

@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", birthday="
+ birthday + "]";
}

@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e = (Employee)o;
return this.name.compareTo(e.name);
}
return 0;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
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;
Employee other = (Employee) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}
public class MyDate {
private  int month;
private int day;
private int year;
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public MyDate(int month, int day, int year) {
super();
this.month = month;
this.day = day;
this.year = year;
}
@Override
public String toString() {
return "MyDate [month=" + month + ", day=" + day + ", year=" + year
+ "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + day;
result = prime * result + month;
result = prime * result + year;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MyDate other = (MyDate) obj;
if (day != other.day)
return false;
if (month != other.month)
return false;
if (year != other.year)
return false;
return true;
}

}
public class Employee1 {
private String name;
private Integer age;
private MyDate birthday;

public String getName() {
return name;
}

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

public Integer getAge() {
return age;
}

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

public MyDate getBirthday() {
return birthday;
}

public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}

public Employee1(String name, Integer age, MyDate myDate) {
super();
this.name = name;
this.age = age;
this.birthday = myDate;
}

@Override
public String toString() {
return "Employee [name=" + name + ", age=" + age + ", birthday="
+ birthday + "]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((age == null) ? 0 : age.hashCode());
result = prime * result
+ ((birthday == null) ? 0 : birthday.hashCode());
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;
Employee1 other = (Employee1) obj;
if (age == null) {
if (other.age != null)
return false;
} else if (!age.equals(other.age))
return false;
if (birthday == null) {
if (other.birthday != null)
return false;
} else if (!birthday.equals(other.birthday))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}

}
public class TestEmployee {
// 定制排序

@Test
// 创建 TreeSet 时传入 Comparator对象,按生日日期的先后排序
public void test2() {
Comparator com = new Comparator(){
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Employee1 && o2 instanceof Employee1){
Employee1 e1 = (Employee1)o1;
Employee1 e2 = (Employee1) o2;
MyDate birth1 = e1.getBirthday();
MyDate birth2 = e2.getBirthday();
if (birth1.getYear() != birth2.getYear()) {
return birth1.getYear() - birth2.getYear();
} else {
if (birth1.getMonth() != birth2.getMonth()) {
return birth1.getMonth() - birth2.getMonth();
} else {
return birth1.getDay() - birth2.getDay();
}
}
}
return 0;
}

};

TreeSet set = new TreeSet(com);
Employee1 e1 = new Employee1("张三", 21, new MyDate(3, 22, 1999));
Employee1 e2 = new Employee1("王三", 22, new MyDate(4, 24, 1990));
Employee1 e3 = new Employee1("列儿科", 25, new MyDate(4, 26, 1993));
Employee1 e4 = new Employee1("李四", 24, new MyDate(2, 24, 1998));
Employee1 e5 = new Employee1("天肉", 23, new MyDate(3, 23, 1995));
Employee1 e6 = new Employee1("天肉", 23, new MyDate(3, 23, 1995));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
set.add(e6);
Iterator i = set.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}

//自然排序
@Test
public void test1() {
Employee e1 = new Employee("张三", 21, new MyDate(3, 22, 1992));
Employee e2 = new Employee("王三", 22, new MyDate(4, 24, 1990));
Employee e3 = new Employee("列儿科", 25, new MyDate(4, 26, 1993));
Employee e4 = new Employee("李四", 24, new MyDate(2, 24, 1998));
Employee e5 = new Employee("天肉", 23, new MyDate(3, 23, 1995));
Employee e6 = new Employee("天肉", 23, new MyDate(3, 23, 1995));
TreeSet<Employee> set = new TreeSet<Employee>();
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
set.add(e6);
Iterator<Employee> i = set.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}

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