Java Collections Performance Comparison

Ahmet YILMAZ
3 min readDec 14, 2022

--

Java Collections is a framework and provide functionality and flexibility for storing and manipulating groups of objects. Although collections are good in terms of functionality and flexibility, they exhibit performance differences among themselves.

Collections are a combination of classes and interfaces.

The figure below shows the Collection hierarchy in detail.

https://en.wikipedia.org/wiki/Java_collections_framework

When to use?

Java collections can be useful when the data types and quantities are unclear because collections provide a way to organize and manage the data in a structured way.

What benefits does it bring me?

Java collections help us perform various operations on data. These are operations such as adding, deleting, manipulating, sorting and checking whether the data is in the collection or not. Java collections can make working with data easier in a consistent and predictable manner.

Tests and Test Results

Below, you can find some test results regarding the performance of Add, Contains, Remove and Clear operations on HashSet, TreeSet, LinkedList, ArrayList, Vector, Stack and PriorityQueue.

Results are shown in nanoseconds.

add method:


========Array Length is: 10000
java.util.HashSet: 1.618.174
java.util.TreeSet: 10.473.095
java.util.LinkedList: 989.235
java.util.ArrayList: 1.090.371
java.util.Vector: 825.284
java.util.Stack: 930.766
java.util.PriorityQueue: 2.008.496


========Array Length is: 100000
java.util.HashSet: 11.999.614
java.util.TreeSet: 23.022.241
java.util.LinkedList: 2.782.422
java.util.ArrayList: 1.075.359
java.util.Vector: 16.099.569
java.util.Stack: 3.245.830
java.util.PriorityQueue: 6.899.759

contains method:

========Array Length is: 10000
java.util.HashSet: 2.220.248
java.util.TreeSet: 5.721.683
java.util.LinkedList: 205.291.619
java.util.ArrayList: 158.914.101
java.util.Vector: 143.935.719
java.util.Stack: 136.925.342
java.util.PriorityQueue: 160.762.991

========Array Length is: 100000
java.util.HashSet: 4.206.225
java.util.TreeSet: 15.407.420
java.util.LinkedList: 55.352.155.143
java.util.ArrayList: 31.931.939.502
java.util.Vector: 32.334.732.907
java.util.Stack: 28.466.556.023
java.util.PriorityQueue: 31.107.495.196

remove method:

========Array Length is: 10000
java.util.HashSet: 1.999.804
java.util.TreeSet: 7.617.586
java.util.LinkedList: 1.124.347
java.util.ArrayList: 5.159.905
java.util.Vector: 6.572.647
java.util.Stack: 4.632.892
java.util.PriorityQueue: 12.709.146

========Array Length is: 100000
java.util.HashSet: 3.508.546
java.util.TreeSet: 20.251.275
java.util.LinkedList: 3.166.817
java.util.ArrayList: 408.159.927
java.util.Vector: 429.917.969
java.util.Stack: 414.395.586
java.util.PriorityQueue: 1.744.120.687

clear method:

========Array Length is: 10000
java.util.HashSet: 470.914
java.util.TreeSet: 2.012.446
java.util.LinkedList: 298.272
java.util.ArrayList: 247.705
java.util.Vector: 425.481
java.util.Stack: 306.173
java.util.PriorityQueue: 444.050

========Array Length is: 100000
java.util.HashSet: 8.180.549
java.util.TreeSet: 34.821.954
java.util.LinkedList: 1.710.619
java.util.ArrayList: 1.405.236
java.util.Vector: 1.933.039
java.util.Stack: 1.507.556
java.util.PriorityQueue: 4.787.362

Conclusion

As the amount of data increases, adding to an ArrayList is generally more performant than other types of collections.

The contains method on a HashSet is very fast because it uses a hash table to quickly look up whether an element is contained in the set.
This is much faster than searching for an element in other types of collections.

The remove operation on a HashSet is very fast because it uses a hash table to quickly find and remove the element from the set.
Similarly, the remove operation on a LinkedList is also very fast because it can remove an element in constant time by simply updating the pointers of the adjacent elements in the list.
In contrast, removing an element from other types of collections, such as an ArrayList, can be slower because it may require shifting elements
in the list to fill the gap left by the removed element.

Such as a LinkedList, the clear method will simply reset the pointers to the beginning and end of the list, so it will be very fast.
For other collections, such as an ArrayList, the clear method will have to loop through each element in the list and set each element to null, which can be slower.
In general, the clear method works fast on all collections.

References:

https://en.wikipedia.org/wiki/Java_collections_framework

TAV Technologies

--

--