How Can You Use Streams to Find Distinct Elements in a Collection in Java?

How Can You Use Streams to Find Distinct Elements in a Collection in Java?

Learn how to remove duplicates and find distinct values in Java collections using the powerful Streams API.

Introduction

Handling collections in Java often involves removing duplicates or finding unique elements. Traditionally, developers would manually filter duplicates using loops or by converting collections into sets. However, with the introduction of Streams in Java 8, this task has become significantly easier and more efficient.

Java Streams provide a more declarative way to find distinct elements from a collection, making the code concise, easy to understand, and efficient. In this guide, we will explore how to use Streams to filter and find distinct elements from various types of collections, such as Lists, Sets, and Arrays. We will also cover some practical examples, highlighting how the distinct() method can be used effectively.

What is Java Stream?

A Stream in Java represents a sequence of elements supporting sequential and parallel aggregate operations. Streams can be created from various data sources such as collections, arrays, or I/O channels, and they provide a way to process data in a functional and declarative manner. The Stream API allows you to perform operations like filtering, mapping, and reducing without the need for explicit iteration.

The distinct() method in Java Streams is specifically designed to remove duplicate elements from a collection, making it easier to find unique values. In this article, we’ll take a closer look at how this method works and explore various ways to use it.

How to Find Distinct Elements Using Streams in Java?

The distinct() method is used to eliminate duplicate elements in a stream. It returns a stream consisting of the distinct elements of the original stream. The method works by applying the equals() method of the objects in the stream to determine if two elements are equal. If they are, only one element is retained in the stream.

Example: Finding Distinct Elements from a List

Let’s begin with a simple example where we find distinct elements in a list of integers:

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

        public class DistinctExample {
            public static void main(String[] args) {
                List numbers = Arrays.asList(1, 2, 2, 3, 4, 5, 1, 6, 3);
                
                // Using Streams to find distinct elements
                List distinctNumbers = numbers.stream()
                                                        .distinct()
                                                        .collect(Collectors.toList());
                
                System.out.println("Distinct numbers: " + distinctNumbers);
            }
        }
        

Output:

        Distinct numbers: [1, 2, 3, 4, 5, 6]
        

In this example, we used the stream() method to convert the list into a stream. We then applied the distinct() method to remove duplicate elements, followed by the collect() method to convert the result back into a list.

Example: Finding Distinct Elements from a Set

Now let’s look at an example where we use a Set to find distinct elements. A Set by nature does not allow duplicates, but we can still demonstrate the use of Streams to find distinct elements in a collection if necessary:

        import java.util.HashSet;
        import java.util.Set;
        import java.util.stream.Collectors;

        public class DistinctSetExample {
            public static void main(String[] args) {
                Set numbersSet = new HashSet<>();
                numbersSet.add(1);
                numbersSet.add(2);
                numbersSet.add(2);
                numbersSet.add(3);
                numbersSet.add(4);

                // Using Streams to find distinct elements
                Set distinctNumbersSet = numbersSet.stream()
                                                           .distinct()
                                                           .collect(Collectors.toSet());
                
                System.out.println("Distinct numbers in set: " + distinctNumbersSet);
            }
        }
        

Output:

        Distinct numbers in set: [1, 2, 3, 4]
        

Even though a Set inherently removes duplicates, the distinct() method can still be used on collections, ensuring that no duplicates remain if the input collection has any.

Example: Finding Distinct Elements from an Array

Streams can also be used to find distinct elements in an array. Here’s an example that shows how to achieve this:

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

        public class DistinctArrayExample {
            public static void main(String[] args) {
                int[] numbersArray = {1, 2, 2, 3, 4, 5, 1, 6, 3};
                
                // Using Streams to find distinct elements from an array
                int[] distinctNumbersArray = Arrays.stream(numbersArray)
                                                   .distinct()
                                                   .toArray();
                
                System.out.println("Distinct numbers in array: " + Arrays.toString(distinctNumbersArray));
            }
        }
        

Output:

        Distinct numbers in array: [1, 2, 3, 4, 5, 6]
        

In this case, we converted the array into a stream using Arrays.stream() and then used distinct() to filter out duplicates. Finally, we converted the stream back into an array using toArray().

Performance Considerations

While Streams are a powerful tool for collection manipulation, it’s essential to consider performance when dealing with large datasets. The distinct() method operates by comparing elements using the equals() method, which may incur a performance cost for certain data types.

For instance, if you are working with a collection of custom objects, ensure that the equals() and hashCode() methods are properly overridden to optimize performance. A poor implementation of these methods can lead to inefficient duplicate elimination.

Best Practices for Finding Distinct Elements

  • Use distinct() method when you want to remove duplicates from any collection or stream.
  • When working with large datasets, consider the performance of custom equals() and hashCode() implementations.
  • For immutable collections, prefer Set or use distinct() only when necessary to minimize overhead.
  • Leverage parallel streams for performance improvements when processing large datasets.

Conclusion

Java Streams offer an efficient and easy way to handle collection manipulation tasks, such as finding distinct elements. By leveraging the distinct() method, you can quickly eliminate duplicates and retrieve unique values from collections like Lists, Sets, and Arrays. Streams enhance the readability and maintainability of code, promoting a more functional and declarative approach to programming in Java.

© 2025 Tech Interview Guide. All rights reserved.

Please follow and like us:

Leave a Comment