A:Comparable接口的使用举例:(自然排序)
1,像String,包装类等实现了Comparable接口,重写了comparable()方法,给出了比较两个对象大小的方式.
2,像String,包装类等重写comparableTo方法以后,进行了从小到大的排列.
String[] str={"f","e","d","c","b","a"}; Arrays.sort(str); System.out.println(Arrays.toString(str));
[a, b, c, d, e, f]
3,重写comparableTo(obj)的规则:
如果当前对象this大于形参对象obj,则返回正整数,
如果当前对象小于新参对象obj,则返回负整数,
如果当前对象this等于形参对象obj,则返回零.
package com.compareabletest; import java.util.Arrays; public class StringTest { public static void main(String[] args) { Goods[] goods =new Goods[4]; goods[0] = new Goods("jane", 12); goods[1] = new Goods("jane", 1); goods[2] = new Goods("jane", 2); goods[3] = new Goods("jane", 22); Arrays.sort(goods); System.out.println(Arrays.toString(goods)); } } class Goods implements Comparable { String name; int age; public Goods() { } @Override public String toString() { return "Goods{" + "name='" + name + ''' + ", age=" + age + '}'; } public Goods(String name, int age) { this.name = name; this.age = age; } @Override public int compareTo(Object o) { if (o instanceof Goods) { Goods goods = (Goods) o; if (this.age > goods.age) { return 1; } else if (this.age < goods.age) { return -1; } else return 0; } else { return -1; } } }
[Goods{name='jane', age=1}, Goods{name='jane', age=2}, Goods{name='jane', age=12}, Goods{name='jane', age=22}]
B:Comparetor接口的使用:定制排序
1,背景:
当元素的类型没有实现java.lang.Comparable接口而又不方便修改代码,或者实现了java.lang.Comparable接口的排序规则不适合当前的 *** 作,那么可以考虑使用Comparator的对象来排序.
2,重写compare(Object o1,Object o2)方法,比较o1,o2的大小;正数,o1大;负数o2大;0一样大.
实例代码:①将String数组逆序排列
package com.compareabletest; import java.util.Arrays; import java.util.Comparator; public class StringComparableTest { public static void main(String[] args) { String[] strs = {"a", "b", "c", "d"}; Arrays.sort(strs, new Comparator() { @Override public int compare(String o1, String o2) { return -o1.compareTo(o2); } }); System.out.println(Arrays.toString(strs)); } }
[a, b, c, d]
②自定义类
package com.compareabletest; import java.util.Arrays; import java.util.Comparator; public class GoodsComparableTest { public static void main(String[] args) { Goods[] goods = new Goods[4]; goods[0] = new Goods("a", 12); goods[1] = new Goods("c", 1); goods[2] = new Goods("b", 2); goods[3] = new Goods("a", 22); Arrays.sort(goods, new Comparator() { @Override public int compare(Object o1, Object o2) { if (o1 instanceof Goods && o2 instanceof Goods) { Goods g1 = (Goods) o1; Goods g2 = (Goods) o2; if (g1.getName().equals(g2.getName())) { return -Integer.compare(g1.age, g2.getAge()); } else return g1.getName().compareTo(g2.getName()); } else throw new RuntimeException("类型不匹配!"); } }); System.out.println(Arrays.toString(goods)); } } class Goods implements Comparable { String name; int age; public Goods() { } @Override public int compareTo(Object o) { if (o instanceof Goods) { Goods goods = (Goods) o; if (this.age > goods.age) { return 1; } else if (this.age < goods.age) { return -1; } else return 0; } else { return -1; } } public Goods(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } @Override public String toString() { return "Goods{" + "name='" + name + ''' + ", age=" + age + '}'; } public int getAge() { return age; } }
[Goods{name='a', age=22}, Goods{name='a', age=12}, Goods{name='b', age=2}, Goods{name='c', age=1}]
C:Comparable接口与Comparator接口的对比
Comparable接口一旦一定,保证Comparable接口实现类的对象在任何位置都可以比较大小
Comparator接口属于临时性的比较.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)