集合排序的两种方法Comparable接口和Comparator接口(比较器)

集合的排序在集合中非常常见,下面就介绍两种排序方法(以TreeMap和TreeSet举例)

方法一:Comparable接口


package cn.hncu.sort;


import java.util.HashSet;
import java.util.Iterator;
import java.util.TreeSet;


//※TreeSet调用add()方法时,会让新添加的元素调用compareTo(obj)方法,以此与几何里的元素进行比较
public class TreeSetDemo {
public static void main(String[] args) {
//因为TreeSet和TreeMap是排序的,而排序就要用到被排序元素的排序方法(compareTo)
//    ----Comparable接口中的抽象方法,因此使用TreeSet,那么加入它当中的元素必须实现该接口
TreeSet set = new TreeSet();//空参构造,用的是默认构造方法,要调用集合中元素的compareTo()方法
//HashSet set=new HashSet();//因为这个集合不排序,所以加入该集合的元素不用实现Comparable接口
//set.add(new String("xky"));//不行,因为String类中的compareTo(obj)方法中把所有obj都强转成String来比较了。此处obj是Employee对象,因此出现ClassCastException异常
set.add(new Employee("张军", 21));
set.add(new Employee("张三", 22));
set.add(new Employee("李云", 23));
set.add(new Employee("王建安", 20));
set.add(new Employee("王建安", 15));
set.add(new Employee("王建安", 23));
//set.add(new String("hncu"));//不行,因为String类中的compareTo(obj)方法中把所有obj都强转成String来比较了。此处obj是Employee对象,因此出现ClassCastException异常
Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}

 class Employee implements Comparable{
private String id;
private String name;
private int age;
private String depart;
private static int num;
public Employee(String name, int age, String depart) {
this.name = name;
this.age = age;
this.depart = depart;
}
public Employee(String name, int age) {
this(name,age,"");
}
public String getId() {
return id;
}
public void setId(String 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;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", depart=" + depart + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((depart == null) ? 0 : depart.hashCode());
result = prime * result + ((id == null) ? 0 : id.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 != other.age)
return false;
if (depart == null) {
if (other.depart != null)
return false;
} else if (!depart.equals(other.depart))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public int compareTo(Object o) {
if(o instanceof Employee){
Employee e=(Employee) o;
if(age!=e.age){
return age-e.age;
}
else {
return name.compareTo(e.name);
}
}
return -1;//0的时候表示相等,不添加
}
}


方法二:Comparator接口(比较器)



注意:自己写类MyCmp实现Comparator接口


package cn.hncu.sort2;


import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


import cn.hncu.sort2.Employee;




public class TreeSetDemo2 {


public static void main(String[] args) {
Comparator comparator =new MyCmp();
TreeSet set=new TreeSet(comparator);
set.add(new Employee("张军", 21));
set.add(new Employee("张三", 22));
set.add(new Employee("李云", 23));
set.add(new Employee("王建安", 20));
set.add(new Employee("王建安", 15));
set.add(new Employee("王建安", 23));
Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}

}

class MyCmp implements  Comparator {




@Override
public int compare(Object o1, Object o2) {
return -o1.toString().compareTo(o2.toString());
}


}


如果是用汉字排序则用下面的接口

package cn.hncu.sort2;


import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;


import cn.hncu.sort2.Employee;




public class TreeSetDemo2 {


public static void main(String[] args) {
Comparator comparator =new MyCmp();
TreeSet set=new TreeSet(comparator);
set.add(new Employee("张军", 21));
set.add(new Employee("张三", 22));
set.add(new Employee("李云", 23));
set.add(new Employee("王建安", 20));
set.add(new Employee("王建安", 15));
set.add(new Employee("王建安", 23));
Iterator it=set.iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}


}

class MyCmp2 implements Comparator {


@Override
public int compare(Object o1, Object o2) {
Collator c=Collator.getInstance();
CollationKey key1=c.getCollationKey(o1.toString());
CollationKey key2=c.getCollationKey(o2.toString());
//将该 String 转换为一系列可以和其他 CollationKey 按位进行比较的位
return key1.compareTo(key2);
}


}


总结:

在JDK1.2中,14个类实现了Comparable接口,这些类中指定的自然排序如下:

1、BigDecimal,BigInteger,Byte,Double,Float,Integer,Long,Short按数字大小排序

2、Character 按Unicode值的数字大小排序

3、CollationKey 按语言环境敏感的字符串排序

4、Date 按年代排序

5、File 按系统特定的路径名的全限定字符的Unicode值排序

6、ObjectStreamField 按名字中字符的Unicode值排序

7、String 按字符串中字符Unicode值排序

如果一个类不能或不便于实现java.lang.Comparable接口,则可以用实现比较器Comparator接口的方法提供自己的排序行为。同样,如果缺省Comparable行为满足不了工程需要,也可以提供自己的Comparator
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值