欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java对ArrayList排序代码示例

程序员文章站 2024-03-06 22:04:08
不废话了,直接给大家贴代码了。 class term { string str; int id; public term(string...

不废话了,直接给大家贴代码了。

class term { 
  string str; 
  int id;  
  public term(string str, int id) { 
    this.str = str; 
    this.id = id; 
  } 
  public string tostring() { 
    return str+" "+id; 
  } 
} 
class sterm implements comparable{ 
  string str; 
  int id; 
  public sterm(string str, int id) { 
    this.str = str; 
    this.id = id; 
  } 
  public int compareto(object o) { 
    return ((sterm)o).id - id; 
  } 
  public string tostring() { 
    return str+" "+id; 
  } 
} 
//method1: explicit implements comparator 
class termcomparator implements comparator { 
  public int compare (object o1, object o2) { 
    return ((term)o1).id - ((term)o2).id; 
  } 
} 
public class t1 { 
  /** 
   * @param args 
   */ 
  public static void main(string[] args) { 
    // todo auto-generated method stub 
//   arraylist<integer> arr = new arraylist<integer>( arrays.aslist(3,1,3,7,8,0)); 
//    
//   collections.sort(arr, new comparator(){ 
//      
//     public int compare(object o1, object o2){ 
//       return new double((integer)o1).compareto(new double ((integer)o2)); 
//     } 
//   }); 
    //method1 
    list<term> ls = new arraylist<term>(); 
    ls.add(new term("a",1)); 
    ls.add(new term("b",5)); 
    ls.add(new term("c",2)); 
    ls.add(new term("d",2)); 
    ls.add(new term("e",3)); 
    ls.add(new term("f",0)); 
    collections.sort(ls, new termcomparator()); 
    system.out.println(ls);//[f 0, a 1, c 2, d 2, e 3, b 5] 
    //method2: anonymous implements 
    collections.sort(ls, new comparator(){ 
      public int compare(object o1, object o2){ 
        return ((term)o2).id - ((term)o1).id; 
      } 
    }); 
    system.out.println(ls);//[b 5, e 3, c 2, d 2, a 1, f 0] 
    //method3:instantiate a comparator template 
    comparator<term> termcmp = new comparator<term>() { 
      public int compare(term t1, term t2) { 
        return t1.id - t2.id; 
      } 
    }; 
    collections.sort(ls, termcmp); 
    system.out.println(ls);//[f 0, a 1, c 2, d 2, e 3, b 5] 
    //method4:element implements comparable 
    list<sterm> lss = new arraylist<sterm>(); 
    lss.add(new sterm("a",1)); 
    lss.add(new sterm("b",5)); 
    lss.add(new sterm("c",2)); 
    lss.add(new sterm("d",2)); 
    lss.add(new sterm("e",3)); 
    lss.add(new sterm("f",0)); 
    collections.sort(lss); 
    system.out.println(lss);//[b 5, e 3, c 2, d 2, a 1, f 0] 
  } 
} 

prioriyqueue的用法和上述的排序类似,有三种方法:

class wordfreq implements comparable{ 
    public string wd; 
    public int freq; 
    public wordfreq(string wd, int freq) { 
      this.wd = wd; 
      this.freq = freq; 
    } 
    public int compareto(object o) { 
      return ((wordfreq)o).freq - freq; 
    } 
    public string tostring() { 
      return wd+" "+freq; 
    } 
  } 
public class testt { 
  public static void main(string[] args) { 
    // todo auto-generated method stub 
    priorityqueue<wordfreq> pq = new priorityqueue<wordfreq>(); 
    pq.offer(new wordfreq("aaa", 3)); 
    pq.offer(new wordfreq("bbb", 4)); 
    pq.offer(new wordfreq("ccc",1)); 
    while(pq.peek() != null) { 
      system.out.println(pq.poll()); 
    }//从大到小输出 
  } 
} 

注意,

for (wordfreq wf : pq) {
system.out.println(wf);
}

并不保证遍历的有序

如果list<string> ls 进行排序的话,不需要写comparator, 因为string本身有compareto的实现。