How to lowercase every element of a collection efficiently?

后端 未结 11 609
走了就别回头了
走了就别回头了 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:41

    I was looking for similar stuff, but was stuck because my ArrayList object was not declared as GENERIC and it was available as raw List type object from somewhere. I was just getting an ArrayList object "_products". So, what I did is mentioned below and it worked for me perfectly ::

    List dbProducts = _products;
        for(int i = 0; i

    That is, I first took my available _products and made a GENERIC list object (As I were getting only strings in same) then I applied the toLowerCase() method on list elements which was not working previously because of non-generic ArrayList object.

    And the method toLowerCase() we are using here is of String class.

    String java.lang.String.toLowerCase()

    not of ArrayList or Object class.

    Please correct if m wrong. Newbie in JAVA seeks guidance. :)

提交回复
热议问题