HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / Collections Framework / Java ArrayList / How to remove duplicate elements in ArrayList

How to remove duplicate elements in ArrayList

Learn to remove duplicate elements in ArrayList in Java using different techniques such as LinkedHashSet in Collections framework and using java 8 stream apis.

1. Remove duplicate elements in arraylist using LinkedHashSet

The LinkedHashSet is the best approach for removing duplicate elements in an arraylist. LinkedHashSet does two things internally :

  • Remove duplicate elements
  • Maintain the order of elements added to it

Java example to remove duplicates in arraylist using LinkedHashSet. In given example, numbersList is an arraylist containing integers and some of them are duplicate numbers e.g. 1, 3 and 5. We add the list to LinkedHashSet, and then get back the content back into the list. The result arraylist does not have duplicate integers.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        // ArrayList with duplicate elements
        ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
        
        System.out.println(numbersList);

        LinkedHashSet<Integer> hashSet = new LinkedHashSet<>(numbersList);
        
        ArrayList<Integer> listWithoutDuplicates = new ArrayList<>(hashSet);
        
        System.out.println(listWithoutDuplicates);
    }
}

Program Output.

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

2. Remove duplicates in arraylist – Java 8

To remove the duplicates from the arraylist, we can use the java 8 stream api as well. Use steam’s distinct() method which returns a stream consisting of the distinct elements comparing by object’s equals() method.

Collect all district elements as List using Collectors.toList().

Java program to remove duplicates from arraylist in java without using Set.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class ArrayListExample 
{
    public static void main(String[] args) 
    {
        // ArrayList with duplicate elements
        ArrayList<Integer> numbersList = new ArrayList<>(Arrays.asList(1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8));
        
        System.out.println(numbersList);

        List<Integer> listWithoutDuplicates = numbersList.stream().distinct().collect(Collectors.toList());
        
        System.out.println(listWithoutDuplicates);
    }
}

Program Output.

[1, 1, 2, 3, 3, 3, 4, 5, 6, 6, 6, 7, 8]
[1, 2, 3, 4, 5, 6, 7, 8]

Drop me your questions related to how to remove duplicate objects in arraylist in Java.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Gopi

    July 12, 2020

    Would this approach work, if my arraylist has custom objects like below

    [ {1,”Alex”,”India”},{1,”Alex”,”India”},{2,”Peter”,”India”},{3,”Alex”,”India”}]

    The above list has one duplicate. How can I filter that out. I have to check all the fields to be same to consider that as duplicate.

    • Lokesh Gupta

      July 13, 2020

      That’s true.

  2. Daniel D

    September 19, 2019

    in order to you `stream.distinct()` the stream has to be ordered, so for a stream of unordered values, the `distinct()` would not work

    • Sudipta Saha

      September 23, 2019

      No it works!

  3. Anirban Choubey

    July 29, 2019

    public static int[] removeDuplicate(int[] ar) {
    return Arrays.stream(ar).distinct().toArray();
    }

    this would have been much simpler.

    • Lokesh Gupta

      July 30, 2019

      Thanks Anirban for comment. The post is focused on arraylist so “numbersList.stream().distinct().collect(Collectors.toList())” makes more sense.

  4. Susil Rout

    January 5, 2019

    How to remove duplicates from ArrayList without using Collection/loop in java 6.

Comments are closed on this article!

Search Tutorials

ArrayList Methods

  • ArrayList – Introduction
  • ArrayList – add()
  • ArrayList – addAll()
  • ArrayList – clear()
  • ArrayList – clone()
  • ArrayList – contains()
  • ArrayList – ensureCapacity()
  • ArrayList – forEach()
  • ArrayList – get()
  • Arraylist – indexOf()
  • Arraylist – lastIndexOf()
  • ArrayList – listIterator()
  • ArrayList – remove()
  • ArrayList – removeAll()
  • ArrayList – removeIf()
  • ArrayList – retainAll()
  • ArrayList – sort()
  • ArrayList – spliterator()
  • ArrayList – subList()
  • ArrayList – toArray()

ArrayList Examples

  • ArrayList – Initialize arraylist
  • ArrayList – Iteration
  • ArrayList – Add/replace element
  • ArrayList – Add multiple elements
  • ArrayList – Check empty list
  • ArrayList – Remove element
  • ArrayList – Replace element
  • ArrayList – Empty arraylist
  • ArrayList – Synchronized arraylist
  • ArrayList – Compare two lists
  • ArrayList – Remove duplicates
  • ArrayList – Merge two lists
  • ArrayList – Serialization
  • ArrayList – Swap two elements
  • Convert ArrayList to Array
  • Convert Array to ArrayList
  • Convert HashSet to ArrayList
  • Convert LinkedList to ArrayList
  • Convert Vector to ArrayList
  • ArrayList vs LinkedList
  • ArrayList vs Vector

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)