I have an List
of String
s and I would like to trim()
each element of the list.
Currently, I'm using an ArrayList
, doing a simple loop through the elements, and adding the trimmed element to a return list, like so:
int listLen = listToTrim.size();
List<String> trimmedList = new ArrayList<String>( listLen );
for ( int i = 0; i < listLen; i++ ) {
trimmedList.add( listToTrim.get( i ).trim() );
}
return trimmedList;
For large lists, would there be a more efficient way of doing this?
The general rule of thumb is that you don't modify a collection/array/list while iterating over it. Use a secondary list to store the items you want to act upon and execute that logic in a loop after your initial loop.
Replace element in arraylist while iterating Do not use iterator if you plan to modify the arraylist during iteration. Use standard for loop, and keep track of index position to check the current element. Then use this index to set the new element. Java program to search and replace an element in an ArrayList.
Using an iterator, or using a foreach loop (which internally uses an iterator), guarantees that the most appropriate way of iterating is used, because the iterator knows about how the list is implemented and how best go from one element to the next.
Nope, you're good. That's about as efficient as it'll get. There's no magic that avoids iterating.
One point to keep in mind, 'though: If listToTrim
is not a random-access-list (i.e. it does not implement RandomAccess
), then using an Iterator
(or an enhanced for-loop, which uses an Iterator
internally) instead of a traditional for-loop is usually much more efficient. The most notable List
that doesn't implement RandomAccess
is the LinkedList
. Calling l.get(300)
on a LinkedList
with 600 elements will have to iterate through ~300 elements to get the correct one!
Modifying your code to use an enhanced for-loop would look like this:
public List<String> trimStrings(Listy<String> listToTrim) {
List<String> trimmedList = new ArrayList<String>(listToTrim.size());
for (String str : listToTrim) {
trimmedList.add(str.trim());
}
return trimmedList;
}
If you don't need the original list any more, then re-using the original list can save memory and improve performance:
public void trimStringsInPlace(List<String> listToTrim) {
ListIterator<String> it = listToTrim.listIterator();
while (it.hasNext()) {
it.set(it.next().trim());
}
}
Not really; you have to call trim
on each element, and preallocating an ArrayList
of the right size is as fast as you can get. Java 8 will allow you to make the syntax more compact, but iterating and trim
ming is the minimum amount of work here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With