JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类
- <pre name="code" class="java"></pre><pre name="code" class="java"><pre name="code" class="java">import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- public class Utils {
- /**
- * 定义分割常量 (#在集合中的含义是每个元素的分割,|主要用于map类型的集合用于key与value中的分割)
- */
- private static final String SEP1 = "#";
- private static final String SEP2 = "|";
- /**
- * List转换String
- *
- * @param list
- * :需要转换的List
- * @return String转换后的字符串
- */
- public static String ListToString(List<?> list) {
- StringBuffer sb = new StringBuffer();
- if (list != null && list.size() > 0) {
- for (int i = 0; i < list.size(); i++) {
- if (list.get(i) == null || list.get(i) == "") {
- continue;
- }
- // 如果值是list类型则调用自己
- if (list.get(i) instanceof List) {
- sb.append(ListToString((List<?>) list.get(i)));
- sb.append(SEP1);
- } else if (list.get(i) instanceof Map) {
- sb.append(MapToString((Map<?, ?>) list.get(i)));
- sb.append(SEP1);
- } else {
- sb.append(list.get(i));
- sb.append(SEP1);
- }
- }
- }
- return "L" + EspUtils.EncodeBase64(sb.toString());
- }
- /**
- * Map转换String
- *
- * @param map
- * :需要转换的Map
- * @return String转换后的字符串
- */
- public static String MapToString(Map<?, ?> map) {
- StringBuffer sb = new StringBuffer();
- // 遍历map
- for (Object obj : map.keySet()) {
- if (obj == null) {
- continue;
- }
- Object key = obj;
- Object value = map.get(key);
- if (value instanceof List<?>) {
- sb.append(key.toString() + SEP1 + ListToString((List<?>) value));
- sb.append(SEP2);
- } else if (value instanceof Map<?, ?>) {
- sb.append(key.toString() + SEP1
- + MapToString((Map<?, ?>) value));
- sb.append(SEP2);
- } else {
- sb.append(key.toString() + SEP1 + value.toString());
- sb.append(SEP2);
- }
- }
- return "M" + EspUtils.EncodeBase64(sb.toString());
- }
- /**
- * String转换Map
- *
- * @param mapText
- * :需要转换的字符串
- * @param KeySeparator
- * :字符串中的分隔符每一个key与value中的分割
- * @param ElementSeparator
- * :字符串中每个元素的分割
- * @return Map<?,?>
- */
- public static Map<String, Object> StringToMap(String mapText) {
- if (mapText == null || mapText.equals("")) {
- return null;
- }
- mapText = mapText.substring(1);
- mapText = EspUtils.DecodeBase64(mapText);
- Map<String, Object> map = new HashMap<String, Object>();
- String[] text = mapText.split("\\" + SEP2); // 转换为数组
- for (String str : text) {
- String[] keyText = str.split(SEP1); // 转换key与value的数组
- if (keyText.length < 1) {
- continue;
- }
- String key = keyText[0]; // key
- String value = keyText[1]; // value
- if (value.charAt(0) == 'M') {
- Map<?, ?> map1 = StringToMap(value);
- map.put(key, map1);
- } else if (value.charAt(0) == 'L') {
- List<?> list = StringToList(value);
- map.put(key, list);
- } else {
- map.put(key, value);
- }
- }
- return map;
- }
- /**
- * String转换List
- *
- * @param listText
- * :需要转换的文本
- * @return List<?>
- */
- public static List<Object> StringToList(String listText) {
- if (listText == null || listText.equals("")) {
- return null;
- }
- listText = listText.substring(1);
- listText = EspUtils.DecodeBase64(listText);
- List<Object> list = new ArrayList<Object>();
- String[] text = listText.split(SEP1);
- for (String str : text) {
- if (str.charAt(0) == 'M') {
- Map<?, ?> map = StringToMap(str);
- list.add(map);
- } else if (str.charAt(0) == 'L') {
- List<?> lists = StringToList(str);
- list.add(lists);
- } else {
- list.add(str);
- }
- }
- return list;
- }
- }
- </pre><br>
- <pre></pre>
- 最终版本
- <pre></pre>
- <pre name="code" class="java">运行结果:<img src="http://hi.csdn.net/attachment/201109/29/0_1317259591S9xc.gif" alt=""></pre>
- <pre></pre>
- </pre>
JAVA中List转换String,String转换List,Map转换String,String转换Map之间的转换类的更多相关文章
- Java中二进制、十进制、十六进制及ASCII码与String及字节数组与十六进制之间的转换
public class DigitalTrans { /** * 数字字符串转ASCII码字符串 * * @param String * 字符串 * @return ASCII字符串 */ publ ...
- 深刻理解Java中final的作用(一):从final的作用剖析String被设计成不可变类的深层原因
声明:本博客为原创博客,未经同意,不得转载!小伙伴们假设是在别的地方看到的话,建议还是来csdn上看吧(原文链接为http://blog.csdn.net/bettarwang/article/det ...
- java中创建字符串的两种方式(“”与new String())及区别
结论:通过""创建的字符串实际上在java堆中只有一个,而通过new string创建出来的字符串在java堆中占有不同的内存. 第一个True表明这两个在内存中拥有相同的地址,那 ...
- java中八种基本数据类型以及它们的封装类,String类型的一些理解
在我们面试或者考试过程中经常会考到八种基本数据类型以及它们的封装类,那么有哪八种基本数据类型呢?它们的封装类又是什么呢? 首先,八种基本数据类型分别是:int.short.float.double.l ...
- python中2进制、10进制、16进制等之间的转换
10转2: bin(8) # '0b1000' 2转10: int( 10转16: hex(15) # '0xf' 16转10: int( 2进制和16进制中间通过转10进制可以相互转换 from b ...
- Java中char和String的相互转换
转自:http://blog.csdn.net/yaokai_assultmaster/article/details/52082763 Java中char是一个基本类型,而String是一个引用类型 ...
- Java中几种常用数据类型之间转换的方法
Java中几种常用的数据类型之间转换方法: 1. short-->int 转换 exp: short shortvar=0; int intvar=0; shortvar= (short) in ...
- 从源代码的角度聊聊java中StringBuffer、StringBuilder、String中的字符串拼接
长久以来,我们被教导字符串的连接最好用StringBuffer.StringBuilder,但是我们却不知道这两者之间的区别.跟字符串相关的一些方法中总是有CharSequence.StringBuf ...
- (转)java 中unsigned类型的转换
转自:http://blog.sina.com.cn/s/blog_77bf45a90101dld9.html 在Java中,不存在Unsigned无符号数据类型,但可以轻而易举的完成Unsigned ...
- Java中short和int的转换
例子[1]: 第一种情况: short a = 1; a = a + 1; // 这一步会报错 System.out.print(a); 编译器会报错,原因如下: 第二种情况: short a = 1 ...
随机推荐
- POJ 2983 Is the Information Reliable?(差分约束系统)
http://poj.org/problem?id=2983 题意:N 个 defense stations,M条消息,消息有精确和模糊之分,若前边为P.则为精确消息,有两个defense stati ...
- RxJava开发精要7 – Schedulers-解决Android主线程问题
原文出自<RxJava Essentials> 原文作者 : Ivan Morgillo 译文出自 : 开发技术前线 www.devtf.cn 转载声明: 本译文已授权开发者头条享有独家转 ...
- WCF - Self Hosting
WCF - Self Hosting Here, the WCF service is hosted in a console application. Given below is the proc ...
- Java之关键字static和final的使用
static 在Java中声明属性.方法时,可使用关键字static来修饰. 1.static变量 按照是否静态的对类成员变量进行分类可分两种:一种是被static修饰的变量,叫静态变量或 ...
- @Component("userService").@Resource(name="userDao")
@Component("userService") public class UserService { private UserDAO userDao; public void ...
- 函数 flst_get_first
/********************************************************************//** Gets list first node addre ...
- c#调用c++动态库的一些理解
调用c++动态库一般我们这样写 [DllImport("UCamer.dll", CallingConvention = CallingConvention.Winapi)] ...
- BZOJ_1620_[Usaco2008_Nov]_Time_Management_时间管理_(二分+贪心)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1620 N个工作,每个工作其所需时间,及完成的Deadline,问要完成所有工作,最迟要什么时候 ...
- nginx 安全漏洞 (CVE-2013-4547)
Nginx 的安全限制可能会被某些请求给忽略,(CVE-2013-4547). 当我们通过例如下列方式进行 URL 访问限制的时候,如果攻击者使用一些没经过转义的空格字符(无效的 HTTP 协议,但从 ...
- WCF中修改接口或步骤名称而不影响客户端程序
WCF中修改接口或方法名称而不影响客户端程序 本篇接着"从Web Service和Remoting Service引出WCF服务"中有关WCF的部分. 运行宿主应用程序. 运行We ...