/*Comparable 是java.lang中的一个接口,所以是默认导入的,不需要显示的导入. *如果你先直接在本类中实现排序,那么可以直接实现该接口(例如:public class ComparaUser implements Comparable<ComparaUser>) *然后复写 int compare(Object o) 提醒:如果在实现接口的时候指明了相应的类型,则这里的Object可以换成对应的类型,避免了强转. int compareTo(ComparaUser user…
Comparable: 一个类实现了Camparable接口则表明这个类的对象之间是可以相互比较的,这个类对象组成的集合就可以直接使用sort方法排序,sort方法调用compareTo()方法里定义的方法进行排序,Comparable的缺点是只能按一种方式排序 package object; import java.util.*; public class Comparator1 implements Comparable<Comparator1>{ public int i; public…
Similarities:Both are custom ways to compare two objects.Both return an int describing the relationship between two objects. Differences: Comparator compare() is from the Comparator interface. A comparator object is capable of comparing two differe…
1,两种接口的展示 下面的程序是两个类各自实现了Comparable接口.Comparator接口 package com.cnblogs.mufasa.Solution; import java.util.Comparator; class Solution1 implements Comparable<Solution1>{ int val; public Solution1(int val){ this.val=val; } @Override public int compareTo(…
一.comparator 接口继承 public class ComparatorTest { /** * @param args */ public static void main(String[] args) { // ----写程序 List<Step> list=new ArrayList<Step>(); for (int i = 10; i < 12; i++) { list.add(new Step(i)); } for (int i = 5; i <…