java 自 定 义 对 象 实 现 排 序

1.让自定义对象本身具有可比较性,首先实现comparable接口,重写compareTo方法。

 例子的思路:比较student对象的count值的大小,进行排序

public class Student implements Comparable<Object>{
private String name;
private String pwd;
private int count;

/**  
* Creates a new instance of Student.  
*  
* @param name
* @param pwd
* @param count  
*/  

public Student(String name, String pwd, int count) {
super();
this.name = name;
this.pwd = pwd;
this.count = count;
}


/**  
* name.  
*  
* @return  the name  
* @since   JDK 1.6  
*/
public String getName() {
return name;
}


/**  
* name.  
*  
* @param   name    the name to set  
* @since   JDK 1.6  
*/
public void setName(String name) {
this.name = name;
}


/**  
* pwd.  
*  
* @return  the pwd  
* @since   JDK 1.6  
*/
public String getPwd() {
return pwd;
}


/**  
* pwd.  
*  
* @param   pwd    the pwd to set  
* @since   JDK 1.6  
*/
public void setPwd(String pwd) {
this.pwd = pwd;
}


/**  
* count.  
*  
* @return  the count  
* @since   JDK 1.6  
*/
public int getCount() {
return count;
}


/**  
* count.  
*  
* @param   count    the count to set  
* @since   JDK 1.6  
*/
public void setCount(int count) {
this.count = count;
}


/**  
* TODO 简单描述该方法的实现功能(可选).  
* @see java.lang.Comparable#compareTo(java.lang.Object)  
*/
@Override
public int compareTo(Object o) {
 
// TODO Auto-generated method stub  
Student s=(Student)o;
if(!(s instanceof Student)){
System.out.println("不是student对象");
throw new RuntimeException("不是student对象");
}
System.out.println("this对象:"+this.getCount()+"=======传入对象:"+s.getCount());
if(this.getCount()>s.getCount()){
return 1;//本类的对象大于比较的对象返回1;
}
else if(this.getCount()==s.getCount()){//本类的对象等于比较的对象返回0;
  return 0;
}
else
{//本类的对象小于比较的对象返回-1;
return -1;
}

}
}

调用类:

Set<Student> st=new TreeSet<Student>();
st.add(new Student("liu","liu123",89));
st.add(new Student("jian","liu123",123));
st.add(new Student("four","liu123",4));
st.add(new Student("four","liu123",4));
st.add(new Student("ni","liu123",56));
st.add(new Student("hao","liu123",23));
System.out.println("排序后遍历:");
for(Student s:st){
System.out.println(s.getName()+"=="+s.getCount());
}

结果:

排序后遍历:
four==4
hao==23
ni==56
liu==89
jian==123

总结:不仅实现了排序还实现了去重效果。

2.当对象不具有比较性时,让容器自身具有比较性。首先实现comparator,重写compare方法,将比较器作为参数传递给treeset集合对象。

例子思路:1.先判断对象的name,2.再判断age的值。如果姓名相同,再判断age的值;在name相同的情况下,age从小到大排列

public class Teacher {
private String name;
private int age;


/**  
* Creates a new instance of Teacher.  
*  
* @param name
* @param age  
*/  

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


/**  
* name.  
*  
* @return  the name  
* @since   JDK 1.6  
*/
public String getName() {
return name;
}


/**  
* name.  
*  
* @param   name    the name to set  
* @since   JDK 1.6  
*/
public void setName(String name) {
this.name = name;
}


/**  
* age.  
*  
* @return  the age  
* @since   JDK 1.6  
*/
public int getAge() {
return age;
}


/**  
* age.  
*  
* @param   age    the age to set  
* @since   JDK 1.6  
*/
public void setAge(int age) {
this.age = age;
}


/**  
* TODO 简单描述该方法的实现功能(可选).  
* @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)  
*/



}
  public class MyCompare implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
 
// TODO Auto-generated method stub  
Teacher s1=(Teacher)o1;
Teacher s2=(Teacher)o2;
int n=s1.getName().compareTo(s2.getName());
if(n==0){//姓名相同,比较统计数
n=new Integer(s1.getAge()).compareTo(s2.getAge());
}
return n;
}
}
  

测试类:

System.out.println("==================");
Set<Teacher> t=new TreeSet<Teacher>(new MyCompare());
t.add(new Teacher("liu",89));
t.add(new Teacher("jian",123));
t.add(new Teacher("jian",123));
t.add(new Teacher("four",4));
t.add(new Teacher("ni",56));
t.add(new Teacher("ni",56));
t.add(new Teacher("hao",23));
System.out.println("排序后遍历:");
for(Teacher s:t){
System.out.println(s.getName()+"=="+s.getAge());
}

运行结果:实现了重启效果。

排序后遍历:
four==4
hao==23
jian==123
liu==89
ni==56

System.out.println("==================");
Set<Teacher> t=new TreeSet<Teacher>(new MyCompare());
t.add(new Teacher("liu",89));
t.add(new Teacher("jian",1234));
t.add(new Teacher("jian",123));
t.add(new Teacher("four",4));
t.add(new Teacher("ni",56));
t.add(new Teacher("ni",5));
t.add(new Teacher("hao",23));
System.out.println("排序后遍历:");
for(Teacher s:t){
System.out.println(s.getName()+"=="+s.getAge());
}

结果:在name相同,age从小大排列

==================
排序后遍历:
four==4
hao==23
jian==123
jian==1234

liu==89
ni==5
ni==56

2 例子思路:1.通过比较对象teacher的age从小到大排列,和前面使用让对象具有比较性,的例子一样的原理:

修改

public class MyCompare implements Comparator<Object> {
@Override
public int compare(Object o1, Object o2) {
 
// TODO Auto-generated method stub  
Teacher s1=(Teacher)o1;
Teacher s2=(Teacher)o2;
//int n=s1.getName().compareTo(s2.getName());
//if(n==0){//姓名相同,比较统计数
int n=new Integer(s1.getAge()).compareTo(s2.getAge());
//}
return n;
}
}

测试类:

System.out.println("==================");
Set<Teacher> t=new TreeSet<Teacher>(new MyCompare());
t.add(new Teacher("liu",89));
t.add(new Teacher("jian",123));
t.add(new Teacher("four",4));
t.add(new Teacher("ni",56));
t.add(new Teacher("hao",23));
System.out.println("排序后遍历:");
for(Teacher s:t){
System.out.println(s.getName()+"=="+s.getAge());
}

运行结果:

排序后遍历:
four==4
hao==23
ni==56
liu==89
jian==123


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值