Wednesday, January 6, 2021

How to Remove Duplicate Elements From an ArrayList in Java

ArrayList in Java allows 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 remove duplicate elements from an ArrayList in Java. Options you have are as follows.

  • There is a brute force approach where you create a new list and loop through the already created old list, use contains() method to see if element is already added to the new list if not add it to the new list otherwise discard it. That way you can remove all duplicate elements from ArrayList without using any other collection. See example.
  • You can simply use a HashSet, since HashSet stores only unique elements you can use that feature of HashSet to remove duplicate elements from a list. Only problem is it won't retain the order of the list. See example.
  • If you want order of the List to be retained you can use LinkedHashSet which maintains the insertion order. See example.
  • Java 8 onward you can use Stream API which provides a very simple way to remove duplicate elements from ArrayList. See example.

Let's see examples of all these options one by one.

Read Also: Java Collections Framework

Removing duplicates from ArrayList without using any API or other Collection

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);
    }
  }
}

Using HashSet to remove duplicate elements from ArrayList

You can create a new HashSet by passing the List as argument. Any duplicates in the ArrayList would be discarded as HashSet stores only unique elements. Then you can add the Set again to the List after clearing the List. That gives you the List without any duplicates.

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 from the ArrayList 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.

Using LinkedHashSet to remove duplicate elements from ArrayList

Here is an example using LinkedHashSet to remove duplicate elements from an ArrayList, insertion order would be retained by using this option.

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.

Removing duplicates from ArrayList using Java Streams

Stream API in Java provides a very simple way to remove duplicate elements from ArrayList using the distinct method. Note that this option is available Java 8 onward.

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 from the ArrayList and original order is retained too and all that is done 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 to Remove Elements From an ArrayList in Java
  2. How ArrayList Works Internally in Java
  3. How and Why to Synchronize ArrayList in Java
  4. Difference Between ArrayList And CopyOnWriteArrayList in Java
  5. Java Collections Interview Questions And Answers

You may also like -

  1. How HashMap Works Internally in Java
  2. Difference Between Comparable and Comparator in Java
  3. Marker interface in Java
  4. Why Class Name And File Name Should be Same in Java
  5. Functional Interfaces in Java
  6. Race Condition in Java Multi-Threading
  7. Object Cloning in Java
  8. SerialVersionUID And Versioning in Java Serialization

1 comment:

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

    ReplyDelete