Wednesday, 12 August 2015

How to remove duplicate elements from an ArrayList in Java

Arraylist does allow adding duplicate elements but sometimes there may be a need to remove duplicates from a list. In this post we'll talk about the ways to do that.

First thing that will come to mind is create a new list and loop through the already created old list, use contains to see if element is already added to the new list if not add it otherwise discard it.
Example code for this logic

public class RemoveDuplicatesDemo {

    public static void main(String[] args) {
        List<String> cityList = new ArrayList<>();
        cityList.add("Delhi");
        cityList.add("Mumbai");
        cityList.add("Bangalore");
        cityList.add("Chennai");
        cityList.add("Kolkata");
        cityList.add("Mumbai");
        
        List<String> newList = new ArrayList<>();
        for(String name : cityList){
            if(!newList.contains(name)){
                newList.add(name);
            }
        }

        for(String name : newList){
            System.out.println("City Name - " + name);
        }
    }
}

But that much code is really not needed, as collection framework itself provides several options to remove duplicates from an arraylist. We can simply use a HashSet to do the trick of removing the duplicate elements. HashSet only stores unique elements and we'll use that feature of HashSet to remove duplicates. Only problem is it won't retain the order of the list.

Example code using HashSet

public class RemoveDuplicatesDemo {
    public static void main(String[] args) {
        List<String> cityList = new ArrayList<>();
        cityList.add("Delhi");
        cityList.add("Mumbai");
        cityList.add("Bangalore");
        cityList.add("Chennai");
        cityList.add("Kolkata");
        cityList.add("Mumbai");
        
        // creating a hashset using the list
        Set<String> citySet = new HashSet<String>(cityList);
        // remove all the elements from the list 
        cityList.clear();
        // add all the elements of the set to create a
        // list with out duplicates
        cityList.addAll(citySet);
        
        // displaying the elements
        for(String name : cityList){
            System.out.println("City Name - " + name);
        }
    }
}

Output

City Name - Delhi
City Name - Chennai
City Name - Kolkata
City Name - Mumbai
City Name - Bangalore

It can be seen that duplicate elements are removed but the original order is not retained. Though in most of the cases when we have such requirement to remove duplicates from the list order doesn't matter but if it does sweat not! LinkedHashSet is there to retain the order. LinkedHashSet differ from HashSet in that it maintains the insertion order.

Example code using LinkedHashSet

public class RemoveDuplicatesDemo {
    public static void main(String[] args) {
        List<String> cityList = new ArrayList<>();
        cityList.add("Delhi");
        cityList.add("Mumbai");
        cityList.add("Bangalore");
        cityList.add("Chennai");
        cityList.add("Kolkata");
        cityList.add("Mumbai");
        // creating a linkedhashset using the list
        Set<String> citySet = new LinkedHashSet<String>(cityList);
        // remove all the elements from the list 
        cityList.clear();
        // add all the elements of the set to create a
                // list with out duplicates
        cityList.addAll(citySet);
        
        // displaying the elements
        for(String name : cityList){
            System.out.println("City Name - " + name);
        }
    }
}

Output

City Name - Delhi
City Name - Mumbai
City Name - Bangalore
City Name - Chennai
City Name - Kolkata

It can be seen that the order is retained now.

Option with Java 8 Streams

Java 8 streams provide a very simple way to remove duplicate elements from a list. Using the distinct method.

Example code

public class RemoveDuplicatesDemo {

    public static void main(String[] args) {
        List<String> cityList = new ArrayList<>();
        cityList.add("Delhi");
        cityList.add("Mumbai");
        cityList.add("Bangalore");
        cityList.add("Chennai");
        cityList.add("Kolkata");
        cityList.add("Mumbai");
        
        cityList = cityList.stream().distinct().collect(Collectors.toList());
        
        
        // displaying the elements
        for(String name : cityList){
            System.out.println("City Name - " + name);
        }
    }
}

Output

City Name - Delhi
City Name - Mumbai
City Name - Bangalore
City Name - Chennai
City Name - Kolkata

It can be seen that the duplicate element is removed and original order is retained too that too in a single line.

That's all for this topic How to remove duplicate elements from an ArrayList in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. How ArrayList works internally in Java
  2. How to iterate an arraylist in Java
  3. How to join lists in Java
  4. How to remove elements from an ArrayList in Java
  5. How HashMap internally works in Java
  6. Java Collections interview questions

You may also like -

1 comment:

  1. This post is praisewothy for removing duplicacy based on List. Please upload post on Map.

    ReplyDelete