Question

How do I convert from String[] to ArrayList using the BeanUtils.populate method? It looks like I could use the ArrayConverter class, but I'm having a hard time finding an explanation on how to do that.

Here is some test code I created:

public class TestAction {
public static TestBean testPopulate() throws IllegalAccessException, InvocationTargetException {
    HashMap<String, Object> map = new HashMap<String, Object>();
    TestBean bean = new TestBean();
    String[] names = {"teststring","testboolean","testinteger","testarray"};
    String[] array = {"hi","bye"};
    Object[] values = {"TEST","","100",array};
    int i = 0;
    while (i < names.length) {
        String name = names[i];
        map.put(name, values[i]);
        i++;
    }
    BeanUtils.populate(bean, map);

    return bean;
}

public static void main(String[] args) {
    try {
        TestBean bean = testPopulate();
        System.out.println(bean.getTeststring() + " " + bean.getTestinteger() + " " + bean.getTeststring() + " " + bean.isTestboolean());
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

public class TestBean {
private String teststring;
private boolean testboolean;
private int testinteger;
private ArrayList<String> testarray;

public String getTeststring() {
    return teststring;
}
public void setTeststring(String teststring) {
    this.teststring = teststring;
}
public boolean isTestboolean() {
    return testboolean;
}
public void setTestboolean(boolean testboolean) {
    this.testboolean = testboolean;
}
public int getTestinteger() {
    return testinteger;
}
public void setTestinteger(int testinteger) {
    this.testinteger = testinteger;
}
public ArrayList<String> getTestarray() {
    return testarray;
}
public void setTestarray(ArrayList<String> testarray) {
    this.testarray = testarray;
}

}

I pass in a String[] type expecting it to know how to convert to ArrayList, but it fails.

Was it helpful?

Solution

This is a general-purpose answer as I don't generally work with Beans. It's possible BeanUtils should handle this case, but it would surprise me.

An array of Strings is not the same thing as an ArrayList object, despite their similarities. It's generally a poor idea to implicitly convert from one to the other, as there are multiple logical ways that could be done.

  • You can use Arrays.asList() which takes an array and returns a wrapper List object. Note that it is not an ArrayList, it is instead a fixed-size custom implementation of List which directly interfaces with the passed array, meaning no new storage has to be allocated.

  • If instead you really do want an ArrayList, you can construct a new one based off the List object you got, with:

    ArrayList<String> ls = new ArrayList<String>(Arrays.asList(myArray));
    

    This call does allocate a new storage array, but if you want adjustable-size, you have to accept that O(n) cost.

These two possible choices, both of which are equally valid depending on your use case, are exactly why it would be a poor idea for your bean to implicitly convert an array into a List - which implementation should it chose? The fast one that throws exceptions if you try to resize it, or the slow one that doesn't?

Wherever possible, be explicit about your type conversions. Despite the verbosity seeming/being obnoxious, this is one of Java's strongest features.

OTHER TIPS

You did not mention what exactly fails in your code but here is how to convert String array to list without any third party utilities:

Arrays.asList(new String[] {"aaa", "bbb", "ccc"})

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top