How do I remove repeated elements from ArrayList?

后端 未结 30 1707
难免孤独
难免孤独 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:38

    Although converting the ArrayList to a HashSet effectively removes duplicates, if you need to preserve insertion order, I'd rather suggest you to use this variant

    // list is some List of Strings
    Set s = new LinkedHashSet<>(list);
    

    Then, if you need to get back a List reference, you can use again the conversion constructor.

提交回复
热议问题