Sunday, 6 November 2011

Java Collections – Performance (Time Complexity)


Many developers I came across in my career as a software developer are only familiar with the most basic data structures, typically, Array, Map and Linked List. These are fundamental data structures and one could argue that they are generic enough to fit most of the commercial software requirements. But what worries me most is that even seasoned developers are not familiar with the vast repertoire of available data structures and their time complexity. In this post the ADTs (Abstract Data Types) present in the Java Collections (JDK 1.6) are enlisted and the performance of the various data structures, in terms of time, is assessed.

Before we start it is helpful to understand the so-called “Big O” notation. This notation approximately describes how the time to do a given task grows with the size of the input. Roughly speaking, on one end we have O(1) which is “constant time” and on the opposite end we have O(xn) which is “exponential time”. The following chart summarizes the growth in complexity due to growth of input (n). In our data structure walk-through we sometimes use the symbol h to signify the Hash Table capacity.


List

A list is an ordered collection of elements.


Add
Remove
Get
Contains
Data  Structure
ArrayList
 O(1)
O(n)
O(1)
O(n)
Array
LinkedList
 O(1)
O(1)
O(n)
O(n)
Linked List
CopyonWriteArrayList
 O(n)
O(n)
O(1)
O(n)
Array

Set

A collection that contains no duplicate elements.


Add
Contains
Next
Data Structure
HashSet
O(1)
O(1)
O(h/n)
Hash Table
LinkedHashSet
O(1)
O(1)
O(1)
Hash Table + Linked List
EnumSet
O(1)
O(1)
O(1)
Bit Vector
TreeSet
O(log n)
O(log n)
O(log n)
Red-black tree
CopyonWriteArraySet
O(n)
O(n)
O(1)
Array
ConcurrentSkipList
O(log n)
O(log n)
O(1)
Skip List

Queue

A collection designed for holding elements prior to processing.


Offer
Peak
Poll
Size
Data Structure
PriorityQueue
O(log n )
O(1)
O(log n)
O(1)
Priority Heap
LinkedList
 O(1)
O(1)
O(1)
O(1)
Array
ArrayDequeue
 O(1)
O(1)
O(1)
O(1)
Linked List
ConcurrentLinkedQueue
 O(1)
O(1)
O(1)
O(n)
Linked List
ArrayBlockingQueue
 O(1)
O(1)
O(1)
O(1)
Array
PriorirityBlockingQueue
O(log n)
O(1)
O(log n)
O(1)
Priority Heap
SynchronousQueue
O(1)
O(1)
O(1)
O(1)
None!
DelayQueue
O(log n)
O(1)
O(log n)
O(1)
Priority Heap
LinkedBlockingQueue
O(1)
O(1)
O(1)
O(1)
Linked List

Map

An object that maps keys to values. A map cannot duplicate keys; each key can map to at most one value.


Get
ContainsKey
Next
Data Structure
HashMap
O(1)
O(1)
O(h / n)
Hash Table
LinkedHashMap
O(1)
O(1)
O(1)
Hash Table + Linked List
IdentityHashMap
O(1)
O(1)
O(h / n)
Array
WeakHashMap
O(1)
O(1)
O(h / n)
Hash Table
EnumMap
O(1)
O(1)
O(1)
Array
TreeMap
O(log n)
O(log n)
O(log n)
Red-black tree
ConcurrentHashMap
O(1)
O(1)
O(h / n)
Hash Tables
ConcurrentSkipListMap
O(log n)
O(log n)
O(1)
Skip List

1,354 comments:

  1. I second that. The element should be found in the list before it can be removed by changing the pointers so it is O(n).

    ReplyDelete
  2. every queue implementation has O(N) on contains() method?

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Array and LinkedList has O(n) on contains() method for sure. If element is not in array or list you have to traverse all elements just to be sure. In Priority Heap as name suggest use some kind of heap (couldn't find exactly which, so I will assume it is binary heap), which is data structure similar to binary tree, with special rules. Contains() on binary heap takes O(log n).

      Delete
  3. Linked List not use Array Data Structure

    ReplyDelete
  4. It is common to just write how long removal itself will take without actual search. You have to take it as: if you have pointer to object o in linked list then removal will take O(1).
    It is done this way, so you can see difference between different collections:
    ArrayList has remove O(n) + search, while LinkedList have O(1)+ search. Removal would have O(n) complexity for both even though LinkedList removal is way faster.

    ReplyDelete
  5. How come LinkedList use Arrays ????

    Big question. Please correct...

    ReplyDelete
  6. Thanks for providing this informative information. it is very useful you may also refer- http://www.s4techno.com/blog/2016/07/12/exception-handling/

    ReplyDelete
  7. perfect explanation about java programming .its very useful.thanks for your valuable information.java training in chennai | java training in velachery

    ReplyDelete
  8. For niit projects, assignments, cycle tests, lab@homes, c#, html, java, java script, sql, oracle and much more visit http://gniithelp.blogspot.in or https://mkniit.blogspot.in

    ReplyDelete
  9. Being new to the blogging world I feel like there is still so much to learn. Your tips helped to clarify a few things for me as well as giving..
    Android App Development Company
    Android App Development Company

    ReplyDelete
  10. Really useful. Thanks for sharing.

    ReplyDelete
  11. Really useful. Thanks for sharing.

    ReplyDelete
  12. Thank you for nice article but I just want some more explanation because I am prepring this for my interview, like reason for each data structure complexity.good explained for arraylist and linkedlist but I need this type of explanation for each is it possible here??

    ReplyDelete
  13. Many of this runtimes are not correct. One should distinguish between per-operation, amortized and (stochastic) expected worst-case runtimes. The runtimes here are mixed up and always choose the beset of the three.

    E.g. ArrayList#add has a worst case complexity of O(n) (array size doubling), but the amortized complexity over a series of operations is in O(1). HashSet#contains has a worst case complexity of O(n) (<= Java 7) and O(log n) otherwise, but the expected complexity is in O(1).

    ReplyDelete
    Replies
    1. In most of the cases here, amortized time complexity is mentioned.

      Delete
  14. Thanks a lot! You made a new blog entry to answer my question; I really appreciate your time and effort.
    java training in chennai |
    java training institute in chennai

    ReplyDelete
  15. Thanks for sharing with us the information on Java collections and I have learned a lot of new programming information from the article that has helped me to improve my basic programming skills. I Will be recommending this site to clients who access our Papers Reviewing Services so that they can read the article.

    ReplyDelete
  16. A easy and exciting blog about java learning. Please comment your opinions and share..
    http://foundjava.blogspot.in

    ReplyDelete
  17. Good Article
    One Correction, HashSet internal uses data structure HashMap not Hash Table.
    For More Technical Blog visit http://www.prabhatkashyap.com/

    ReplyDelete
  18. Good post and I like it very much. By the way, anybody try this increase app downloads? I do not how to use.

    ReplyDelete
  19. It is amazing and wonderful to visit your site. Thanks for sharing this information; this is useful to everyone..
    Read more about Java training in delhi, java programming in delhi

    ReplyDelete
  20. I agree with Robin but the problem is that it is kind of misleading. If a less "seasoned" programmer sees the chart then he will immediately assume that removing an element in LinkedList will just be O(1). An extra column needs to be put or a simple explanation should be given in the opening paragraphs.
    Otherwise, great post!

    ReplyDelete
  21. What does “Next” mean for Set and Map? There is no such operation. If you mean the next() method of their Iterators, then the complexities are dead wrong. Iterators keep a reference to the current node, so it’s always O(1) for the hash maps instead of O(h / n).

    ReplyDelete
  22. This comment has been removed by the author.

    ReplyDelete
  23. [url=http://kataku.pw]berita terkeren seindonesia[/url]

    ReplyDelete
  24. This is not clear at all.
    You have to specify that all of Big-O you are mentioning is the best case.
    For example: get in HashMap in Java
    + best case: O(1)
    + worst case: O(n) or O(logn) - depends on Java SDK version.

    ReplyDelete
  25. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.
    Click here:
    angularjs training in online

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Your new valuable key points imply much a person like me and extremely more to my office workers. With thanks from every one of us.

    Best AWS Training in Chennai | Amazon Web Services Training in Chennai

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    ReplyDelete
  28. I would assume that we use more than the eyes to gauge a person's feelings. Mouth. Body language. Even voice. You could at least have given us a face in this test.
    java training in annanagar | java training in chennai

    java training in marathahalli | java training in btm layout

    java training in rajaji nagar | java training in jayanagar

    java training in chennai

    ReplyDelete
  29. I ‘d mention that most of us visitors are endowed to exist in a fabulous place with very many wonderful individuals with very helpful things.
    nebosh course in chennai

    ReplyDelete
  30. Nice tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs Training in online

    angularjs Training in marathahalli

    ReplyDelete
  31. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    Amazon Web Services Training in Pune | Best AWS Training in Pune

    AWS Online Training | Online AWS Certification Course - Gangboard

    ReplyDelete
  32. I third that. Seasoned programmers wouldn't even say that the remove method runs in constant time because no seasoned programmer only cares about the singular action the method makes. They care about everything that lead up to the action and that proceeded it. If you only cared about the main action of a method, everything would operate in constant time.

    ReplyDelete
  33. Also, add() is only constant time if it's added to the beginning or end of a list. This post is super inaccurate.

    ReplyDelete
  34. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram

    ReplyDelete
  35. I have read your blog its very attractive and impressive. I like your blog core Java online course

    ReplyDelete
  36. That was a great message in my carrier, and It's wonderful commands like mind relaxes with understand words of knowledge by information's.
    python interview questions and answers | python tutorials

    ReplyDelete
  37. Really you have done great job,There are may person searching about that now they will find enough resources by your post
    Devops Training courses
    Devops Training in Bangalore
    Best Devops Training in pune
    Devops interview questions and answers

    ReplyDelete
  38. Nice tips. Very innovative... Your post shows all your effort and great experience towards your work Your Information is Great if mastered very well.

    Java interview questions and answers | Core Java interview questions and answers

    ReplyDelete
  39. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa interview questions and answers
    automation anywhere interview questions and answers
    blueprism interview questions and answers
    uipath interview questions and answers
    rpa training in chennai

    ReplyDelete
  40. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in velachery| rpa training in tambaram |rpa training in sholinganallur | rpa training in annanagar| rpa training in kalyannagar

    ReplyDelete
  41. Inspiring writings and I greatly admired what you have to say , I hope you continue to provide new ideas for us all and greetings success always for you..Keep update more information..
    Data Science training in Chennai | Data science training in bangalore

    Data science training in pune | Data science online training

    Data Science Interview questions and answers

    ReplyDelete
  42. Look some more information
    https://theprogrammersfirst.blogspot.com/2017/10/data-structure-performance-and-time.html

    ReplyDelete
  43. It’s always so sweet and also full of a lot of fun for me personally and my office colleagues to search you blog a minimum of thrice in a week to see the new guidance you have got.
    iosh course in chennai

    ReplyDelete
  44. Goyal packers and movers in Panchkula is highly known for their professional and genuine packing and moving services. We are top leading and certified relocation services providers in Chandigarh deals all over India. To get more information, call us.


    Packers and movers in Chandigarh
    Packers and movers in Panchkula
    Packers and movers in Mohali
    Packers and movers in Zirakpur
    Packers and movers in Patiala
    Packers and movers in Ambala
    Packers and movers in Ambala cantt
    Packers and movers in Pathankot
    Packers and movers in Jalandhar
    Packers and movers in Ludhiana

    ReplyDelete
  45. Am also agree with you but we have many features in java8 version to solve these type of issues. And Collections are more important topic in programming language. If we need to send more objects at a time as return a value then we use collections.
    To know more about the java language just go through this website to learn more online AWS Developer courses

    ReplyDelete
  46. Great!it is really nice blog information.after a long time i have grow through such kind of ideas.
    thanks for share your thoughts with us.
    AWS Course in Anna Nagar
    Best AWS Training Institute in Anna nagar
    AWS Courses in T nagar
    AWS Training Institutes in T nagar

    ReplyDelete
  47. Very nice post here thanks for it .I always like and such a super contents of these post.Excellent and very cool idea and great content of different kinds of the valuable information's.
    machine learning training in chennai
    machine learning training in omr
    top institutes for machine learning in chennai
    Android training in chennai
    PMP training in chennai

    ReplyDelete
  48. Remove if you're passing in the ListNode, it is indeed O(1). If you remove by index, then it is O(n)

    ReplyDelete
  49. Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
    Best Devops Training in pune
    Data science training in Bangalore

    ReplyDelete
  50. This comment has been removed by the author.

    ReplyDelete
  51. I am waiting for your more posts like this or related to any other informative topic.
    Aws Training
    Cognos Training

    ReplyDelete
  52. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.
    All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    ReplyDelete
  53. This comment has been removed by the author.

    ReplyDelete
  54. This comment has been removed by the author.

    ReplyDelete
  55. Such a wonderful blog on Machine learning . Your blog almost full information about Machine learning .Your content covered full topics of Machine learning that it cover from basic to higher level content of Machine learning . Requesting you to please keep updating the data about Machine learning in upcoming time if there is some addition.
    Thanks and Regards,
    Machine learning tuition in chennai
    Machine learning workshops in chennai
    Machine learning training with certification in chennai

    ReplyDelete
  56. Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

    DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

    Good to learn about DevOps at this time.


    devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

    ReplyDelete
  57. Such a wonderful blog on Machine learning . Your blog have almost full information about Machine learning .Your content covered full topics of Machine learning that it cover from basic to higher level content of Machine learning . Requesting you to please keep updating the data about Machine learning in upcoming time if there is some addition.
    Thanks and Regards,
    Machine learning tuition in chennai
    Machine learning workshops in chennai
    Machine learning training with certification in chennai

    ReplyDelete
  58. Thanks for providing wonderful information with us. Thank you so much.
    Regards,
    Best Devops Training Institute in Chennai

    ReplyDelete
  59. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definitely interested in this one. Just thought that I would post and let you know. Nice! thank you so much! Thank you for sharing.
    Website Development Company in Delhi
    Website Designing Company in Delhi
    Mobile App Development Company
    Mobile App Development Company in India

    ReplyDelete
  60. Such a wonderful blog on Machine learning . Your blog have almost full information about Machine learning .Your content covered full topics of Machine learning that it cover from basic to higher level content of Machine learning . Requesting you to please keep updating the data about Machine learning in upcoming time if there is some addition.
    Thanks and Regards,
    Machine learning tuition in chennai
    Machine learning workshops in chennai
    Machine learning training with certification in chennai

    ReplyDelete
  61. You are doing a great job. I would like to appreciate your work for good accuracy.
    Dotnet Course in Chennai

    ReplyDelete
  62. Amazing Post, Thank you for sharing this post really this is awesome and very useful.

    Cheers!

    WhatsApp Group Join Link List

    ReplyDelete
  63. Awesome work! That is quite appreciated. I hope you’ll get more success.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  64. great job and please keep sharing such an amazing article and its really helpful for us thank you.
    Whatsapp Group Links List

    ReplyDelete
  65. Nice Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    check out : best hadoop training in chennai
    hadoop big data training in chennai
    best institute for big data in chennai
    big data course fees in chennai

    ReplyDelete
  66. This comment has been removed by the author.

    ReplyDelete
  67. Excellent post, it will be definitely helpful for many people. Keep posting more like this.
    Selenium Training in Chennai | SeleniumTraining Institute in Chennai

    ReplyDelete
  68. It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
    Data Science Course in Chennai | Best Data Science Training in Chennai
    Python Course in Chennai | Best Python Training Course Institutes in Chennai
    RPA Course in Chennai | RPA Course Training in Chennai
    Digital Marketing Course in Chennai | Digital Marketing Course Training in Chennai

    ReplyDelete
  69. Such an ideal piece of blog. It’s quite interesting to read content like this. I appreciate your blog
    Data Science Certification

    ReplyDelete
  70. I like your blog, I read this blog please update more content on hacking,Nice post
    Excellent Blog , I appreciate your hardwork ,it is useful
    Tableau Training

    ReplyDelete
  71. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    Microsoft Azure online training
    Selenium online training
    Java online training
    Python online training
    uipath online training

    ReplyDelete
  72. This comment has been removed by the author.

    ReplyDelete
  73. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on
    updating...

    securityguardpedia
    Education

    ReplyDelete
  74. It’s been a amazing article. It’s provide lot’s of information, I really enjoyed to read this. thank u so much
    for your sharing

    best institute for big data in chennai
    best hadoop training in chennaii
    big data course fees in chennai
    hadoop training in chennai cost

    ReplyDelete
  75. Really nice post.provided a helpful information.I hope that you will post more updates like this AWS Online Training

    ReplyDelete
  76. Thanks for posting such an blog it is really very informative. And useful for the freshers Keep posting the
    updates.

    Article submission sites
    Guest posting sites

    ReplyDelete

  77. Its a wonderful post and very helpful, thanks for all this information.
    Java Training in Delhi

    ReplyDelete
  78. Thank you for providing such an informative content. I like the way you publish such an useful post which may help many needful.Professional Web design services are provided by W3BMINDS- Website designer in Lucknow.
    Web development Company | Web design company

    ReplyDelete
  79. This comment has been removed by the author.

    ReplyDelete
  80. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  81. My rather long internet look up has at the end of the day been compensated with pleasant insight to talk about with my family and friends.
    fire and safety course in chennai
    safety course in chennai

    ReplyDelete
  82. many peoples want to join random whatsapp groups . as per your demand we are ready to serve you whatsapp group links . On this website you can join unlimited groups . click and get unlimited whatsapp group links

    ReplyDelete
  83. if you want girls mobile numbers then this website is best for you . you can visit on this website and get their information and you also can meet with thrm and go for a date . click here to use our website --- online dating website

    ReplyDelete
  84. My blog is in the same niche as yours, and my users would benefit from some of the information you provide here. Please let me know if this ok with you. Thank you.
    safety course in chennai
    nebosh course in chennai

    ReplyDelete
  85. A bewildering web journal I visit this blog, it's unfathomably heavenly. Oddly, in this present blog's substance made purpose of actuality and reasonable. The substance of data is informative
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  86. A bewildering web journal I visit this blog, it's unfathomably heavenly. Oddly, in this present blog's substance made purpose of actuality and reasonable. The substance of data is informative
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  87. Software Testing Training in Chennai | Software Testing Training Institute in Chennai
    thanks for Providing a Great Info, if anyone want to learn advance devops tools or devops classroom training visit:

    ReplyDelete
  88. amazing blog layout! How long have you been blogging for? Join Free indian whatsapp group Latest 2019 you make blogging look easy.

    ReplyDelete
  89. In the case that above process still does not help to resolve this error, then contact the QuickBooks Support Number to be of assistance due to the problem and solve it immediately. They generally have a passionate team of experts who were created for all your issues and you will be able to restore your software to its old working condition.

    ReplyDelete
  90. insta dp and story viewer

    instadp story

    instagram profile downloader

    insta dp for girls

    instagram dp quotes

    instagram profile picture ideas

    how to save instagram dp

    instagram profile picture maker

    ReplyDelete
  91. Really helpful post. thanks for sharing this type of information with us. i really appreciated this information. Checkout Best and Cool Whatsapp Group Names
    Checkout Whatsapp Group Names 2019

    ReplyDelete
  92. An astounding web diary I visit this blog, it's inconceivably magnificent. Strangely, in this current blog's substance made point of fact and sensible. The substance of information is instructive.
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  93. Thanks For sharing this post and its was very useful post.keep up the good work.

    Backwater resorts in kerala

    ReplyDelete

  94. Thanks For sharing this post. I really enjoyed to read this.keep up the good work.

    best honeymoon resorts in keral

    ReplyDelete
  95. A befuddling web diary I visit this blog, it's incredibly grand. Strangely, in this present blog's substance made motivation behind fact and sensible. The substance of information is instructive
    Oracle Fusion Financials Online Training
    Oracle Fusion HCM Online Training
    Oracle Fusion SCM Online Training

    ReplyDelete
  96. Thanks for the info And I hope to read this good article again.


    โปรโมชั่นGclub ของทางทีมงานตอนนี้แจกฟรีโบนัส 50%
    เพียงแค่คุณสมัคร Gclub กับทางทีมงานของเราเพียงเท่านั้น
    ร่วมมาเป็นส่วนหนึ่งกับเว็บไซต์คาสิโนออนไลน์ของเราได้เลยค่ะ
    สมัครสมาชิกที่นี่ >>> Gclub online

    ReplyDelete
  97. Thank you for bestowing this information which will enlighten many of us. It’s really helpful for me and the one who wants to know more about mobile app development company.

    ReplyDelete
  98. Yes JavaScript has some complexity I agree with it.
    Samaj Kalyan Hostel Cutoff
    <a heref="https://www.vikasanshil.com/nalanda-university-history</a>

    ReplyDelete
  99. Learn About Google Adsense, SEO, Digital Marketing, Blogging, Etc by The SEO Guider

    ReplyDelete
  100. Thanks for sharing valuable information.It will help everyone.keep Post.
    Dhankesari

    ReplyDelete