java - Maximum size of HashSet, Vector, LinkedList -


what maximum size of hashset, vector, linkedlist? know arraylist can store more 3277000 numbers.

however size of list depends on memory (heap) size. if reaches maximum jdk throws outofmemoryerror.

but don't know limit number of elements in hashset, vector , linkedlist.

there no specified maximum size of these structures.

the actual practical size limit somewhere in region of integer.max_value (i.e. 2147483647, 2 billion elements), that's maximum size of array in java.

  • a hashset uses hashmap internally, has same maximum size
    • a hashmap uses array has size power of two, can @ 230 = 1073741824 elements big (since next power of 2 bigger integer.max_value).
    • normally number of elements @ number of buckets multiplied load factor (0.75 default). however, when hashmap stops resizing, still allow add elements, exploiting fact each bucket managed via linked list. therefore limit elements in hashmap/hashset memory.
  • a vector uses array internally has maximum size of integer.max_value, can't support more many elements
  • a linkedlist doesn't use array underlying storage, doesn't limit size. uses classical doubly linked list structure no inherent limit, size only bounded available memory. note linkedlist report size wrongly if bigger integer.max_value, because uses int field store size , return type of size() int well.

note while collection api does define how collection more integer.max_value elements should behave. importantly states the size() documentation:

if collection contains more integer.max_value elements, returns integer.max_value.

note while hashmap, hashset , linkedlist seem support more integer.max_value elements, none of implement size() method in way (i.e. let internal size field overflow).

this leads me believe other operations also aren't well-defined in condition.

so i'd it's safe use general-purpose collections up to integer.max_vlaue elements. if know you'll need store more that, should switch dedicated collection implementations support this.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -