MapUtil

/**
* 功能:遍歷map集合,打印key,value
* @param
*/
public static void iteratorMap(Map map) {
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry =(Entry) it.next();
Object key = entry.getKey();
Object value = entry.getValue();
System.out.println("[key="+key+",value="+value+"]");
}
}


/**
* 功能:傳入map與對象,判斷該map是否包含該key
* @param map
* @param key
* @return
*/
public static boolean isContainsKey(Map map,Object key){
return map.containsKey(key);
}


/**
* 功能:傳入map與對象,判斷該map是否包含該對象
* @param map
* @param value
* @return
*/
public static boolean isContainsObject(Map map,Object value){
return map.containsValue(value);
}


/**
* 功能:傳入map,如果map不包含鍵-值映射。則返回true
* @param map
* @return
*/
public static boolean isEmptyMap(Map map){
return map.isEmpty();
}


/**
* 功能:傳入兩個map,將oldMap複製到newMap中
* @param oldMap
* @param newMap
* @return
*/
public static Map paseOneMapToOther(Map oldMap,Map newMap){
newMap.putAll(oldMap);
return newMap;
}


/**
* 傳入map,返回map中建-值映射的數目
* @param map
* @return
*/
public static int returnMappingNumber (Map map) {
return map.size();
}


/**
* 功能:傳入Map,將Integer類型key轉化為String
* @param map
* @return
*/
public static Map chanMapKeyToString(Map<Integer,Object> map){
Iterator it = map.entrySet().iterator();
Map<String, Object> mp = new HashMap<String, Object>();
while(it.hasNext()){
Map.Entry<Integer,Object> entry = (Entry<Integer,Object>) it.next();
Integer keys = entry.getKey();
Object o = entry.getValue();
String key = String.valueOf(keys);
mp.put(key, o);
}
return mp;
}


/**
* 功能:傳入map和key,根據key取出value值
* @param map
* @param ob
* @return
*/
public static Object getMapValue(Map map,Object ob){
return map.get(ob);
}


/**
* 功能:傳入map和key,移除key及相應的映射
* @param map
* @param o
* @return
*/
public static Map removeMapKeyByValue(Map map,Object o){
map.remove(o);
return map;
}


/**
* 功能:傳入map及key數組,移除相應的映射
* @param map
* @param list
*/
public static Map removeMapKeyByKeyList(Map map,List list){
for(int i=0; i<list.size(); i++){
map.remove(list.get(i));
}
return map;
}


/**
* 功能:傳入map,按key值對value進行排序,逆排序,隨機排序
* @param map
* @param str
* @return
*/
public static Map sortBeyMapKey(Map map ,String str){
List keys = new ArrayList();
Map mp = new HashMap();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
keys.add(entry.getKey());
}

if(str.equals("sort")){//按key值進行排序(整形從小到大,字符按字母順序)
Collections.sort(keys);
}else if(str.equals("descending")){//按key值進行降序排序,整形從大到小字母逆向排序
Collections.sort(keys, Collections.reverseOrder());
}else{//按key值進行隨機排序
Collections.shuffle(keys);
}

for(int i=0; i<keys.size(); i++){
mp.put(keys.get(i), map.get(keys.get(i)));
}

return mp;
}


/**
* 功能:傳入map與對象,返回map中value與對象相等的key的list
* @param map
* @param o
* @return
*/
public static List getCommentValueKey(Map map,Object o){
List list = new ArrayList();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
if(o.equals(entry.getValue())){
list.add(entry.getKey());
}
}
return list;
}


/**
* 功能:傳入map,按key的最大值與最小值取出value
* @param map
* @return
*/
public static Object getMapMaxByKey(Map map,String str){
List list = new ArrayList();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
list.add(entry.getKey());
}

if(str.equals("max")){
return map.get(Collections.max(list));
}else{
return map.get(Collections.min(list));
}
}


/**
* 功能:替換map中所有指定value的值
* @param map
* @param oldValue
* @param newValue
* @return
*/
public static Map replaceMapValue(Map map,String oldValue,String newValue){
List list = new ArrayList();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
if(oldValue.equals(entry.getValue())){
list.add(entry.getKey());
}
}

for(int i=0; i<list.size(); i++){
map.remove(list.get(i));
}

Collections.replaceAll(list, oldValue, newValue);
for(int j=0; j<list.size();j++){
map.put(list.get(j), newValue);
}

return map;
}


/**
* 功能:替換map中所有value值
* @param map
* @param newValue
* @return
*/
public static Map replaceAllMapValue(Map map,String newValue){
List list = new ArrayList();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
Map.Entry entry = (Entry) it.next();
list.add(entry.getKey());
}

for(int i=0; i<list.size(); i++){
map.remove(list.get(i));
}

for(int j=0; j<list.size(); j++){
map.put(list.get(j), newValue);
}
return map;
}


/**
* 功能:傳入map,返回map的value數組
* @param map
* @return
*/
public static List returnMapValueList(Map map){
List list = new ArrayList();
Collection ct = map.values();
Iterator it = ct.iterator();
while(it.hasNext()){
Object value = it.next();
list.add(value);
}

return list;
}


/**
* 功能:傳入map,返回map的key數組
* @param map
* @return
*/
public static List returnMapKeysList(Map map){
List list = new ArrayList();
Set set = map.keySet();
Iterator it = set.iterator();
while(it.hasNext()){
Object key = it.next();
list.add(key);
}

return list;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值