当前位置: 移动技术网 > IT编程>开发语言>Java > 浅谈java Collection中的排序问题

浅谈java Collection中的排序问题

2019年07月22日  | 移动技术网IT编程  | 我要评论

这里讨论list、set、map的排序,包括按照map的value进行排序。

1)list排序

list排序可以直接采用collections的sort方法,也可以使用arrays的sort方法,归根结底collections就是调用arrays的sort方法。

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来规定其排序。

例如:

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)); 
  } 
 
}

排序:

// 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进行排序。

例如:

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。

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”进行排序?

// 按照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中的排序问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网