(p.toString()); ? ? ? } ? ? ? ? ? ? ? ? ? /* ? ? ? ? * 使用comparator进行排序的第二种方法, ? ? ? ? * 该方法不需要People实现Comparator接口 ? ? ? ? * 直接Comparator对象传递给TreeSet的构造器, ? ? ? ? * 并重载Comparator类的compara方法,指定排序规则 ? ? ? ? */ //? ? SortedSetset = new TreeSet<>( //? ? ? ? ? ? newComparator() //? ? ? ? ? ? { // //? ? ? ? ? ? ? ? //重载Comparator类的compara方法,设定比较规则:按名字降序排列 //? ? ? ? ? ? ? ? @Override //? ? ? ? ? ? ? ? publicint compare(People people1, People people2) //? ? ? ? ? ? ? ? { //? ? ? ? ? ? ? ? ? //TODO Auto-generated method stub //? ? ? ? ? ? ? ? ? if(people1.getName().compareTo(people2.getName())>0) //? ? ? ? ? ? ? ? ? { //? ? ? ? ? ? ? ? ? ? ? return-1; //? ? ? ? ? ? ? ? ? } //? ? ? ? ? ? ? ? ? else //? ? ? ? ? ? ? ? ? { //? ? ? ? ? ? ? ? ? ? ? return1; //? ? ? ? ? ? ? ? ? } //? ? ? ? ? ? ? ? ? //? ? ? ? ? ? ? ? } //? ? ? ? ? ? ? //? ? ? ? ? ? } //? ? ? ? ? ? ); //? ? set.addAll(list); //? ? System.out.println("输出按降序排列的结果:"); //? ? for(Peoplep: set) //? ? { //? ? ? ? System.out.println(p.toString()); //? ? } ? ? } ? }
|