-2

I have created an ArrayList in Java:

List<String> listValuesFromMap = new ArrayList<String>();

which I fill with values using entry.getValue() method:

listValuesFromMap.add((String) entry.getValue().toString());

and I got something like this:

"names" : ["[name1, name2, name3]"]

How to remove these extra brackets with quotes to get only:

[name1, name2, name3]

Whole part of code:

@SuppressWarnings("unchecked")

Map<String, Object> attributes = (Map<String, Object>) input
                        .getValueByField(ATTRIBUTES_FIELD);

Document mongoDoc= new Document();
List<String> listValuesFromMap = new ArrayList<String>();

for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        if (entry.getKey().contains("names")) {

            JSONArray jsonArray = new JSONArray();
            listValuesFromMap.add((String) entry.getValue().toString());

            for (String item : listValuesFromMap) {
                jsonArray.add(item.toString());
            }
            mongoDoc.put(entry.getKey(), jsonArray);
        }
        else {
            listValuesFromMap.add((String) entry.getValue().toString());
            mongoDoc.put(entry.getKey(), entry.getValue());
        }
}

I think the problem is in the line listValuesFromMap.add((String) entry.getValue().toString()); - values from the entry.getValue() are already in [] and add method put extra []. Does anyone know how to avoid these extra brackets?

corry
  • 1,457
  • 7
  • 32
  • 63
  • 1
    Possible duplicate of [How can I override the toString method of an ArrayList in Java?](http://stackoverflow.com/questions/18129505/how-can-i-override-the-tostring-method-of-an-arraylist-in-java) – Frederic Klein Sep 08 '16 at 14:44
  • I need a complete example to tell. Look at [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Could it be that the value from the map is itself a collection? – Ole V.V. Sep 08 '16 at 14:55
  • @OleV.V. the value from the map is also ArrayList – corry Sep 09 '16 at 08:48

1 Answers1

1

I’m still unsure whether I know enough to answer. I’ll try.

As I understand, the double brackets come from having an ArrayList in an ArrayList. Which is fine for many purposes. However, to avoid the double brackets, you either need not to put an ArrayList into your ArrayList, or to take it out again before printing it (or what you need to do with it). For the first option, here’s how to add the elements of the ArrayList individually to listValuesFromMap rather than just adding the list:

    for (Map.Entry<String, Object> entry : attributes.entrySet()) {
        Object value = entry.getValue();
        if (entry.getKey().contains("names")) {

            JSONArray jsonArray = new JSONArray();
            for (Object name : (ArrayList<?>) value) {
                listValuesFromMap.add(name.toString());
            }

            for (String item : listValuesFromMap) {
                jsonArray.add(item);
            }
            mongoDoc.put(entry.getKey(), jsonArray);
        } else {
            listValuesFromMap.add(value.toString());
            mongoDoc.put(entry.getKey(), entry.getValue());
        }
    }

You may also consider taking the items to put into jsonArray directly from value instead of from listValuesForMap; however, this will not include values added to the list in previous iterations of the outer for loop, so it may not be what you want.

For the second option, listValuesFromMap.get(0) will return the first element of the list, that is, the ArrayList that you put in there. However, I don’t think this is what you need here.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161