How do I remove repeated elements from ArrayList?

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

    this can solve the problem:

    private List clearListFromDuplicateFirstName(List list1) {
    
         Map cleanMap = new LinkedHashMap();
         for (int i = 0; i < list1.size(); i++) {
             cleanMap.put(list1.get(i).getFirstName(), list1.get(i));
         }
         List list = new ArrayList(cleanMap.values());
         return list;
    }
    

提交回复
热议问题