This is a very generic question. I have a hashmap and I want to print it in a tabular format dynamically without knowing the size of the contents beforehand. The table should be well spaced just like we see in a Db query result. Are there any libraries/utilities which directly helps in this type of conversion? Or does JAVA have some intrinsic functions which I could make use of?
The code which I have written is a very naive one, and does not cater to dynamic length of the strings. I need the rows to be aligned also.
StringWriter returnString = new StringWriter();
Map<String,HashMap<String, String>> hashMap = new HashMap<String, HashMap<String, String>>();
for (Entry e : hashMap.entrySet()) {
HashMap<String, Number> hm = (HashMap<String, Number>) e.getValue();
String key = (String) e.getKey();
returnString.append("|\t").append(key).append("\t|");
for (Entry en : hm.entrySet()){
returnString.append("|\t").append((String) en.getValue()).append("\t|");
}
returnString.append("\r\n");
}
return returnString.toString();
The output should be like this irrespective of the strings length
s1 | s3 | s4
askdkc | asdkask | jksjndan