Java在集合中使用泛型出现的问题
本帖最后由 Twilight.. 于 2021-12-1 00:39 编辑利用TreeSet实现Comparable、Comparator的两种排序方式(自然排序、定制排序)
如果不采用泛型的话排序就正确了,采用泛型定制排序,结果发现排序不正确,请教大神们问题到底出在哪里了呢{:10_266:}
不采用泛型:
package collectionexer11_5;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class TreeSetTest {
public static void main(String[] args) {
Employee e1 = new Employee("Tom", 22, new MyDate(1999,10,11));
Employee e2 = new Employee("Jerry", 20, new MyDate(2001,3,23));
Employee e3 = new Employee("Rose", 27, new MyDate(1994,5,1));
Employee e6 = new Employee("Rrose", 27, new MyDate(1927,5,1));
Employee e4 = new Employee("Jack", 22, new MyDate(1999,10,21));
Employee e5 = new Employee("Jim", 44, new MyDate(1977,12,6));
//2). 创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序。
System.out.println("\n创建 TreeSet 时传入 Comparator 对象,按生日日期的先后排序");
Comparator com = new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Employee && o2 instanceof Employee){
Employee e1 = (Employee) o1;
Employee e2 = (Employee) o2;
int year1 = e1.getBirthday().getYear();
int year2 = e2.getBirthday().getYear();
if(year1 < year2){
return -1;
}else if(year1 > year2){
return 1;
}else{
int month1 = e1.getBirthday().getMonth();
int month2 = e2.getBirthday().getMonth();
if(month1 < month2){
return -1;
}else if(month1 > month2){
return 1;
}else{
return Integer.compare(e1.getBirthday().getDay(), e2.getBirthday().getDay());
}
}
}
throw new RuntimeException("输入的参数类型有误");
}
};
TreeSet set2 = new TreeSet(com);
set2.add(e1);
set2.add(e2);
set2.add(e3);
set2.add(e4);
set2.add(e5);
set2.add(e6);
Iterator iterator2 = set2.iterator();
while(iterator2.hasNext()){
System.out.println(iterator2.next());
}
}
}
class Employee implements Comparable{
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Employee() {
}
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;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birthday=" + birthday +
'}';
}
@Override
public int compareTo(Object obj){
if(obj instanceof Employee){
Employee e = (Employee)obj;
return this.name.compareTo(e.name);
}
throw new RuntimeException("输入的数据类型不一致");
}
}
class MyDate{
private int year;
private int month;
private int day;
public MyDate() {
}
public MyDate(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = 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;
}
@Override
public String toString() {
return "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
采用泛型后:
package genericdemo12_2;
import org.junit.Test;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
public class EmployeeTest {
//定制排序
@Test
public void testComparator(){
Comparator<Employee> com = new Comparator<Employee>() {
@Override
public int compare(Employee e1, Employee e2) {
// int year1 = e1.getBirthday().getYear();
// int year2 = e2.getBirthday().getYear();
// if (year1 < year2) {
// return -1;
// } else if (year1 > year2) {
// return 1;
// } else {
// int month1 = e1.getBirthday().getMonth();
// int month2 = e2.getBirthday().getMonth();
// if (month1 < month2) {
// return -1;
// } else if (month1 > month2) {
// return 1;
// } else {
// return Integer.compare(e1.getBirthday().getDay(), e2.getBirthday().getDay());
// }
// }
return e1.compareTo(e2);
}
};
TreeSet<Employee> set = new TreeSet<>(com);
Employee e1 = new Employee("Tom", 22, new MyDate(1999, 10, 11));
Employee e2 = new Employee("Jerry", 20, new MyDate(2001, 3, 23));
Employee e3 = new Employee("Rose", 27, new MyDate(1997, 3, 12));
Employee e6 = new Employee("RRose", 27, new MyDate(1927, 3, 12));
Employee e4 = new Employee("Jack", 22, new MyDate(1999, 10, 21));
Employee e5 = new Employee("Jim", 44, new MyDate(1977, 12, 6));
set.add(e1);
set.add(e2);
set.add(e3);
set.add(e4);
set.add(e5);
set.add(e6);
Iterator<Employee> iterator = set.iterator();
while(iterator.hasNext()) {
Employee e = iterator.next();
System.out.println(e);
}
}
}
附employee和MyDate:
package genericdemo12_2;
/**
* @author whoo
* @create 2021-11-30 10:34
*/
public class Employee implements Comparable<Employee>{
private String name;
private int age;
private MyDate birthday;
public Employee(String name, int age, MyDate birthday) {
this.name = name;
this.age = age;
this.birthday = birthday;
}
public Employee() {
}
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;
}
public MyDate getBirthday() {
return birthday;
}
public void setBirthday(MyDate birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", age=" + age +
", birihday=" + birthday +
'}';
}
@Override
public int compareTo(Employee o) {
return this.name.compareTo(o.name);
}
}
package genericdemo12_2;
/**
* @author whoo
* @create 2021-11-30 10:35
*/
public class MyDate implements Comparable<MyDate>{
private int month;
private int day;
private int year;
public MyDate(int month, int day, int year) {
this.month = month;
this.day = day;
this.year = year;
}
public MyDate() {
}
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;
}
@Override
public String toString() {
return "{month=" + month +
", day=" + day +
", year=" + year + "}";
}
@Override
public int compareTo(MyDate o) {
if(this.year < o.year){
return -1;
}else if(this.year > o.year){
return 1;
}else{
if(this.month < o.month){
return -1;
}else if(this.month > o.month){
return 1;
}else{
return Integer.compare(this.day, o.day);
}
}
}
}
没问题啊,是不是你在MyDate的构造函数里写反了:
public MyDate(int month, int day, int year)
应改成public MyDate(int year, int month, int day)
页:
[1]