How To Remove Duplicate Elements From ArrayList In Java

Removing duplicate elements from an ArrayList in Java can be achieved through various methods. This guide will cover several approaches to remove duplicates, explain how they work, and provide examples to demonstrate their functionality. Additionally, we will cover a real-world use case to illustrate its application.

Table of Contents

  1. Introduction
  2. Methods to Remove Duplicates
    • Using a Set
    • Using Streams (Java 8 and above)
    • Using a Custom Method
  3. Examples
    • Using a Set
    • Using Streams
    • Using a Custom Method
  4. Real-World Use Case
  5. Conclusion

Introduction

An ArrayList in Java can contain duplicate elements. However, there are situations where you might need to remove these duplicates to ensure that each element appears only once. This can be accomplished using various techniques, each with its advantages.

Methods to Remove Duplicates

Using a Set

A Set in Java does not allow duplicate elements. By converting the ArrayList to a Set and then back to an ArrayList, you can easily remove duplicates.

Using Streams (Java 8 and above)

Java 8 introduced the Stream API, which provides a functional approach to remove duplicates using the distinct() method.

Using a Custom Method

You can manually iterate through the ArrayList and remove duplicates by checking if an element has already been encountered.

Examples

Using a Set

Example

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class RemoveDuplicatesUsingSet {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");

        // Remove duplicates using a Set
        Set<String> set = new HashSet<>(list);
        list.clear();
        list.addAll(set);

        System.out.println("List after removing duplicates: " + list);
    }
}

Output:

List after removing duplicates: [Apple, Orange, Banana]

Using Streams

Example

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

public class RemoveDuplicatesUsingStreams {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");

        // Remove duplicates using Streams
        list = list.stream().distinct().collect(Collectors.toList());

        System.out.println("List after removing duplicates: " + list);
    }
}

Output:

List after removing duplicates: [Apple, Banana, Orange]

Using a Custom Method

Example

import java.util.ArrayList;
import java.util.List;

public class RemoveDuplicatesUsingCustomMethod {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Apple");
        list.add("Orange");
        list.add("Banana");

        // Remove duplicates using a custom method
        list = removeDuplicates(list);

        System.out.println("List after removing duplicates: " + list);
    }

    public static <T> List<T> removeDuplicates(List<T> list) {
        List<T> newList = new ArrayList<>();
        for (T element : list) {
            if (!newList.contains(element)) {
                newList.add(element);
            }
        }
        return newList;
    }
}

Output:

List after removing duplicates: [Apple, Banana, Orange]

Real-World Use Case

Cleaning Up User Input

In a web application, users might submit forms with duplicate entries. For instance, when selecting interests, users might accidentally select the same interest multiple times. Removing duplicates from the list of interests before storing them can ensure clean data.

Example

import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Set;

public class UserInterestsCleanup {
    public static void main(String[] args) {
        List<String> userInterests = new ArrayList<>();
        userInterests.add("Reading");
        userInterests.add("Traveling");
        userInterests.add("Reading");
        userInterests.add("Cooking");

        // Remove duplicates using a Set
        Set<String> set = new HashSet<>(userInterests);
        userInterests.clear();
        userInterests.addAll(set);

        System.out.println("User interests after removing duplicates: " + userInterests);
    }
}

Output:

User interests after removing duplicates: [Reading, Traveling, Cooking]

Conclusion

Removing duplicate elements from an ArrayList in Java can be done in several ways, including using a Set, streams, or custom methods. Each method has its benefits and can be chosen based on the specific requirements of your application. Understanding these methods helps you maintain clean and efficient collections in your Java programs.

Comments