导航:首页 > 源码编译 > map排序算法

map排序算法

发布时间:2023-06-30 11:21:49

java中list里面存放map,根据map中的某两个个字段进行排序

packagecom.compare.test;

importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;

publicclassMain{
publicstaticvoidmain(String[]args){
MainmainTest=newMain();
mainTest.sortMap();
}

publicvoidsortMap(){
List<Map<Integer,Double>>maps=newArrayList<Map<Integer,Double>>();
for(inti=0;i<10;i++){
HashMap<Integer,Double>map=newHashMap<Integer,Double>();
for(intj=0;j<2;j++){
map.put(j,Math.random());
}
maps.add(map);
}

for(Map<Integer,Double>map:maps){
System.out.println(getValue(map));
}
System.out.println("************************");
Map<Integer,Double>currentMap;
for(inti=0;i<maps.size()-1;i++){
for(intj=0;j<maps.size()-i-1;j++){
if(getValue(maps.get(j))>getValue(maps.get(j+1))){
currentMap=maps.get(j+1);
maps.set(j+1,maps.get(j));
maps.set(j,currentMap);
}
}
}

for(Map<Integer,Double>map:maps){
System.out.println(getValue(map));
}}

publicDoublegetValue(Map<Integer,Double>currentMap){
returncurrentMap.get(0)+currentMap.get(1);
}
}

我采用最简单的排序大数沉底。而且getValue
方法你可以自己实现,决定使用哪几个进行排序。(我们有进行key值不存在的判断)

阅读全文

与map排序算法相关的资料

热点内容
我的世界如何在服务器里设置货币 浏览:591
酷猫系统如何安装app 浏览:636
邮寄服务器是干什么用 浏览:159
解除电脑加密文件夹 浏览:358
androidcheckbox组 浏览:546
linux在线安装软件 浏览:823
如何设置手机安卓版 浏览:285
简历pdfword 浏览:123
锋云视频服务器网关设置 浏览:162
linux服务器如何查看网卡型号 浏览:142
加密相册误删了怎么恢复 浏览:380
安卓代练通怎么下载 浏览:518
知道域名如何查询服务器 浏览:907
方舟手游怎么才能进服务器 浏览:289
抖音算法自动爆音 浏览:24
linux修改网卡配置 浏览:913
云服务器和本地服务器数据 浏览:843
在家如何创业python 浏览:225
编译原理好课 浏览:718
python中实数的表示 浏览:372