Removing Duplicate Values from ArrayList

后端 未结 18 1319
无人及你
无人及你 2020-11-30 03:24

I have one Arraylist of String and I have added Some Duplicate Value in that. and i just wanna remove that Duplicate value So how to remove it.

Here Example I got o

18条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 03:48

    if you want to use only arraylist then I am worried there is no better way which will create a huge performance benefit. But by only using arraylist i would check before adding into the list like following

    void addToList(String s){
      if(!yourList.contains(s))
           yourList.add(s);
    }
    

    In this cases using a Set is suitable.

提交回复
热议问题