码迷,mamicode.com
首页 > 编程语言 > 详细

Java 输出对象为字符串

时间:2017-09-04 13:11:53      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:for   pre   style   efi   reflect   对象   object   map   UI   

  

public static String reflectionToString(Object o){
if(o == null) return StringUtils.EMPTY;
StringBuilder toStr = new StringBuilder();
if(o instanceof Collection){
Iterator it = ((Collection) o).iterator();
while (it.hasNext()){
toStr.append(reflectionToString(it.next()));
toStr.append("\n");
}
}else if(o instanceof Map){
Iterator<Map.Entry> mit = ((Map) o).entrySet().iterator();
while (mit.hasNext()){
Map.Entry entry = mit.next();
String kv = entry.getKey() + ": " + reflectionToString(entry.getValue());
toStr.append(kv);
toStr.append(" ");
}
} else if(o instanceof Object[]){
for (Object temp: (Object[])o){
toStr.append(reflectionToString(temp));
}
}else if(o instanceof String){
toStr.append(o);
} else {
toStr.append(ToStringBuilder.reflectionToString(o, ToStringStyle.SHORT_PREFIX_STYLE));
}
return toStr.toString();
}

Java 输出对象为字符串

标签:for   pre   style   efi   reflect   对象   object   map   UI   

原文地址:http://www.cnblogs.com/niejunlei/p/7472903.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!