Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Overview

We know that an ArrayList can contain duplicate values in Java.

In this quick tutorial, we’ll explore a few techniques for obtaining unique values from an ArrayList in Java.

2. Introduction to the Problem

Sometimes, we need to extract unique values from an ArrayList — for example, to enhance data analysis, improve efficiency, or simplify further processing.

Let’s say we have a list carrying some operating system names:

List<String> MY_LIST = Arrays.asList(new String[]{
  "Microsoft Windows",
  "Mac OS",
  "GNU Linux",
  "Free BSD",
  "GNU Linux",
  "Mac OS"});

In the code above, we’ve initialized the MY_LIST ArrayList from an Array. We aim to get a list of unique operating system names from MY_LIST.

We’ll discuss two different approaches to solving the problem. So, for simplicity, we’ll use unit tests and AssertJ assertions to verify if each approach produces the expected result.

Next, let’s see them in action.

3. Using a Set to Eliminate Duplicate Elements

One important difference between the Set and List interfaces is that a Set cannot hold duplicate elements. Therefore, to get MY_LIST‘s unique elements, we can first convert MY_LIST to a Set and then convert the Set back to a List.

Let’s create a test to see how this works:

List<String> result = new ArrayList<>(new HashSet<>(MY_LIST));
assertThat(result).containsExactlyInAnyOrder("Free BSD", "Microsoft Windows", "Mac OS", "GNU Linux");

Sharp eyes may have spotted that we’ve used AssertJ’s containsExactlyInAnyOrder() method for the verification. This is because we converted MY_LIST to a HashSet, and HashSet doesn’t maintain insertion order.

When the insertion order should be preserved, we can convert the list to a LinkedHashSet instead:

result = new ArrayList<>(new LinkedHashSet<>(MY_LIST));
assertThat(result).containsExactly("Microsoft Windows", "Mac OS", "GNU Linux", "Free BSD");

As we can see, this time, we used the containsExactly() method to verify the result. It checks not only the element values but also their orders.

4. Using the Stream API

The Stream API is one significant new feature of Java 8. It allows us to handle a collection of elements.

To remove duplicates from a stream, we can simply call the distinct() method:

List<String> result = MY_LIST.stream()
  .distinct()
  .collect(toList());
assertThat(result).containsExactly("Microsoft Windows", "Mac OS", "GNU Linux", "Free BSD");

The test passes when we give it a run.

It’s worth mentioning that Collectors.toList() always preserves the stream’s origin order unless we turn the stream into the unordered mode, for example, by calling unordered() or converting it to a HashSet using Collectors.toSet(). Therefore, we verify the result list using the containsExactly() method.

5. Conclusion

Obtaining unique values from a list is a common requirement in Java development. In this article, we’ve delved into two approaches to solving this problem:

  • Convert the List to a Set and then convert the Set back to a List
  • Use the Stream API’s distinct() functionality

Through comprehensive examples, we’ve showcased how these techniques can be applied to extract distinct elements from the list efficiently.

Additionally, we’ve discussed methods to maintain the order of elements in the resulting list, aligning it with the original input list.

As usual, all code snippets presented here are available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
1 Comment
Oldest
Newest
Inline Feedbacks
View all comments
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.