Remove Empty String array from ArrayList I am trying to remove empty a String array from ArrayList:

  List&lt;String[]&gt; someList = new ArrayList&lt;String[]&gt;();


which contains data like:

[
  { "Loic" , "Remy" , "Fail","Medical"} ,
  { "Faser" , "Foster" , "Pass","Southampton","GK","Young"} ,
  { "" , "" ,} ,
  { "" , "" , "","","",""} ,
  { "Emre" , "Can" , "Pass","Liverpool","CDM"}
]


I want to remove the empty String[] from the ArrayList:

List&lt;String[]&gt; someList = (List&lt;String[]&gt;) csvMappedData.get(Constants.CSV_DATA);
    //Clone to new Array List
    List&lt;String[]&gt; cloneCSV = new ArrayList&lt;String[]&gt;(someList);
    for (String[] csvSingleLine : cloneCSV) {
        //Create New List of String only
        List&lt;String&gt; strings = new ArrayList&lt;String&gt;(Arrays.asList(csvSingleLine));
        //Remove all string that are null or blank
        strings.removeAll(Arrays.asList(null, ""));
        //Check if the List is empty or null after removing in above step
        if (strings.isEmpty() || Validator.isNull(strings)) {
            //If yes it is all blank String array 
            //Remove from orginal LIST
            someList.remove(csvSingleLine);
        }
    }


The above code is working fine for me.

Questions: 


Is there any other alternative solution that is quick and elegant?
What are the change that I could make to it, to make it more
efficient?


The size of each row varies, so I couldn't use remove all by creating a dummy array.
There are a few tricks you can use to improve this code.

First of all,

List&lt;String[]&gt; someList = (List&lt;String[]&gt;) csvMappedData.get(Constants.CSV_DATA);
List&lt;String[]&gt; cloneCSV = new ArrayList&lt;String[]&gt;(someList);
for (String[] csvSingleLine : cloneCSV) {


I understand that you're using this "copy the list"-approach to avoid a ConcurrentModificationException, but instead of doing that you can use an Iterator.

List&lt;String[]&gt; someList = (List&lt;String[]&gt;) csvMappedData.get(Constants.CSV_DATA);
Iterator&lt;String[]&gt; iterator = someList.iterator();
while (iterator.hasNext()) {
    String[] strings = iterator.next();
    if (stringsShouldBeRemoved) {
        iterator.remove();
    }
}




Now, as for your code to check for whether to remove a String[]:

List&lt;String&gt; strings = new ArrayList&lt;String&gt;(Arrays.asList(csvSingleLine));
strings.removeAll(Arrays.asList(null, ""));
if (strings.isEmpty() || Validator.isNull(strings)) {
    someList.remove(csvSingleLine);
}


Again, you're using a copying-approach. To check for whether or not an object fulfills a criteria, you don't need to copy it, modify it, and check if it fulfills another criteria. Copying it and modifying it makes it slower and uses more memory.

Instead, let's use a method:

boolean shouldRemoveCSVSingleLine(String[] csvSingleLine) {
    for (String str : csvSingleLine) {
        if (str != null &amp;&amp; !str.equals("")) {
            return false;
        }
    }
    return true;
}


Now you can use this method in the while(iterator.hasNext()) loop:

List&lt;String[]&gt; someList = (List&lt;String[]&gt;) csvMappedData.get(Constants.CSV_DATA);
Iterator&lt;String[]&gt; iterator = someList.iterator();
while (iterator.hasNext()) {
    String[] strings = iterator.next();
    if (shouldRemoveCSVSingleLine(strings)) {
        iterator.remove();
    }
}