package toolClass.Collections.Demo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import org.w3c.dom.stylesheets.LinkStyle;
import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;import sun.security.action.GetBooleanAction;
import toolClass.Comparator.Demo.ComparatorByLength;
public class ColletctionsDemo {
public static void main(String[] args) {
/*
* Collections 是集合框架的工具类 里面的方法都是静态的
*/
demo_1();
}
public static void demo_1() {
java.util.List<String>list=new ArrayList<String>();
list.add("abcde");
list.add("cba");
list.add("aa");
list.add("zzz");
list.add("nbaa");
System.out.println(list);
//对list 集合进行指定顺序的排序
//Collections.sort(list);
//mySort(list);
// mySort(list, new ComparatorByLength());
Collections.sort(list, new ComparatorByLength());
System.out.println(list);
}
public static <T>void mySort(java.util.List<T> list ,Comparator< ? super T> comp) {
for (int i = 0; i < list.size()-1; i++) {
for(int j=i+1;j< list.size();j++) {
if(comp.compare(list.get(i),list.get(j))>0) {
/* T temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);*/
Collections.swap(list, i, j);
}
}
}
}
/*public static <T extends Comparable<? super T>>void mySort(java.util.List<T> list) {
for (int i = 0; i < list.size()-1; i++) {
for(int j=i+1;j<list.size();j++) {
if(list.get(i).compareTo(list.get(j))>0) {
T temp=list.get(i);
list.set(i,list.get(j));
list.set(j,temp);
Collections.swap(list, i, j);
}
}
}
}*/
}
Java Collections工具类
最新推荐文章于 2024-08-16 20:16:07 发布