Map型別和String型別相互轉化
阿新 • • 發佈:2019-01-05
Map轉換成String /** * 方法名稱:transMapToString * 傳入引數:map * 返回值:String 形如 username'chenziwen^password'1234 */ public static String transMapToString(Map map){ java.util.Map.Entry entry; StringBuffer sb = new StringBuffer(); for(Iterator iterator = map.entrySet().iterator(); iterator.hasNext();) { entry = (java.util.Map.Entry)iterator.next(); sb.append(entry.getKey().toString()).append( "'" ).append(null==entry.getValue()?"": entry.getValue().toString()).append (iterator.hasNext() ? "^" : ""); } return sb.toString(); } String轉換成Map
/**
* 方法名稱:transStringToMap
* 傳入引數:mapString 形如 username'chenziwen^password'1234
* 返回值:Map
*/
public static Map transStringToMap(String mapString){
Map map = new HashMap();
java.util.StringTokenizer items;
for(StringTokenizer entrys = new StringTokenizer(mapString, "^");entrys.hasMoreTokens();
map.put(items.nextToken(), items.hasMoreTokens() ? ((Object) (items.nextToken())) : null))
items = new StringTokenizer(entrys.nextToken(), "'");
return map;
}
源文件:http://blog.csdn.net/ziwen00/article/details/7976144