How to rebuild String object in ArrayList <Entity>?

I have an arraylist

List<?> myList=new ArrayList(); myList = fetchQuery(); //fetches the list of Entities 

Now myList Has a list of objects

Now I am converting this list to a string, so now it is a string object.

 String temp=myList.toString(); 

My question is how to convert this temporary string to this myList again (entity list) ???

Any ideas?

My pace value is as follows

temp = "[entityObject1, entityObject2 .......]" ..

I could not extract every object and drop it with this entity class. Is there any way?

Thanks..

+4
source share
4 answers

I made an example program for this conversion. Things you need to be careful about. 1. The class whose list we are going to work with must have the toString method dragged. (You may have your own toString () format but you need to change the rest of the implementation accordingly).

Sample Content An object class using the overString toString () method.

 class Sample { private String name; private String sex; @Override public String toString() { return "name=" + name + "&" + "sex=" + sex; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * @param sex * the sex to set */ public void setSex(String sex) { this.sex = sex; } } 

Home Application.java

 import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class MainApplication { public static void main(String[] args) { List<Sample> e = new ArrayList<Sample>(); Sample a1 = new Sample(); a1.setName("foo1"); a1.setSex("Male"); Sample a2 = new Sample(); a2.setName("foo2"); a2.setSex("Male"); e.add(a1); e.add(a2); String tmpString=e.toString(); List<Sample> sampleList = (List<Sample>) chengeToObjectList(tmpString, Sample.class); } /** * Method to change String to List<Obj>. * @param listString * @param contentClass * @return List of Objects */ public static Collection chengeToObjectList(String listString, Class contentClass) { Collection returnList = new ArrayList(); // Code to remove [ and ] coming from the toString method if (listString.charAt(0) == '[') { listString = listString.substring(1); } if (listString.charAt(listString.length() - 1) == ']') { listString = listString.substring(0, listString.length() - 1); } String[] stringArray = listString.trim().split(","); for (int i = 0; i < stringArray.length; i++) { String[] contentArray = stringArray[i].trim().split("&"); Object ob = null; try { ob = contentClass.newInstance(); } catch (InstantiationException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IllegalAccessException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } for (int j = 0; j < contentArray.length; j++) { String[] keyValueArray = contentArray[j].trim().split("="); String fieldName = keyValueArray[0].trim(); //Code to make the 1st char uppercase String s = String.valueOf(fieldName.toCharArray()[0]); s = s.toUpperCase(); fieldName = s + fieldName.substring(1); String fieldValue = keyValueArray[1].trim(); Class[] paramTypes = new Class[1]; paramTypes[0] = String.class; String methodName = "set" + fieldName; Method m = null; try { m = contentClass.getMethod(methodName, paramTypes); } catch (NoSuchMethodException m) { m.printStackTrace(); } try { String result = (String) m.invoke(ob, fieldValue); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e1) { e1.printStackTrace(); } } returnList.add(ob); } return returnList; } } 
+2
source

It depends on the implementation of toString() , if you exposed each field in toString () in a specific format, parsing its opposite, you can get the original object in the format, but this is usually not the case.

For instance:

You cannot form an instance of a person from string because id is not displayed

 class Person{ private long id; private String name; //stuffs @Override public String toString(){ return "Person :" +name;} } 

see also

0
source

When you execute myList.toString (); you actually call the AbstractCollection.toString () method, which in turn calls the toString () method for each of the objects in the list.

If the toString () method of the Entity class does not serialize objects in such a way that you can subsequently restore them, you cannot do anything.

If it serializes them correctly, you will need to parse the temp line, identify all the individual lines for each Entity object, and restore them there.

This can be done only if the serialized lines for each Entity object do not contain square brackets and commas, or if they leave them properly. This is because the AbstractCollection.toString () method uses these special characters when creating the temp string.

If all of the above conditions are met, you can use regular expressions to parse the temp string and get each of the individual serialized Entity objects. Then you need to restore the objects and add them to the new list.

For regular expressions in Java, see http://docs.oracle.com/javase/6/docs/api/java/util/regex/package-summary.html .

0
source

Here is an idea:

Make the toString method of your Entity class generate an XML string compatible with some serializer like JAXB or XStream. Then, when you get Collection.toString() , split it into Entity (maybe String.split() might help). In short, you want the entity's XML definition to be returned in String, which you can pass to you an XML deserializer, which in turn can transform you into an Object of this type.

0
source

All Articles