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

你可能感兴趣的:(J#)