Java Top

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


1. Introduction

In this quick guide, we're going to discuss the performance of the contains() method available in java.util.HashSet and java.util.ArrayList. They are both collections for storing and manipulating objects.

HashSet is a collection for storing unique elements. To learn more about the HashSet, check out this link.

ArrayList is a popular implementation of the java.util.List interface.

We have an extended article about the ArrayList available here.

2. HashSet.contains()

Internally, the HashSet implementation is based on a HashMap instance. The contains() method calls HashMap.containsKey(object).

Here, it's checking whether the object is in the internal map or not. The internal map stores data inside of the Nodes, known as buckets. Each bucket corresponds to a hash code generated with hashCode() method. So contains() is actually using hashCode() method to find the object's location.

Now let's determine the lookup time complexity. Before moving ahead, make sure you are familiar with Big-O notation.

On average, the contains() of HashSet runs in O(1) time. Getting the object's bucket location is a constant time operation. Taking into account possible collisions, the lookup time may rise to log(n) because the internal bucket structure is a TreeMap.

This is an improvement from Java 7 which used a LinkedList for the internal bucket structure. In general, hash code collisions are rare. So we can consider the elements lookup complexity as O(1).

3. ArrayList.contains()

Internally, ArrayList uses the indexOf(object) method to check if the object is in the list. The indexOf(object) method iterates the entire array and compares each element with the equals(object) method.

Getting back to complexity analysis, the ArrayList.contains() method requires O(n) time. So the time we spend to find a specific object here depends on the number of items we have in the array.

4. Benchmark Testing

Now, let's warm up the JVM with the performance benchmark test. We'll use the JMH (Java Microbenchmark Harness) OpenJDK product. To learn more about setup and execution, check out our useful guide.

To start, let's create a simple CollectionsBenchmark class:

@Warmup(iterations = 5)
public class CollectionsBenchmark {

    public static class MyState {
        private Set<Employee> employeeSet = new HashSet<>();
        private List<Employee> employeeList = new ArrayList<>();

        private long iterations = 1000;

        private Employee employee = new Employee(100L, "Harry");

        public void setUp() {

            for (long i = 0; i < iterations; i++) {
                employeeSet.add(new Employee(i, "John"));
                employeeList.add(new Employee(i, "John"));


Here, we create and initialize HashSet and an ArrayList of Employee objects:

public class Employee {

    private Long id;
    private String name;

    // constructor and getter setters go here

We add the employee = new Employee(100L, “Harry”) instance as the last elements to both collections. So we test the employee object's lookup time for the worst possible case.

@OutputTimeUnit(TimeUnit.NANOSECONDS) indicates that we want the results in nanoseconds. The number of default @Warmup iterations are 5 in our case. The @BenchmarkMode is set to Mode.AverageTime, which means we're interested in calculating an average running time. For the first execution, we put iterations = 1000 items in our collections.

After, we add our benchmark methods to the CollectionsBenchmark class:

public boolean testArrayList(MyState state) {
    return state.employeeList.contains(state.employee);

Here we check whether the employeeList contains employee object.

Likewise, we have the familiar test for employeeSet:

public boolean testHashSet(MyState state) {
    return state.employeeSet.contains(state.employee);

Finally, we can run the test:

public static void main(String[] args) throws Exception {
    Options options = new OptionsBuilder()
    new Runner(options).run();

Here are the results:

Benchmark                           Mode  Cnt     Score     Error  Units
CollectionsBenchmark.testArrayList  avgt   20  4035.646 ± 598.541  ns/op
CollectionsBenchmark.testHashSet    avgt   20     9.456 ±   0.729  ns/op

We can clearly see that the testArrayList method has 4035.646 ns average lookup score, while the testHashSet performs faster with 9.456 ns on average.

Now, let's increase the elements count in our test and run it for iterations = 10.000 items:

Benchmark                           Mode  Cnt      Score       Error  Units
CollectionsBenchmark.testArrayList  avgt   20  57499.620 ± 11388.645  ns/op
CollectionsBenchmark.testHashSet    avgt   20     11.802 ±     1.164  ns/op

Here also, the contains() in HashSet has a huge performance advantage over the ArrayList.

5. Conclusion

This quick write-up explains the performance of the contains() method of the HashSet and ArrayList collections. With the help of the JMH benchmarking, we've presented the performance of contains() for each type of collection.

As a conclusion, we can learn, that the contains() method works faster in HashSet compared to an ArrayList.

As usual, the complete code for this article is over on GitHub project.


Java bottom

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

Comments are closed on this article!