I am trying to remove empty a String
array from ArrayList
:
List<String[]> someList = new ArrayList<String[]>();
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<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
//Clone to new Array List
List<String[]> cloneCSV = new ArrayList<String[]>(someList);
for (String[] csvSingleLine : cloneCSV) {
//Create New List of String only
List<String> strings = new ArrayList<String>(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.