How to Ensure the Uniqueness of Elements in a List in Java?

How to Ensure the Uniqueness of Elements in a List in Java? A Complete Guide

When working with lists in Java, one of the most common requirements is ensuring that the list contains only unique elements. By default, lists such as ArrayList in Java allow duplicate elements. However, sometimes, you may need to make sure that each item in the list is unique. In this article, we’ll explore several approaches to ensure uniqueness in a list, including the use of HashSet, LinkedHashSet, and ArrayList itself with some additional tricks.

1. Using HashSet to Ensure Uniqueness

The easiest way to ensure uniqueness in a collection is by using a HashSet. A HashSet is a part of Java’s Collections Framework and does not allow duplicate values. This is the most straightforward solution to remove duplicates from a list.

Here is a simple example of how you can use HashSet to ensure that your list contains unique elements:

            
public class EnsureUniqueness {
    public static void main(String[] args) {
        List numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(2);
        numbers.add(3);
        numbers.add(3);
        numbers.add(4);

        // Convert List to Set to remove duplicates
        Set uniqueNumbers = new HashSet<>(numbers);

        System.out.println("Unique numbers: " + uniqueNumbers);
    }
}
            
        

In this code:

  • The list numbers contains some duplicate values.
  • The HashSet automatically removes the duplicates when the list is converted to a set.
  • The output will show only the unique values in the list.

Output: Unique numbers: [1, 2, 3, 4]

2. Using LinkedHashSet to Maintain Order

If maintaining the order of elements is important in addition to ensuring uniqueness, LinkedHashSet is a better choice. A LinkedHashSet preserves the insertion order of elements while removing duplicates.

Here’s an example using LinkedHashSet:

            
public class EnsureUniquenessWithOrder {
    public static void main(String[] args) {
        List numbers = new ArrayList<>();
        numbers.add(5);
        numbers.add(2);
        numbers.add(5);
        numbers.add(1);
        numbers.add(3);
        
        // Using LinkedHashSet to maintain order and remove duplicates
        Set uniqueNumbers = new LinkedHashSet<>(numbers);

        System.out.println("Unique numbers in order: " + uniqueNumbers);
    }
}
            
        

In this example:

  • LinkedHashSet ensures that duplicates are removed, but the order of insertion is maintained.
  • The original list is processed in the order of the elements being added.

Output: Unique numbers in order: [5, 2, 1, 3]

3. Using Java Streams to Filter Duplicates

Java 8 introduced Streams, which allow us to work with collections in a more functional programming style. You can use the distinct() method of Streams to remove duplicates from a list.

Here’s an example using Java Streams:

            
import java.util.*;
import java.util.stream.*;

public class EnsureUniquenessWithStreams {
    public static void main(String[] args) {
        List numbers = Arrays.asList(4, 5, 6, 6, 7, 8, 7, 9);

        // Using Java Streams to remove duplicates
        List uniqueNumbers = numbers.stream()
                                              .distinct()
                                              .collect(Collectors.toList());

        System.out.println("Unique numbers using Streams: " + uniqueNumbers);
    }
}
            
        

Explanation:

  • numbers.stream() converts the list into a Stream.
  • distinct() filters out duplicate values.
  • collect(Collectors.toList()) collects the unique elements back into a list.

Output: Unique numbers using Streams: [4, 5, 6, 7, 8, 9]

4. Removing Duplicates from an ArrayList in Place

Sometimes, you may want to remove duplicates from an existing ArrayList in place, without creating a new set or list. To do this, you can use a combination of HashSet and the List.removeAll() method.

Here’s an example of removing duplicates in place:

            
import java.util.*;

public class RemoveDuplicatesInPlace {
    public static void main(String[] args) {
        List numbers = new ArrayList<>();
        numbers.add(3);
        numbers.add(3);
        numbers.add(6);
        numbers.add(9);
        numbers.add(6);

        // Remove duplicates in place
        Set set = new HashSet<>(numbers);
        numbers.clear();
        numbers.addAll(set);

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

Explanation:

  • We first convert the list to a HashSet to remove duplicates.
  • The clear() method is used to remove all elements from the original list.
  • The addAll() method is used to add the unique elements back into the original list.

Output: List after removing duplicates: [9, 6, 3]

Conclusion

Ensuring the uniqueness of elements in a Java list is a common requirement when dealing with collections. There are several ways to achieve this:

  • HashSet for simple removal of duplicates.
  • LinkedHashSet if you want to maintain the insertion order.
  • Java Streams (distinct()) for a functional approach.
  • In-place removal of duplicates from an ArrayList using a HashSet.

Choose the method that best fits your requirements based on factors like performance and the need to preserve order.

© 2025 Tech Interview Guide. All rights reserved.

Please follow and like us:

Leave a Comment