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
useshashmap
internally, has same maximum size- a
hashmap
uses array has size power of two, can @ 230 = 1073741824 elements big (since next power of 2 biggerinteger.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 inhashmap
/hashset
memory.
- a
- a
vector
uses array internally has maximum size ofinteger.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. notelinkedlist
report size wrongly if biggerinteger.max_value
, because usesint
field store size , return type ofsize()
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, returnsinteger.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
Post a Comment