容器中元素的排序 Set,Map ,List


package collectionSort;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.Map.Entry;

/*
* Map : 实现按值排序
* Set : 实现按其中的元素的长度从大到小排序
* List: 实现按其中的元素的长度从小到大排序
*
*/

public class HashMapSort {
private Map<String , Double> hashMap ;
private Set<String> set;
private List<String> list;

public HashMapSort(){
this.hashMap = new HashMap<String,Double>();
this.hashMap.put("1", 2.0);
this.hashMap.put("2", 1.0);
this.hashMap.put("3", 3.0);
this.hashMap.put("4", 5.0);
this.hashMap.put("5", 33.0);
this.hashMap.put("6", 344.0);
this.hashMap.put("7", 36.0);
this.hashMap.put("8", 33.0);
this.hashMap.put("9", 322.0);


this.set = new HashSet<String>();
this.set.add("");
this.set.add("#");
this.set.add("##");
this.set.add("###");
this.set.add("####");
this.set.add("#####");
this.set.add("######");
this.set.add("#######");
this.set.add("########");


this.list = new ArrayList<String>();
this.list.add("");
this.list.add("#");
this.list.add("##");
this.list.add("###");
this.list.add("####");
this.list.add("#####");
this.list.add("######");
this.list.add("#######");
this.list.add("########");

}

@SuppressWarnings("unchecked")
public static void main(String[] args) {
HashMapSort hashMapSort = new HashMapSort();
Set<Entry<String,Double>> entrySet = hashMapSort.hashMap.entrySet();
Set<Entry<String,Double>> treeSet = new TreeSet(new Comparator<Entry<String,Double>>(){

public int compare( Entry<String , Double> o1, Entry<String,Double> o2) {
return (int) (o2.getValue()-o1.getValue());
}

});


for(Entry<String , Double> entry: entrySet){
treeSet.add(entry);
}


System.out.println(treeSet);


System.out.println("+++++++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++");
Set<String> treeSet1 = new TreeSet(new Comparator<String>(){

public int compare(String o1, String o2) {

return o1.length()-o2.length();
}

});

for(String s: hashMapSort.set){
treeSet1.add(s);
}
System.out.println(treeSet1);

System.out.println("+++++++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++");


Collections.sort(hashMapSort.list,new Comparator<String>(){

public int compare(String o1, String o2) {
return o2.length() - o1.length();
}

});

System.out.println(hashMapSort.list);
}
}




运行结果:
[quote]
[6=344.0, 9=322.0, 7=36.0, 5=33.0, 4=5.0, 3=3.0, 1=2.0, 2=1.0]
+++++++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
[, #, ##, ###, ####, #####, ######, #######, ########]
+++++++++++++++++++++++++++++++++++++++分割线+++++++++++++++++++++++++++++++++++
[########, #######, ######, #####, ####, ###, ##, #, ]
[/quote]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值