浅谈java Collection中的排序问题

网友投稿 253 2023-06-25


浅谈java Collection中的排序问题

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

1)list排序

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

public static void sort(List list, Comparator super T> c) {

Object[] a = list.toArray();

Arrays.sort(a, (Comparator)c);

ListIterator i = list.listIterator();

for (int j=0; j

i.next();

i.set(a[j]);

}

}

如果是自定义对象,需要实现Comparable接口使得对象自身就有“比较”的功能,当然我们也可以在外部使用Comparator来规定其排序。

例如:

package com.fox;

/**

* @author huangfox

* @desc

*/

public class User implements Comparable {

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 us = new ArrayList();

List us = new LinkedList();

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() {

@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 us = new HashSet();

// Set us = new TreeSet();

// Set us = new TreeSet(new Comparator() {

//

// @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));

RaHFNRQus.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 list = new ArrayList(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();

// Map us = new TreeMap();

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(us));

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

// 按照value排序

Set> ks = us.entrySet();

List> list = new ArrayList>(

ks);

Collections.sort(list, new Comparator>() {

@Override

public int compare(Entry o1,

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

i.next();

i.set(a[j]);

}

}

如果是自定义对象,需要实现Comparable接口使得对象自身就有“比较”的功能,当然我们也可以在外部使用Comparator来规定其排序。

例如:

package com.fox;

/**

* @author huangfox

* @desc

*/

public class User implements Comparable {

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 us = new ArrayList();

List us = new LinkedList();

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() {

@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 us = new HashSet();

// Set us = new TreeSet();

// Set us = new TreeSet(new Comparator() {

//

// @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));

RaHFNRQus.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 list = new ArrayList(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();

// Map us = new TreeMap();

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(us));

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

// 按照value排序

Set> ks = us.entrySet();

List> list = new ArrayList>(

ks);

Collections.sort(list, new Comparator>() {

@Override

public int compare(Entry o1,

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:学习SpringMVC——国际化+上传+下载详解
下一篇:详解Java豆瓣电影爬虫——小爬虫成长记(附源码)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~