微信搜索lxw1234bigdata | 邀请体验:数阅–数据管理、OLAP分析与可视化平台 | 赞助作者:赞助作者

Java中ArrayList和HashMap的排序

编程语言 lxw1234@qq.com 19002℃ 0评论

关键字:ArrayList排序、HashMap排序、集合排序、升序、降序、key排序、value排序

最近在需求中经常需要对Java的集合,特别是Map根据key或者value进行升序降序排序,在这里整理记录一下,对java不熟,用于以后备查。

主要使用Collections提供的sort方法,另外也使用了TreeMap的排序特性。

直接看代码:

package com.lxw1234.test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * http://lxw1234.com
 * lxw的大数据田地
 * @author lxw1234.com
 *
 */
public class Test {

	public static void main(String[] args) {
		List list = new ArrayList();
		list.add("1");
		list.add("5");
		list.add("8");
		list.add("6");
		list.add("7");
		System.out.println("list before sort " + list + " ..");
		//默认升序排序
		Collections.sort(list);
		System.out.println("list after asc sort " + list + " ..");
		//降序排序
		Collections.sort(list, new Comparator(){
			@Override
			public int compare(String arg0, String arg1) {
				return arg1.compareTo(arg0);
			}
		});
		System.out.println("list after desc sort " + list + " ..");
		
		Map<String,Integer> map = new HashMap<String,Integer>();
		map.put("a", 2);
		map.put("e", 9);
		map.put("b", 5);
		map.put("d", 3);
		map.put("t", 7);
		
		System.out.println("map before sort " + map + " ..");
		
		//map按照key升序排序
		SortedMap<String,Integer> sortedMapByKey = new TreeMap<String,Integer>();
		sortedMapByKey.putAll(map);
		System.out.println("map after sort by key asc.." + sortedMapByKey);
		
		//map按照key降序排序
		sortedMapByKey = new TreeMap<String,Integer>(new Comparator() {
			@Override
			public int compare(String arg0, String arg1) {
				return arg1.compareTo(arg0);
			}
		});
		sortedMapByKey.putAll(map);
		System.out.println("map after sort by key desc.." + sortedMapByKey);
		
		//map按照value升序排序
		List<Map.Entry<String, Integer>> mapList =
			    new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
		Collections.sort(mapList,new Comparator<Map.Entry<String, Integer>> (){
			@Override
			public int compare(Map.Entry<String, Integer> o1,
					Map.Entry<String, Integer> o2) {
				//根据value升序排序
				return o1.getValue() - o2.getValue();
				//根据value降序排序
				//return o2.getValue() - o1.getValue();
			}
		});
		LinkedHashMap<String,Integer> sortedMapByValue = new LinkedHashMap<String,Integer>();
		for(int i=0;i<mapList.size();i++) {
			
			Map.Entry<String, Integer> x = mapList.get(i);
			sortedMapByValue.put(x.getKey(), x.getValue());
		}
		System.out.println("map after sort by value asc " + sortedMapByValue + " ..");
		
	}

}

程序的输出为:

list before sort [1, 5, 8, 6, 7] ..
list after asc sort [1, 5, 6, 7, 8] ..
list after desc sort [8, 7, 6, 5, 1] ..
map before sort {d=3, t=7, e=9, b=5, a=2} ..
map after sort by key asc..{a=2, b=5, d=3, e=9, t=7}
map after sort by key desc..{t=7, e=9, d=3, b=5, a=2}
map after sort by value asc {a=2, d=3, b=5, t=7, e=9} ..

以上程序仅供参考,未测试大数据量的性能问题。

 

如果觉得本博客对您有帮助,请 赞助作者

转载请注明:lxw的大数据田地 » Java中ArrayList和HashMap的排序

喜欢 (4)
分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址