服务器之家

服务器之家 > 正文

java对ArrayList排序代码示例

时间:2020-01-20 11:30     来源/作者:sunmenggmail

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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的用法和上述的排序类似,有三种方法:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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());
    }//从大到小输出
  }
}

注意,

?
1
2
3
for (WordFreq wf : pq) {
System.out.println(wf);
}

并不保证遍历的有序

如果List<String> ls 进行排序的话,不需要写Comparator, 因为String本身有compareTo的实现。

标签:

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
返回顶部