patternjavaModerate
Remove Empty String array from ArrayList
Viewed 0 times
arrayemptyremovefromstringarraylist
Problem
I am trying to remove empty a
which contains data like:
I want to remove the empty
The above code is working fine for me.
Questions:
efficient?
The size of each row varies, so I couldn't use remove all by creating a dummy array.
String array from ArrayList:List someList = new ArrayList();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 someList = (List) csvMappedData.get(Constants.CSV_DATA);
//Clone to new Array List
List cloneCSV = new ArrayList(someList);
for (String[] csvSingleLine : cloneCSV) {
//Create New List of String only
List strings = new ArrayList(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.
Solution
There are a few tricks you can use to improve this code.
First of all,
I understand that you're using this "copy the list"-approach to avoid a
Now, as for your code to check for whether to remove a
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:
Now you can use this method in the
First of all,
List someList = (List) csvMappedData.get(Constants.CSV_DATA);
List cloneCSV = new ArrayList(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 someList = (List) csvMappedData.get(Constants.CSV_DATA);
Iterator 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 strings = new ArrayList(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 && !str.equals("")) {
return false;
}
}
return true;
}Now you can use this method in the
while(iterator.hasNext()) loop:List someList = (List) csvMappedData.get(Constants.CSV_DATA);
Iterator iterator = someList.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
if (shouldRemoveCSVSingleLine(strings)) {
iterator.remove();
}
}Code Snippets
List<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
List<String[]> cloneCSV = new ArrayList<String[]>(someList);
for (String[] csvSingleLine : cloneCSV) {List<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
Iterator<String[]> iterator = someList.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
if (stringsShouldBeRemoved) {
iterator.remove();
}
}List<String> strings = new ArrayList<String>(Arrays.asList(csvSingleLine));
strings.removeAll(Arrays.asList(null, ""));
if (strings.isEmpty() || Validator.isNull(strings)) {
someList.remove(csvSingleLine);
}boolean shouldRemoveCSVSingleLine(String[] csvSingleLine) {
for (String str : csvSingleLine) {
if (str != null && !str.equals("")) {
return false;
}
}
return true;
}List<String[]> someList = (List<String[]>) csvMappedData.get(Constants.CSV_DATA);
Iterator<String[]> iterator = someList.iterator();
while (iterator.hasNext()) {
String[] strings = iterator.next();
if (shouldRemoveCSVSingleLine(strings)) {
iterator.remove();
}
}Context
StackExchange Code Review Q#59776, answer score: 10
Revisions (0)
No revisions yet.