MapUtil



import java.util.*;
import java.util.Map.Entry;




/**
 *

客户端基类:Map集合工具类


 *

Map集合处理工具类


 * @author liqs
 * @createDate 2016-8-9 15:26:47
 */
public class MapUtil  {

/**
* 遍历Map集合,将所有的Key值转为小写
* @author liqs
* @param Map
* @return Map
*/
public static  Map getMapLowerCase(Map map) {
 
Map newMap =  new HashMap();
Set set = map.keySet();

for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
newMap.put(key.toLowerCase(), value);
}


return newMap;
}


/**
* 遍历Map集合,将所有的Key值转为大写
* @author liqs
* @param Map
* @return Map
*/
public static  Map getMapUpperCase(Map map) {

Map newMap =  new HashMap();
Set set = map.keySet();

for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
newMap.put(key.toUpperCase(), value);
}


return newMap;
}




/**
* 遍历Map集合,替换单个Key值
* @author liqs
* @param Map 待替换的Map
* @param keyName  Key值
* @param renameKeyName 替换key值
* @return Map
*/
public static  Map getMapChangeKey(Map map,String keyName,String renameKeyName) {

Map newMap =  new HashMap();
Set set = map.keySet();

for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
if (keyName.trim().equals(key)) {
key=renameKeyName;
}
newMap.put(key.toLowerCase(), value);
}


return newMap;
}


/**
* 遍历Map集合,替换单个Key值
* @author liqs
* @param Map 待替换的Map
* @param keyName  Key值
* @param renameKeyName 替换key值
* @return Map  key没有转换小写
*/
public static  Map getMapChangeKeyNoLowerCase(Map map,String keyName,String renameKeyName) {

Map newMap =  new HashMap();
Set set = map.keySet();

for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
if (keyName.trim().equals(key)) {
key=renameKeyName;
}
newMap.put(key, value);
}


return newMap;
}

/**
* 遍历Map集合,替换单个Key值
* @author liqs
* @param Map 待替换的Map
* @param Map reMap
* @return Map
*/
public static  Map getMapChangeKey(Map map,Map reMap) {

Map newMap =  new HashMap();
Set set = map.keySet();

for (Iterator it = set.iterator(); it.hasNext();) {
String key = (String) it.next();
Object value = map.get(key);
String reName=reMap.get(key)+"".trim();
if (!reName.equals("null")) {
key=reName;
}
newMap.put(key.toLowerCase(), value);
}


return newMap;
}


/**
* 获取Map中的List
* @param Map
* @return  List
*/
public static List getList (Map map) {    

           List list = new ArrayList();
           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           Object type = (Object) entry.getValue();
           list.add(type);
}        
        return list;
}

/**
* 获取Map中指定的Map
* @param map
* @param key K值
* @return value值转Map
*/
public static Map getKeyMap (Map map,String key){

  Map newMap = new HashMap();


   List list = new ArrayList();
           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           if (key.equals(entry.getKey().toString())) {
           
           Object object =entry.getValue();
           if(object instanceof List || object instanceof ArrayList ){
            while (object instanceof List || object instanceof ArrayList ){
            list=(List) object;
            object=list.get(0);
            }
            list.add(object);
           }else {
                list.add(entry.getValue());
}
           
}
          
}
           if (list.size()>0) { 
             newMap=(Map) list.get(0);
}else {
newMap=null;
}


return newMap;
}

/**
* 获取Map中指定的Map
* @param map
* @param key K值
* @return value值转Map
*/
public static List  getListKeyList (Map map,String key){

   List list = new ArrayList();
           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           if (key.equals(entry.getKey().toString())) {
           
           Object object =entry.getValue();
           if(object instanceof List || object instanceof ArrayList ){
            while (object instanceof List || object instanceof ArrayList ){
            list=(List) object;
            object=list.get(0);
            }
        //    list.add(object);
           }else {
                list.add(entry.getValue());
}
           
}
          
}
    


return list;
}


/**
* 获取Map中指定的List
* @param map
* @param key K值
* @return value值转List
*/
public static List getMapList (Map map,String key){

   List list = new ArrayList();
           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           if (key.equals(entry.getKey().toString())) {
           Object object=entry.getValue();
           if(object instanceof HashMap ){
           list.add((Map)entry.getValue());
           }else if(object instanceof List){
           list=(List) entry.getValue();
           }
           
}
          
}
           if (list.size()==0) { 
           list=null;
           }


return list;
}
/**
* 获取Map中指定的List
* @param map
* @param key K值
* @return value值转List
*/
public static String getKeyString (Map map,String key){

   String value = null;
           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           if (key.equals(entry.getKey().toString())) {
           value = (String) entry.getValue();
           
}
          
}


return value;
}
/**
* 获取Map中指定的List中的第一个Map
* @param Map
* @return  Map
*/
public static Map getKeyList (Map map,String key) {    

           List list = new ArrayList();
   Map result = new HashMap();


           Iterator> it = map.entrySet().iterator();
           while (it.hasNext()) {
           Entry entry = it.next();
           if(key.equals(entry.getKey())){  
           Object object = entry.getValue();
           if (object instanceof List) {
           list = (List) object;
                       if (list.size()>0) {
                       result=(Map) list.get(0);
      }
}else {
result= (Map) object;
}
                   
                   
                }
          
}        
        return result;
}

/**
* 合并list中的Map为一个Map
* @param list  list 中 Map的K值是唯一的
* @return  Map String为小写
*/
public static Map getListToMap (List list) {    

Map result = new HashMap();
if (list!=null && list.size()>0) {
for (int i = 0; i < list.size(); i++) {
        Map map =(Map) list.get(i);
        Set set = map.keySet();
   
    for (Iterator it = set.iterator(); it.hasNext();) {
    String key = (String) it.next();
    Object value = map.get(key);
    result.put(key.toLowerCase(), value);
    }        
       }
}else{
System.out.println("传入list为null或list的大小小于1");
}
        
  
        return result;
}

}

你可能感兴趣的:(map,转xml)