How do I remove repeated elements from ArrayList?

后端 未结 30 1580
难免孤独
难免孤独 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:45
    public Set<Object> findDuplicates(List<Object> list) {
            Set<Object> items = new HashSet<Object>();
            Set<Object> duplicates = new HashSet<Object>();
            for (Object item : list) {
                if (items.contains(item)) {
                    duplicates.add(item);
                    } else { 
                        items.add(item);
                        } 
                } 
            return duplicates;
            }
    
    0 讨论(0)
  • 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<String> s = new HashSet<String>(list);
    

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

    0 讨论(0)
  • you can use nested loop in follow :

    ArrayList<Class1> l1 = new ArrayList<Class1>();
    ArrayList<Class1> l2 = new ArrayList<Class1>();
    
            Iterator iterator1 = l1.iterator();
            boolean repeated = false;
    
            while (iterator1.hasNext())
            {
                Class1 c1 = (Class1) iterator1.next();
                for (Class1 _c: l2) {
                    if(_c.getId() == c1.getId())
                        repeated = true;
                }
                if(!repeated)
                    l2.add(c1);
            }
    
    0 讨论(0)
  • 2020-11-21 06:49

    It is possible to remove duplicates from arraylist without using HashSet or one more arraylist.

    Try this code..

        ArrayList<String> lst = new ArrayList<String>();
        lst.add("ABC");
        lst.add("ABC");
        lst.add("ABCD");
        lst.add("ABCD");
        lst.add("ABCE");
    
        System.out.println("Duplicates List "+lst);
    
        Object[] st = lst.toArray();
          for (Object s : st) {
            if (lst.indexOf(s) != lst.lastIndexOf(s)) {
                lst.remove(lst.lastIndexOf(s));
             }
          }
    
        System.out.println("Distinct List "+lst);
    

    Output is

    Duplicates List [ABC, ABC, ABCD, ABCD, ABCE]
    Distinct List [ABC, ABCD, ABCE]
    
    0 讨论(0)
  • 2020-11-21 06:49

    this can solve the problem:

    private List<SomeClass> clearListFromDuplicateFirstName(List<SomeClass> list1) {
    
         Map<String, SomeClass> cleanMap = new LinkedHashMap<String, SomeClass>();
         for (int i = 0; i < list1.size(); i++) {
             cleanMap.put(list1.get(i).getFirstName(), list1.get(i));
         }
         List<SomeClass> list = new ArrayList<SomeClass>(cleanMap.values());
         return list;
    }
    
    0 讨论(0)
  • 2020-11-21 06:50

    If you're willing to use a third-party library, you can use the method distinct() in Eclipse Collections (formerly GS Collections).

    ListIterable<Integer> integers = FastList.newListWith(1, 3, 1, 2, 2, 1);
    Assert.assertEquals(
        FastList.newListWith(1, 3, 2),
        integers.distinct());
    

    The advantage of using distinct() instead of converting to a Set and then back to a List is that distinct() preserves the order of the original List, retaining the first occurrence of each element. It's implemented by using both a Set and a List.

    MutableSet<T> seenSoFar = UnifiedSet.newSet();
    int size = list.size();
    for (int i = 0; i < size; i++)
    {
        T item = list.get(i);
        if (seenSoFar.add(item))
        {
            targetCollection.add(item);
        }
    }
    return targetCollection;
    

    If you cannot convert your original List into an Eclipse Collections type, you can use ListAdapter to get the same API.

    MutableList<Integer> distinct = ListAdapter.adapt(integers).distinct();
    

    Note: I am a committer for Eclipse Collections.

    0 讨论(0)
提交回复
热议问题