top of page

Removing duplicates from a List

When working with lists, sometimes you only want them to contain distinct elements. There are two options to create lists without duplicates: either to not add these from the start, or, if that is not possible or too difficult, to remove the duplicates from the list later on. Let’s see how you can do both of these.

First of all, we need to create an empty list where we will add the items. For simplicity reasons, these examples will use a Java List of String elements. The corresponding import needed for working with Lists is:

import java.util.List;

Creating the empty List (where we will later store the needed items) can be done as follows:

List<String> theList = new ArrayList<>();

Now, we need to add content to this list. Let’s say, again for example purposes, that the list holds the text retrieved from web page elements with Selenium. That means there is a List of WebElements that needs to be iterated, so that the text for each of these WebElements will be extracted and added to the List we just created (‘theList’). Let this list of WebElements be named ‘listOfWebElements’, and let’s pretend it is not empty, so we can iterate it to get the text we want.

for (WebElement element : listOfWebElements) {
     if (!theList.contains(element.getText()))

In this case the generated List, ‘theList’, only contains unique values. How is that done? The ‘contains()’ method from the List class returns true only if the List already contains the item you are using it with, at least once. If this items is already in the List, it will not be added again. An item is added only if it is not already present in the List.

However, if for some reason you cannot use the ‘contains()’ method to ensure the uniqueness of the item you want to add to the List, you can remove duplicates from an already populated List, using Java streams (when using Java 8 and above). Creating a new List (‘newList’ below) out of all the unique items from an initial List (‘listWithDuplicates’ below) can be done as follows:

List<String> newList =;

Recent Posts

See All

Creating an Architecture for Your Automated Tests Writing Automated Tests in Small Increments 4 Times Your Automation Passed While Bugs Were Present Now That You’ve Created Automated Tests, Run Them!

This week my newest article was released, this time on how to use Log4j to log relevant test automation information:

bottom of page