java关于collections的排序,默认是升序,为什么我这段代码得到的结果是降序? 50
importjava.util.*;publicclassTestLink{publicstaticvoidmain(String[]args){Listl=newLin...
import java.util.*;
public class TestLink {
public static void main(String[] args) {
List l = new LinkedList();
l.add(new Bbbbbbbb("f1","f2"));
l.add(new Bbbbbbbb("b2","f3"));
l.add(new Bbbbbbbb("c1","f4"));
l.add(new Bbbbbbbb("a2","f1"));
Collections.sort(l);
System.out.println(l);
}
}
class Bbbbbbbb implements Comparable {
String a,b;
public Bbbbbbbb(String a,String b) {
this.a = a;
this.b = b;
}
public boolean equals(Object obj) {
if (obj instanceof Bbbbbbbb) {
Bbbbbbbb name = (Bbbbbbbb) obj;
return (a.equals(name.b))
&& (b.equals(name.b));
}
return super.equals(obj);
}
public int hashCode() {
return a.hashCode();
}
public String toString() {
return (a+b);
}
public int compareTo(Object o) {
Bbbbbbbb ob = (Bbbbbbbb)o;
return ob.a.compareTo(this.a) != 0 ?
ob.a.compareTo(this.a) : ob.b.compareTo(this.b);
}
} 展开
public class TestLink {
public static void main(String[] args) {
List l = new LinkedList();
l.add(new Bbbbbbbb("f1","f2"));
l.add(new Bbbbbbbb("b2","f3"));
l.add(new Bbbbbbbb("c1","f4"));
l.add(new Bbbbbbbb("a2","f1"));
Collections.sort(l);
System.out.println(l);
}
}
class Bbbbbbbb implements Comparable {
String a,b;
public Bbbbbbbb(String a,String b) {
this.a = a;
this.b = b;
}
public boolean equals(Object obj) {
if (obj instanceof Bbbbbbbb) {
Bbbbbbbb name = (Bbbbbbbb) obj;
return (a.equals(name.b))
&& (b.equals(name.b));
}
return super.equals(obj);
}
public int hashCode() {
return a.hashCode();
}
public String toString() {
return (a+b);
}
public int compareTo(Object o) {
Bbbbbbbb ob = (Bbbbbbbb)o;
return ob.a.compareTo(this.a) != 0 ?
ob.a.compareTo(this.a) : ob.b.compareTo(this.b);
}
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询