Q: Convert String into nested ArrayList in Java I have a String as:
    String input = {{a,b,c}, 4,6, 7 , {c, d, {g,h, {} }}};

I need to convert 'input' into a nested ArrayList wherein contents within each '{ }' are in a separate list. Note that there are random blank spaces in input which need to be trimmed before adding each element in a List. 
Hence, the required output for needs to be (output depicted in toString() method for representation):
    List<Object> output = [[a,b,c],4,6,7,[c,d,[g,h,[]]]]

Here, every element is a list item. So
    List<Object> emptyList = []
    List<Object> subList1 = [g,h,emptyList]
    List<Object> subList2 = [c,d,subList1]
    List<Object> subList3 = [a,b,c]
    List<Object> parentList = [sublist3,4,6,7,sublist2]

I only require the last/parent List as 'parentList' above, and intermediate results can be ignored.
Could someone please help me with a neat algorithm to solve this. I have tried doing this for hours, but failed to derive a proper and recursive call method solution. However a non-recursive method solution is also most welcomed :)
I have tried using the following code. Although, it is a little crude.
    package com.psl.demo;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Collections;
    import java.util.List;

    public class ListClass {

        public List<Object> converToList(String string){
            List<Object> list = new ArrayList<Object>();

            string = string.trim();
            StringBuilder sb = new StringBuilder(string);

            List<Integer> startIndices= new ArrayList<Integer>();
            List<Integer> endIndices = new ArrayList<Integer>();

            int counter = 0;
            while(counter < sb.length()){
                if(sb.charAt(counter) == '{'){
                    startIndices.add(counter);
                    sb.setCharAt(counter, ' ');
                }
                counter++;
            }

            int reverseCounter = sb.length()-1;
            while(reverseCounter > 0){
                if(sb.charAt(reverseCounter) == '}'){
                    endIndices.add(reverseCounter);
                    sb.setCharAt(reverseCounter, ' ');
                }
                reverseCounter--;
            }

            if(startIndices.size() != endIndices.size()){
                System.out.println("Brackets do not match. exiting");
                System.exit(1);
            }


            int size = startIndices.size();
            counter = 0;
            reverseCounter = size-1;
            for(int i=0; i<size; i++){
                String contents =                 sb.substring(startIndices.get(reverseCounter), endIndices.get(counter));
                //sb.replace(startIndices.get(reverseCounter),         endIndices.get(counter),"");
                contents = contents.trim();
                List<Object> subList = new ArrayList<Object>();
                Object[] tokens = contents.split(",");
                for(int j=0; j< tokens.length;j++){
                    tokens[j] = tokens[j].toString().trim();
                }
                subList = Arrays.asList(tokens);
                list.add(subList);
                counter++;

                reverseCounter--;
            }

            Collections.reverse(list);
            return list;
        }

        public static void main(String[] args) {
            String string = "{{a ,b, {hello, world}},  4 ,6,  7, { c , d }         }";
            ListClass obj = new ListClass();
            List<Object> list= obj.converToList(string);
            System.out.println(list);
        }

    }

Please ignore the System.exit() part of code. That will be replaced with proper code. 
And List of Object type is used due to that specific requirement mentioned to me. 

A: What you're trying to do seems a lot like JSON deserialization. If the a, b, etc. were enclosed in quotes, you could use Jackson:
    import com.fasterxml.jackson.databind.ObjectMapper;
    // ...
    ObjectMapper jsonMapper = new ObjectMapper();
    String content = "{{\"a\",\"b\",\"c\"}, 4,6, 7 , {\"c\", \"d\", {\"g\", \"h\", {} }}}";
    String contentWithBrackets = content.replace('{', '[').replace('}',']');
    List list =  jsonMapper.readValue(contentWithBrackets, List.class);
    System.out.println(list); // [[a, b, c], 4, 6, 7, [c, d, [g, h, []]]]


A: This is may be similar to expression evaluation in DS. So i think you need to go with Stack data structure for such type of questions. Try with following code, it may solve your problem.
package src.com;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.List;
public class Abc {      
    public static class Bean {
        int token;
        Object obj;
        public Bean(){}
        public Bean(int token, Object o) {
            super();
            this.token = token;
            this.obj = o;
        }
    }
    public static void main(String[] args) {
        String input = "{{a,b,c},4, 6,7,{d,e,{},{ }}}";
        input = input.replaceAll(" ", "");
        List<Object> list = getList(input);
        System.out.println(list);
    }
    @SuppressWarnings("unchecked")
    public static List<Object> getList(String input1) {
        Deque<Object> stack = new ArrayDeque<Object>();
        char[] chararray = input1.toCharArray();
        String temp = "";
        for (int i = 0; i < chararray.length; i++) {
            if (chararray[i] == '}') {
                if (!temp.equals("")) {
                    stack.push(new Bean(0, temp));
                    temp = "";
                }
                List<Object> tmplist = new ArrayList<>();
                while (true) {
                    Object object = stack.pop();
                    if (object instanceof Bean) {
                        Bean b = (Bean) object;
                        if (b.token == 1)
                            break;
                        tmplist.add(b.obj);
                    } else {
                        tmplist.add(object);
                        if (stack.isEmpty())
                            break;
                    }
                }
                Collections.reverse(tmplist);
                stack.push(tmplist);
            } else {
                if (chararray[i] == '{') {
                    stack.push(new Bean(1, Character.toString(chararray[i])));
                } else if (chararray[i] == ',') {
                    if (!temp.equals("")) {
                        stack.push(new Bean(0, temp));
                        temp = "";
                    }
                } else {
                    temp = temp + Character.toString(chararray[i]);
                }
            }
        }
        return (List<Object>) stack.pop();
    }
}

