How do I remove repeated elements from ArrayList?

后端 未结 30 2258
难免孤独
难免孤独 2020-11-21 06:24

I have an ArrayList, and I want to remove repeated strings from it. How can I do this?

30条回答
  •  我寻月下人不归
    2020-11-21 06:47

    If you don't want duplicates, use a Set instead of a List. To convert a List to a Set you can use the following code:

    // list is some List of Strings
    Set s = new HashSet(list);
    

    If really necessary you can use the same construction to convert a Set back into a List.

提交回复
热议问题