How to lowercase every element of a collection efficiently?

后端 未结 11 651
走了就别回头了
走了就别回头了 2020-12-02 16:53

What\'s the most efficient way to lower case every element of a List or Set?

My idea for a List:

final List strings = new ArrayList<         


        
11条回答
  •  被撕碎了的回忆
    2020-12-02 17:22

    Well, there is no real elegant solution due to two facts:

    • Strings in Java are immutable
    • Java gives you no real nice map(f, list) function as you have in functional languages.

    Asymptotically speaking, you can't get a better run time than your current method. You will have to create a new string using toLowerCase() and you will need to iterate by yourself over the list and generate each new lower-case string, replacing it with the existing one.

提交回复
热议问题