激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 淺談java Collection中的排序問題

淺談java Collection中的排序問題

2020-07-12 15:37jingxian JAVA教程

下面小編就為大家帶來一篇淺談java Collection中的排序問題。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

這里討論list、set、map的排序,包括按照map的value進行排序。

1)list排序

list排序可以直接采用Collections的sort方法,也可以使用Arrays的sort方法,歸根結底Collections就是調用Arrays的sort方法。

java" id="highlighter_524320">
?
1
2
3
4
5
6
7
8
9
public static <T> void sort(List<T> list, Comparator<? super T> c) {
  Object[] a = list.toArray();
  Arrays.sort(a, (Comparator)c);
  ListIterator i = list.listIterator();
  for (int j=0; j<a.length; j++) {
    i.next();
    i.set(a[j]);
  }
  }

如果是自定義對象,需要實現Comparable接口使得對象自身就有“比較”的功能,當然我們也可以在外部使用Comparator來規定其排序。

例如:

?
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
package com.fox;
 
/**
 * @author huangfox
 * @desc
 */
public class User implements Comparable<User> {
 
  private String name;
  private int age;
 
  public User() {
  }
 
  public User(String name, int age) {
    super();
    this.name = name;
    this.age = age;
  }
 
  @Override
  public String toString() {
    return "name:" + name + ",age:" + age;
  }
 
  public String getName() {
    return name;
  }
 
  public void setName(String name) {
    this.name = name;
  }
 
  public int getAge() {
    return age;
  }
 
  public void setAge(int age) {
    this.age = age;
  }
 
  @Override
  public int compareTo(User o) {
    if (o.age < this.age)
      return 1;
    else if (o.age > this.age)
      return -1;
    else
      return 0;
  }
 
  /**
   * @param args
   */
  public static void main(String[] args) {
    User u1 = new User("fox", 11);
    User u2 = new User("fox2", 21);
    System.out.println(u2.compareTo(u1));
  }
 
}

排序:

?
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
// List<User> us = new ArrayList<User>();
    List<User> us = new LinkedList<User>();
    us.add(new User("f5", 12));
    us.add(new User("f2", 22));
    us.add(new User("f3", 2));
    us.add(new User("f4", 14));
    us.add(new User("f5", 32));
    us.add(new User("f4", 12));
    us.add(new User("f7", 17));
    us.add(new User("f8", 52));
    System.out.println(us.toString());
    long bt = System.nanoTime();
    Collections.sort(us, new Comparator<User>() {
 
      @Override
      public int compare(User o1, User o2) {
        if (o1.getAge() < o2.getAge())
          return -1;
        else if (o1.getAge() > o2.getAge())
          return 1;
        else
          return o1.getName().compareTo(o2.getName());
      }
    });
    long et = System.nanoTime();
    System.out.println(et - bt);
    System.out.println(us.toString());

當然這里可以直接Collections.sort(us),這里用Comparator對User自身的比較方法compareTo做了一點點優化(對相同年齡的人根據用戶名排序,String的排序)。

簡單提一下,Arrays的排序采用的是插入排序和歸并排序,當數組長度較小時直接插入排序。

2)set排序

set包括HashSet和TreeSet,HashSet是基于HashMap的,TreeSet是基于TreeMap的。

TreeMap是用紅黑樹實現,天然就具有排序功能,“天然就具有排序功能”是指它擁有升序、降序的迭代器。

那么HashSet怎么排序呢?我們可以將HashSet轉成List,然后用List進行排序。

例如:

?
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
Set<User> us = new HashSet<User>();
    // Set<User> us = new TreeSet<User>();
    // Set<User> us = new TreeSet<User>(new Comparator<User>() {
    //
    // @Override
    // public int compare(User o1, User o2) {
    // if (o1.getAge() < o2.getAge())
    // return -1;
    // else if (o1.getAge() > o2.getAge())
    // return 1;
    // else
    // return o1.getName().compareTo(o2.getName());
    // }
    // });
    us.add(new User("f5", 12));
    us.add(new User("f2", 22));
    us.add(new User("f3", 2));
    us.add(new User("f4", 14));
    us.add(new User("f5", 32));
    us.add(new User("f4", 12));
    us.add(new User("f7", 17));
    us.add(new User("f8", 52));
    // set -> array
    List<User> list = new ArrayList<User>(us);
    System.out.println(list);
    Collections.sort(list);
    System.out.println(list);

也可以將HashSet轉換成數組用Arrays排序。

3)map排序

map包括HashMap和TreeMap,上面已經提過,TreeMap用紅黑樹實現,天然具有排序功能。

那么HashMap怎么按“key”排序呢?方法很簡單,用HashMap來構造一個TreeMap。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
Map<String, Integer> us = new HashMap<String, Integer>();
    // Map<String, Integer> us = new TreeMap<String, Integer>();
    us.put("f1", 12);
    us.put("f2", 13);
    us.put("f5", 22);
    us.put("f4", 42);
    us.put("f3", 15);
    us.put("f8", 21);
    us.put("f6", 123);
    us.put("f7", 1);
    us.put("f9", 19);
    System.out.println(us.toString());
    System.out.println(new TreeMap<String, Integer>(us));

怎么按照“value”進行排序?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 按照value排序
    Set<Entry<String, Integer>> ks = us.entrySet();
    List<Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
        ks);
    Collections.sort(list, new Comparator<Entry<String, Integer>>() {
 
      @Override
      public int compare(Entry<String, Integer> o1,
          Entry<String, Integer> o2) {
        if (o1.getValue() < o2.getValue())
          return -1;
        else if (o1.getValue() > o2.getValue())
          return 1;
        return 0;
      }
    });
    System.out.println(list);

將map的Entry提出成set結構,然后將set轉成list,最后按照list進行排序。

以上這篇淺談java Collection中的排序問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩av一区三区 | 中文字幕 在线观看 | 欧美另类综合 | 精品人成| 国产精品美女一区二区 | 久久久久久久.comav | 国产一区二区精彩视频 | 最新欧美精品一区二区三区 | 天天鲁在线视频免费观看 | 日韩色视频在线观看 | 黄色大片免费网站 | 免费在线观看成人av | 美女黄色影院 | 欧美成人一区二区视频 | 免费观看三级毛片 | 成人短视频在线播放 | 康妮卡特欧美精品一区 | 一级一片免费 | 日韩视频一区二区在线观看 | 性色av一区二区三区四区 | 蜜桃av鲁一鲁一鲁一鲁 | 福利在线免费视频 | 成年免费看 | 4399一级成人毛片 | 天堂亚洲一区 | 一区二区三区欧美在线 | 暖暖免费观看高清完整版电影 | 免费视频一区 | 成人国产免费观看 | 久久久久久中文字幕 | 蜜桃传媒视频麻豆第一区免费观看 | 九九热精品免费 | av不卡毛片 | 蜜桃av鲁一鲁一鲁一鲁 | 精品国产一区二区三区四区阿崩 | 久久精品一区二区三 | 中国3xxxx| 小雪奶水翁胀公吸小说最新章节 | 久久久久久久久久久久久九 | 高清视频91 | 欧美激情猛片xxxⅹ大3 |