How do I remove repeated elements from ArrayList?

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

    In Java 8:

    List deduped = list.stream().distinct().collect(Collectors.toList());
    

    Please note that the hashCode-equals contract for list members should be respected for the filtering to work properly.

提交回复
热议问题