How to lowercase every element of a collection efficiently?

Posted by Chris on Stack Overflow See other posts from Stack Overflow or by Chris
Published on 2010-04-15T11:00:08Z Indexed on 2010/04/15 11:03 UTC
Read the original article Hit count: 229

Filed under:
|

Whats the most efficient way to lower case every element of a list or set?

My idea for a List:

final List<String> strings = new ArrayList<String>();
strings.add("HELLO");
strings.add("WORLD");

for(int i=0,l=strings.size();i<l;++i)
{
  strings.add(strings.remove(0).toLowerCase());
}

is there a better, faster way? How would this exmaple look like for a set? As there is currently no method for applying an operation to each element of a set (or list) can it be done without creating an additional temporary set?

Something like this would be nice:

Set<String> strings = new HashSet<String>();
strings.apply(
  function (element)
  { this.replace(element, element.toLowerCase();) } 
);

Thanks,

© Stack Overflow or respective owner

Related posts about java

Related posts about string-manipulation