MapUtil

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);

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值