How to remove duplicate objects from arraylist in java?

It is a short tutorial that talks about How to Store unique objects to avoid duplicates in java List?

In real-time scenarios, you encountered cases having duplicate objects in a list. There are no java Collections List implementations to solve this.

In Collections, List allows duplicate values and maintain the insertion order, whereas Set does not allow duplicates, doesn’t maintain order.

To achieve this, We can do this in several ways.

How to remove duplicate values from ArrayList using SetUniqueList?

apache common-collection API provides org.apache.commons.collections4.list.SetUniqueList class which contains SetUniqueList used to remove duplicates from an arraylist and returns new list without duplicates..

import java.util.*;
import org.apache.commons.collections4.list.SetUniqueList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("one");
        System.out.println(list);

        List<String> result = SetUniqueList.setUniqueList(list);

        System.out.println(result);

    }
}

You can use this class for the same functionality Please leave a comment if you need more information on the implementation

using LinkedHashSet

LinkedHashSet is the implementation of Set which does not allow duplicates and maintains insertion order.

Here is a sequence of steps

  • Create an ArrayList with duplicate values
  • Next, Create LinkedHashSet object passing list to constructor
  • the result is returned without duplicates
import java.util.*;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("one");
        System.out.println(list);
        LinkedHashSet<String> result = new LinkedHashSet<>(list);
        System.out.println(result);

    }
}

Output

[one, two, three, four, one]
[one, two, three, four]

Java8 to remove duplicate elements from ArrayList

Java8 introduced stream API to process collections API. It has a distinct() method, removes duplicates. Finally returns list using collect with parameter Collectors.toList()

import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> list=new ArrayList<>();
        list.add("one");
        list.add("two");
        list.add("three");
        list.add("four");
        list.add("one");
        System.out.println(list);

        List<String> result = list.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println(result);

    }
}

Custom List Implementation: for Avoiding Duplicates-**

  • We can write our own set and list implementation by extending List implementation and Implementing Set interface
  • You can not consider implementing List as already Set is implemented. You can Consider extending ArrayList as it is not final
  • For this, you need to override the Add method and hashcode method to achieve without duplicates and order

Conclusion

To Sum Up, You can do multiple ways to remove duplicate elements with LinkedHashSet, SetUniqueList and Java8 stream distinct method.