How to delete duplicate objects from an arraylist in java?


This is short tutorial talks about How to Store unique objects to avoid the duplicates in java List?

In real time scenarios, you encountered the 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 a number of 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 implementation of Set which does not allow duplicates and maintains insertion order.

Here are sequence of steps

  • Create an ArrayList with duplicate values
  • Next, Create LinkedHashSet object passing list to constructor
  • 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 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 implementing. You can Consider for 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.

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.