collection java 顺序,java Collection中的排序问题

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

1)list排序

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

publicstaticvoidsort(List list, Comparator<?superT> c) {

Object[] a = list.toArray();

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

ListIterator i = list.listIterator();

for(intj=0; j

i.next();

i.set(a[j]);

}

}

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

例如:

packagecom.fox;

/**

*

@author huangfox

*

@data 2012-7-5

*

@email huangfox009@126.com

*/

publicclassUserimplementsComparable {

privateString name;

privateintage;

publicUser() {

}

publicUser(String name,intage) {

super();

this.name = name;

this.age = age;

}

@Override

publicString toString() {

return"name:"+ name +",age:"+ age;

}

publicString getName() {

returnname;

}

publicvoidsetName(String name) {

this.name = name;

}

publicintgetAge() {

returnage;

}

publicvoidsetAge(intage) {

this.age = age;

}

@Override

publicintcompareTo(User o) {

if(o.age

return1;

elseif(o.age >this.age)

return-1;

else

return0;

}

/**

* @param args

*/

publicstaticvoidmain(String[] args) {

User u1 =newUser("fox",11);

User u2 =newUser("fox2",21);

System.out.println(u2.compareTo(u1));

}

}

排序:

// List us = new ArrayList();

List us =newLinkedList();

us.add(newUser("f5",12));

us.add(newUser("f2",22));

us.add(newUser("f3",2));

us.add(newUser("f4",14));

us.add(newUser("f5",32));

us.add(newUser("f4",12));

us.add(newUser("f7",17));

us.add(newUser("f8",52));

System.out.println(us.toString());

longbt = System.nanoTime();

Collections.sort(us,newComparator() {

@Override

publicintcompare(User o1, User o2) {

if(o1.getAge() < o2.getAge())

return-1;

elseif(o1.getAge() > o2.getAge())

return1;

else

returno1.getName().compareTo(o2.getName());

}

});

longet = 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 =newHashSet();

// 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(newUser("f5",12));

us.add(newUser("f2",22));

us.add(newUser("f3",2));

us.add(newUser("f4",14));

us.add(newUser("f5",32));

us.add(newUser("f4",12));

us.add(newUser("f7",17));

us.add(newUser("f8",52));

// set -> array

List list =newArrayList(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 us =newHashMap();

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

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

// 按照value排序

Set> ks = us.entrySet();

List> list =newArrayList>(

ks);

Collections.sort(list,newComparator>() {

@Override

publicintcompare(Entry o1,

Entry o2) {

if(o1.getValue() < o2.getValue())

return-1;

elseif(o1.getValue() > o2.getValue())

return1;

return0;

}

});

System.out.println(list);

将map的Entry提出成set结构,然后将set转成list,最后按照list进行排序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值