Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting elements under List<String[]> to upper case in one go

Tags:

java

string

I am using a List<String[]> with a few smaller case fields.

List<String[]> csvBody = reader.readAll();

My output should be of the same datatype List<String[]>.

Is there a possibility to convert every String in my Container to uppercase in a stretch?

like image 275
Mike Avatar asked Dec 13 '22 16:12

Mike


2 Answers

I reckon you can use a combination of List#replaceAll and a Stream to map the arrays:

csvBody.replaceAll(a -> Arrays.stream(a)
                              .map(String::toUpperCase)
                              .toArray(String[]::new));
like image 170
Jacob G. Avatar answered Dec 16 '22 06:12

Jacob G.


Just iterate the strings:

List<String[]> csvBody = reader.readAll();
for (String[] strings : csvBody) {
    for (int i = 0; i < strings.length; i++) {
        strings[i] = strings[i].toUpperCase();
    }
}
like image 30
xingbin Avatar answered Dec 16 '22 07:12

xingbin