Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8087995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:57:29+00:00 2026-06-05T18:57:29+00:00

I have a Arraylist List<?> myList=new ArrayList(); myList = fetchQuery(); //fetches the list of

  • 0

I have a Arraylist

List<?> myList=new ArrayList();

myList = fetchQuery(); //fetches the list of Entities

Now myList Has a list of Entities

Now i convert that list to string,so it is a string object now.

String temp=myList.toString();

My question is how to convert that temp string again to that myList(List of entities) ???

Any ideas??

My Temp Value looks like this

temp=”[entityObject1,entityObject2…….]” ..

i could not extract each object and cast it with that entity class.. is there a way??

Thanks..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:57:32+00:00Added an answer on June 5, 2026 at 6:57 pm

    I have done a sample program to do this conversion.
    Things you have to be careful about.
    1.The class whose list with which we will be working should have an
    over ridden toString method.(You can have your own toString() format
    but need to change the rest of implementation accordingly).

    Sample content Object Class with over ridden 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;
        }
    
    }
    

    The Main 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;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list like that: private List<T> myList = new ArrayList<T>(); I want
I have one list: List<Object> myList = new ArrayList<Object>(); To get from this list
Say I have the following java code: Object myObj = new ArrayList(); List myList
I have something like @XmlElementWrapper(name=Mylist) List<Items> myItems = new ArrayList<Items>() and that comes out
I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
I have one customized list MyList that extends ArrayList, like this: class MyList extends
I have a TreeMap that holds the following keys/values: private ArrayList<TreeMap<String,String>> mList = new
I have a list (an ArrayList , infact) of type HColumn<ColName, ColValue> . Now
I have a List of HashMap such as below ArrayList l = new ArrayList
I have an array list of objects in my application. private static ArrayList<Player> userList=new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.