---
title: "MapUtil"
author: "JavaBrain"
date: "2014-08-12T00:27:28Z"
tags: []
categories: ["编程语言"]
slug: "maputil"
draft: false
lastmod: "2014-08-12T00:27:28Z"
id: 2102672
url: "https://cai-bird.iteye.com/blog/2102672"
---

package com.common.util.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class MapUtil {

	private static final Map initMap;
	static {
		//初始化静态Map，且不能修改。
		Map aMap  = new HashMap();
		aMap.put(1, "one");
		aMap.put(3, "three");
		aMap.put(5, "five");
		aMap.put(2, "two");
//		aMap.put(6, null);
		initMap = Collections.unmodifiableMap(aMap);
	}

	/**
	 * 取得key List 无序
	 * @param map
	 * @return
	 */
	public static List getMapKeyList(Map map) {

		return new ArrayList(map.keySet());
		 

	}
	
	/**
	 * 取得value List 无序
	 * @param map
	 * @return
	 */
	public static List getMapValueList(Map map) {

		return new ArrayList(map.values());
	}
	
	/**
	 * 取得entry List 无序
	 * @param map
	 * @return
	 */
	public static List  getMapEntryList(Map map) {

		return new ArrayList(map.entrySet());
	}
	
	/**
	 * 取得entry List key有序
	 * 注：map 不要 null key
	 * @param map
	 * @return
	 */
	public static List getMapEntryListSortKey(Map map){
		List entryList = getMapEntryList(map);
		Collections.sort(entryList, new Comparator(){

			public int compare(Object o1, Object o2) {
				Entry e1 = (Entry)o1;
				Entry e2 = (Entry)o2;
				return e1.getKey().toString().compareTo(e2.getKey().toString());
			} 
		});
		return entryList;
	}
	
	/**
	 * 取得entry List value有序
	 * 注：map 不要 null value
	 * @param map
	 * @return
	 */
	public static List getMapEntryListSortValue(Map map){
		List entryList = getMapEntryList(map);
		Collections.sort(entryList, new Comparator(){

			public int compare(Object o1, Object o2) {
				Entry e1 = (Entry)o1;
				Entry e2 = (Entry)o2;
				return e1.getValue().toString().compareTo(e2.getValue().toString());
			} 
		});
		return entryList;
	}
	
	
	
	/**
	 * 复制Map, 2个map同步更新
	 * @param sourceMap
	 * @return
	 */
	public static Map copyMap(Map sourceMap){
		return Collections.synchronizedMap(sourceMap);
	}
	
	/**
	 * 清空Map，不能用。若要用，则new HashMap();
	 */
	public static Map emptyMap(Map map){
		return Collections.emptyMap();
	}
	
	
	
	
	public static void displayList(List list){
	    Iterator it = list.iterator();
	    while(it.hasNext()){
	    	System.out.println(it.next().toString());
	    }
	}
	
	
	public static void displayMap(Map map){
		Iterator it = map.entrySet().iterator();
		while(it.hasNext()){
			Entry entry = (Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			
			System.out.println(key + "====" + value);
		}
	}
	
	/**
	 * 链接有序Map
	 * @return
	 */
	public static Map getLinkedHashMap(){
		Map map = new LinkedHashMap();
//		map.put(4, "three");
//		map.put(1, "one");
//		map.put(2, "two");
//		map.put(3, "three");
		return map;
	}
	
	/**
	 * 同步，线程安全的map
	 * @return
	 */
	public static Map getHashtable(){
		Map map = new Hashtable();
		
		return map;
	}
	
	
	

	public static void main(String[] args) {
		
//		displayMap(MapUtil.initMap);
		
//		List list = getMapKeyList(MapUtil.initMap);		
//		displayList(list);
//		
//		List list2 = getMapValueList(MapUtil.initMap);		
//		displayList(list2);
//		
//		List list3 = getMapEntryList(MapUtil.initMap);		
//		displayList(list3);
//		
//		System.out.println("========");
//		
//		List list4 = getMapEntryListSortKey(MapUtil.initMap);		
//		displayList(list4);
//		
//		System.out.println("========");
//		
//		List list5 = getMapEntryListSortValue(MapUtil.initMap);		
//		displayList(list5);
		
		Map linkedHashMap = getLinkedHashMap();
		displayMap(linkedHashMap);
		
		
		
		
//		System.out.println("========");
//		Map m1 = copyMap(MapUtil.initMap);
//		displayMap(m1);
//		System.out.println("========");
//		m1.put("2", "four");
//		displayMap(m1);
//		
//		System.out.println("========");
//		displayMap(MapUtil.initMap);

	}

}
