根据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();
}


}
0 0
原创粉丝点击