Java集合操作——Map排序

一、Map介绍

在讲解Map排序之前,我们先来稍微了解下map。map是键值对的集合接口,它的实现类主要包括:HashMap,TreeMap,Hashtable以及LinkedHashMap等。其中这四者的区别如下(简单介绍):

HashMap:我们最常用的Map,它根据key的HashCode 值来存储数据,根据key可以直接获取它的Value,同时它具有很快的访问速度。HashMap最多只允许一条记录的key值为Null(多条会覆盖);允许多条记录的Value为 Null。非同步的。

TreeMap: 能够把它保存的记录根据key排序,默认是按升序排序,也可以指定排序的比较器,当用Iterator 遍历TreeMap时,得到的记录是排过序的。TreeMap不允许key的值为null。非同步的。

Hashtable: 与 HashMap类似,不同的是:key和value的值均不允许为null;它支持线程的同步,即任一时刻只有一个线程能写Hashtable,因此也导致了Hashtale在写入时会比较慢。

LinkedHashMap: 保存了记录的插入顺序,在用Iterator遍历LinkedHashMap时,先得到的记录肯定是先插入的.在遍历的时候会比HashMap慢。key和value均允许为空,非同步的。

二、Map排序

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

public class MapSort {

	public static void main(String[] args) {

		// 1.在使用treeMap排序,默认是根据key排序,升序排序
		Map<String, String> map1 = new TreeMap<String, String>();
		map1.put("c", "ccccc");
		map1.put("a", "aaaaa");
		map1.put("b", "bbbbb");
		map1.put("d", "ddddd");
		System.out.println("使用treeMap的默认排序方式");
		for (String key : map1.keySet()) {
			System.out.println(key + ":" + map1.get(key));
		}
		/**
		 * 2.treeMap排序,默认是根据key排序,如果我们需要改变排序方式,则需要使用比较器:Comparator。
		 *  Comparator可以对集合对象或者数组进行排序的比较器接口,实现该接口的public compare(T o1,To2)方法即可实现排序,
		 *	该方法主要是根据第一个参数o1,小于、等于或者大于o2分别返回负整数、0或者正整数。改变排序方式使其降序排序
		 */
		Map<String, String> map2 = new TreeMap<String, String>(new Comparator<String>() {
			public int compare(String o1, String o2) {

				return o2.compareTo(o1);// 降序排序
			}
		});
		map2.put("c", "ccccc");
		map2.put("a", "aaaaa");
		map2.put("b", "bbbbb");
		map2.put("d", "ddddd");
		System.out.println("改变treeMap的默认排序方式");
		for (String key : map2.keySet()) {
			System.out.println(key + ":" + map2.get(key));
		}
		/** 3.treeMap排序,默认是根据key排序,对value排序我们就需要借助于Collections的sort(List<T> list,
		 *   Comparator<? super T> c)方法,该方法根据指定比较器产生的顺序对指定列表进行排序。但是有一个前提条件,
		 *   那就是所有的元素都必须能够根据所提供的比较器来进行比较。   改变排序方式为根据value升序排序.
		 */
		Map<String, String> map3 = new TreeMap<String, String>();
		map3.put("d", "ddddd");
		map3.put("b", "bbbbb");
		map3.put("a", "aaaaa");
		map3.put("c", "ccccc");

		// 这里将map.entrySet()转换成list
		List<Map.Entry<String, String>> list3 = new ArrayList<Map.Entry<String, String>>(map3.entrySet());
		// 然后通过比较器来实现排序
		Collections.sort(list3, new Comparator<Map.Entry<String, String>>() {
			// 升序排序
			public int compare(Entry<String, String> o1, Entry<String, String> o2) {
				return o1.getValue().compareTo(o2.getValue());
			}

		});
		System.out.println("改变treeMap的默认排序方式为根据value升序排序");
		for (Map.Entry<String, String> mapping : list3) {
			System.out.println(mapping.getKey() + ":" + mapping.getValue());
		}
		// 4.HashMap排序,HashMap的值是没有顺序的,它是按照key的HashCode来实现的。下面的实现根据value升序排序
		Map<String, String> map4 = new HashMap<String, String>();
		map4.put("c", "ccccc");
		map4.put("a", "aaaaa");
		map4.put("b", "bbbbb");
		map4.put("d", "ddddd");

		List<Map.Entry<String, String>> list4 = new ArrayList<Map.Entry<String, String>>(map4.entrySet());
		Collections.sort(list4, new Comparator<Map.Entry<String, String>>() {
			// 升序排序
			public int compare(Entry<String, String> o1, Entry<String, String> o2) {
				return o1.getValue().compareTo(o2.getValue());
			}

		});
		System.out.println("HashMap排序,排序方式为根据value升序排序");
		for (Map.Entry<String, String> mapping : list4) {
			System.out.println(mapping.getKey() + ":" + mapping.getValue());
		}
	}

}

猜你喜欢

转载自blog.csdn.net/u013514928/article/details/79779294