Java – treemap vs ArrayList – performance and resources when iterating / adding / editing values
Talk about performance and resources
Which is faster and requires less resources when adding and editing values, ArrayList or treemap?
Or is there any type of data that can beat both? (it must be able to sort the data in some way)
Solution
It depends on what you need
If we are talking about sorting data, because ArrayList is a list / array, if it has been sorted, you can get the value of O (log n) speed But to insert, it is O (n), because you may need to move the entire array when inserting a new element
The treemap data structure is implemented as a red black tree, and the insertion time and search time are o (log n)
So, in short:
I will definitely choose treemap It has the added advantage of making it ready now (you have to implement some code yourself to make ArrayList work)
Note: if the O (n) (called big oh) representation is not obtained, it can be regarded as the formula for the number of seconds required when the structure has n elements Therefore, if you have an ArrayList with 1000 elements (n = 1000), it takes 3 seconds (log 1000 = 3) to find an item there Although it takes 1000 seconds to insert a new element
On the other hand, treemap takes 3 seconds to search and insert