根据map的value值排序并输出
来源:互联网 发布:多功能工具箱软件 编辑:程序博客网 时间:2024/05/29 11:54
package test.com.whty.platform.modules.interfaces;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
HashMap<String,String> map=new HashMap<String,String>();
public void compareMap()
{
map.put("1", "3");
map.put("2", "4");
map.put("3", "1");
map.put("4", "2");
map.put("5", "5");
List<Map.Entry> entryLists=new ArrayList<Map.Entry>(map.entrySet());
Collections.sort(entryLists, new Comparator<Map.Entry>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2)
// TODO Auto-generated method stub
{
if(Integer.valueOf((String) o1.getValue())>Integer.valueOf((String) o2.getValue()))
{
return 1;
}
else if(o1.getValue().equals(o2.getValue()))
{
return 0;
}
else return -1;
}
});
Iterator<Entry> itr=entryLists.iterator();
while(itr.hasNext())
{
Map.Entry entry=(Entry) itr.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
public static void main(String[] args) {
Test test=new Test();
test.compareMap();
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Test {
HashMap<String,String> map=new HashMap<String,String>();
public void compareMap()
{
map.put("1", "3");
map.put("2", "4");
map.put("3", "1");
map.put("4", "2");
map.put("5", "5");
List<Map.Entry> entryLists=new ArrayList<Map.Entry>(map.entrySet());
Collections.sort(entryLists, new Comparator<Map.Entry>() {
@Override
public int compare(Map.Entry o1, Map.Entry o2)
// TODO Auto-generated method stub
{
if(Integer.valueOf((String) o1.getValue())>Integer.valueOf((String) o2.getValue()))
{
return 1;
}
else if(o1.getValue().equals(o2.getValue()))
{
return 0;
}
else return -1;
}
});
Iterator<Entry> itr=entryLists.iterator();
while(itr.hasNext())
{
Map.Entry entry=(Entry) itr.next();
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
public static void main(String[] args) {
Test test=new Test();
test.compareMap();
}
}
0 0
- 根据map的value值排序并输出
- 根据Map的value值进行排序
- 根据Map的value排序
- MAP根据value值排序
- Map中根据value排序---输出value最大时所对应的Key
- Map根据value排序
- Map 根据value 排序
- Map中根据value值的大小进行排序
- Map可根据value的值进行排序
- 根据Map的value进行排序
- 根据value值对map进行排序
- map根据其value值进行排序
- Map根据value进行排序
- 【C++】map根据value排序
- Map根据value进行排序
- Map根据value进行排序
- Map排序,获取map的第一值,根据value取key等操作(数据预处理)
- map的value值排序
- h5+ mui笔记
- Linux驱动学习总结转载博客地址记录
- 并行编程(1) - sum.msic.Unsafe 一
- Facebook 推出PHP 语言规范草案
- HDU1250Hat's Fibonacci大数
- 根据map的value值排序并输出
- 百度地图
- 电脑突然死机,编译报错dll缺少依赖项
- Andrioid网络编程入门介绍【入门必修】
- mysql忘记root密码解决办法
- LeetCode-43 Multiply Strings
- java验证1-200内哥德巴赫猜想正确性
- 在线配色网,方便实用!
- 预防高血脂