Removing duplicates from a List

Published by

on

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()))
          theList.add(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 = listWithDuplicates.stream().distinct().collect(toList());

 

2 responses to “Removing duplicates from a List”

  1. João Farias (@JFThatsABug) Avatar

    If you don’t care about ordering, using the Set interface, you will have better performance:

    newList = new ArrayList(new HashSet(initialList));

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.