What's the best way to trim() all elements in a List<String>?

heez

I have a List<String>, potentially holding thousands of strings. I am implementing a validation method, which includes ensuring that there aren't any leading or trailing whitespaces in each string.

I'm currently iterating over the list, calling String.trim() for each String, and adding it to a new List<String> and reassigning back to the original list after:

List<String> trimmedStrings = new ArrayList<String)();
for(String s : originalStrings) {
  trimmedStrings.add(s.trim());
}

originalStrings = trimmedStrings;

I feel like there's a DRYer way to do this. Are there alternate, more efficient approaches here? Thanks!

Edit: Yes I am on Java 8, so Java 8 suggestions are welcome!

Cootri

In Java 8, you should use something like:

List<String> trimmedStrings = 
    originalStrings.stream().map(String::trim).collect(Collectors.toList());

also it is possible to use unary String::trim operator for elements of the initial list (untrimmed String instances will be overwritten) by calling this method:

originalStrings.replaceAll(String::trim);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What is the best way to trim a list?

From Dev

What is the best way to trim a list?

From Dev

Simplest Way to Trim All String within a List<string[]> using LINQ?

From Dev

What is the best way to search a List<List<string>>?

From Dev

What's the best way to compile a list of all Parent objects of a list of child objects?

From Dev

What's the best way to organize JPanels in a list?

From Dev

What's the best way to list size of / in Terminal?

From Dev

What's the best way to add a list of items all having the same value

From Dev

What is the best way to get all ui elements of a window?

From Dev

What is the best way, to traverse and update all elements in an Erlang ETS table?

From Dev

What is the best way to get all ui elements of a window?

From Dev

What is the best way to categorize/group and sort a list of elements using javascript?

From Dev

What is the best way to categorize/group and sort a list of elements using javascript?

From Dev

What's the best way to wrap Label and Input elements

From Dev

What is the correct way to trim a string value?

From Dev

What's the best way to sum all values in a Pandas dataframe?

From Dev

What's the best way to find all 'cycles/cliques' in a matrix in R?

From Dev

What's the best way to add add 'All' value to <Select> Control?

From Dev

What's the best way to get a single random element from a List<>?

From Dev

What's the best way to declare a list of scalar values in Swift

From Dev

What's the best way to add the same ajax function to a list of comments?

From Dev

What's the best way to organize a display list using AS3?

From Dev

What's the best way to code with list from other layout?

From Dev

What’s the best way to Convert a list to dict in Python2.7

From Dev

What's the best way to return List of data from controller

From Dev

List of all characters that String.Trim() removes?

From Dev

What's the best way to implement a string buffer in Rust?

From Java

What's the best way to convert a number to a string in JavaScript?

From Dev

What's the best way to represent a short bit string?

Related Related

  1. 1

    What is the best way to trim a list?

  2. 2

    What is the best way to trim a list?

  3. 3

    Simplest Way to Trim All String within a List<string[]> using LINQ?

  4. 4

    What is the best way to search a List<List<string>>?

  5. 5

    What's the best way to compile a list of all Parent objects of a list of child objects?

  6. 6

    What's the best way to organize JPanels in a list?

  7. 7

    What's the best way to list size of / in Terminal?

  8. 8

    What's the best way to add a list of items all having the same value

  9. 9

    What is the best way to get all ui elements of a window?

  10. 10

    What is the best way, to traverse and update all elements in an Erlang ETS table?

  11. 11

    What is the best way to get all ui elements of a window?

  12. 12

    What is the best way to categorize/group and sort a list of elements using javascript?

  13. 13

    What is the best way to categorize/group and sort a list of elements using javascript?

  14. 14

    What's the best way to wrap Label and Input elements

  15. 15

    What is the correct way to trim a string value?

  16. 16

    What's the best way to sum all values in a Pandas dataframe?

  17. 17

    What's the best way to find all 'cycles/cliques' in a matrix in R?

  18. 18

    What's the best way to add add 'All' value to <Select> Control?

  19. 19

    What's the best way to get a single random element from a List<>?

  20. 20

    What's the best way to declare a list of scalar values in Swift

  21. 21

    What's the best way to add the same ajax function to a list of comments?

  22. 22

    What's the best way to organize a display list using AS3?

  23. 23

    What's the best way to code with list from other layout?

  24. 24

    What’s the best way to Convert a list to dict in Python2.7

  25. 25

    What's the best way to return List of data from controller

  26. 26

    List of all characters that String.Trim() removes?

  27. 27

    What's the best way to implement a string buffer in Rust?

  28. 28

    What's the best way to convert a number to a string in JavaScript?

  29. 29

    What's the best way to represent a short bit string?

HotTag

Archive