user3776241
user3776241

Reputation: 543

ArrayList of HashMaps to string, and then back to ArrayList of HashMaps

I have a ArrayList<HashMap<String,String>> that I am using the toString() method on to store in a database.

Here is the code that I use to store it the toString() to a database (it works):

    HashMap<String, String> commentsHash = null; 
    ArrayList<HashMap<String, String>> test2 = new ArrayList<HashMap<String, String>>(); 

    for (int i=0; i < test.size(); i++)
    { 
        String timestamp = test.get(i).get("timestamp");
        String last_name = test.get(i).get("last_name"); 
        String first_name = test.get(i).get("first_name"); 
        String comment = test.get(i).get("comment");

        commentsHash = new HashMap<String, String>(); 

        commentsHash.put("creation_timestamp", timestamp); 
        commentsHash.put("first_name", first_name); 
        commentsHash.put("last_name", last_name); 
        commentsHash.put("comment", comment);   

        test2.add(commentsHash); 
    }

    dbHelper.addCommentsToMyLiPost(Integer.parseInt(sqlId), test2.toString());

Here is the method I want to use to convert a string to a HashMap<String, String>:

protected HashMap<String,String> convertToStringToHashMap(String text){
    HashMap<String,String> data = new HashMap<String,String>();
    Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
    String[] split = p.split(text);
    for ( int i=1; i+2 <= split.length; i+=2 ){
        data.put( split[i], split[i+1] );
    }
    return data;
}

I have tried using .split(",") on the string to split the string into 2 parts, but instead of returning two, it returns 8.

Here is what the toString() method prints. It is an ArrayList of HashMaps, and I am trying to grab the two HashMaps that are inside of the ArrayList.

[{comment=hello, last_name=u1, first_name=u1, creation_timestamp=1404938643772}, {comment=hello2, last_name=u2, first_name=u2, creation_timestamp=1404963221598}]

Upvotes: 0

Views: 923

Answers (2)

sendon1982
sendon1982

Reputation: 11234

In convertToStringToHashMap, when you put your data into HashMap, the old value will be replaced since they have same key for each records, such as comment, last_name, etc.

    public static Map<String, Map<String, String>> convertToStringToHashMap(String text)
{
    Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();

    Pattern p = Pattern.compile("[\\{\\}\\=\\, ]++");
    String[] split = p.split(text);

    Map<String, String> data = new HashMap<String, String>();
    int gap = 8;
    int key = 1;

    for (int i = 1; i + 2 <= split.length; i += 2)
    {
        data.put(split[i], split[i+1]);
        if((i + 1) % gap == 0)
        {
            map.put(String.valueOf(key++), data);
            data = new HashMap<String, String>();
            data.clear();
        }
    }

    return map;
}

This will return a Map:

2={first_name=u2, last_name=u2, comment=hello2, creation_timestamp=1404963221598}
1={first_name=u1, last_name=u1, comment=hello, creation_timestamp=1404938643772}

Upvotes: 1

user2833557
user2833557

Reputation: 677

This program will recreate the whole list from the database entry

    Pattern firstPat = Pattern.compile("\\{.*?\\}");
    Matcher firstMat = firstPat.matcher(text);
    ArrayList<HashMap<String, String>> list = new ArrayList<>();
    while(firstMat.find()){
        HashMap<String, String> map = new HashMap<>();
        String assignStrings = firstMat.group();
        String [] assignGroups = assignStrings.substring(1,assignStrings.length()-1).split("\\s*\\,\\s*");
        for(String assign:assignGroups){
            String [] parts = assign.split("\\=");
            map.put(parts[0], parts[1]);
        }
        list.add(map);
    }
    return list

Upvotes: 1

Related Questions