How do I remove repeated elements from ArrayList?

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

    This three lines of code can remove the duplicated element from ArrayList or any collection.

    List entities = repository.findByUserId(userId);
    
    Set s = new LinkedHashSet(entities);
    entities.clear();
    entities.addAll(s);
    

提交回复
热议问题