排序
public static voidmain(String[] args) {
List demoList =newArrayList();
demoList.add(newDemo(1, 2));
demoList.add(newDemo(1, 3));
demoList.add(newDemo(1, 5));
demoList.add(newDemo(4, 2));
demoList.add(newDemo(1, 2));
demoList.add(newDemo(2, 4));
demoList.add(newDemo(1, 2));
demoList.add(newDemo(2, 5));
demoList.add(newDemo(1, 2));
Collections.sort(demoList,newComparator() {
public intcompare(Demo d1, Demo d2) {
if(d2.getAge() == d1.getAge()) {
returnd2.getSort() - d1.getSort();
}else{
returnd2.getAge() - d1.getAge();
}
};
});
// [[4:2], [2:5], [2:4], [1:5], [1:3], [1:2], [1:2], [1:2], [1:2]]
System.out.println(demoList);
Date q1 =newDate();
Calendar c = java.util.Calendar.getInstance();
c.add(Calendar.DATE, 1);
Date q2 = c.getTime();
System.out.println(q2.compareTo(q1));//1
}
删除
public static voidmain(String[] args) {
List coll =newArrayList();
coll.add(newDemo(1, 2));
coll.add(newDemo(1, 3));
coll.add(newDemo(4, 2));
coll.add(newDemo(2, 4));
coll.add(newDemo(1, 2));
coll.add(newDemo(2, 5));
coll.add(newDemo(1, 2));
for(Iterator it = coll.iterator(); it.hasNext();)
if(it.next().getAge() == 1)
it.remove();
System.out.println(coll);//[[4:2], [2:4], [2:5]]
Map map =newHashMap();
for(inti = 0; i
map.put(""+ i, coll.get(i));
}
for(Iterator> it = map.entrySet().iterator(); it.hasNext();) {
if(it.next().getValue().getAge()== 4)
it.remove();
}
System.out.println(map);//{2=[2:5], 1=[2:4]}
}
classDemo {
privateintsort;
privateintage;
publicDemo(intage,intsort) {
this.sort= sort;
this.age= age;
}
publicDemo() {}
publicintgetSort() {
returnsort;
}
publicintgetAge() {
returnage;
}
publicString toString() {
return"["+age+":"+sort+"]";
}
}