Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Maximum size of Collections

  1. #1
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Maximum size of Collections

    Hi every one!
    I am just not sure of some theory in collections, I'm just a less than a year java programmer.

    Most people in the internet said, ArrayList and LinkedList maximum capacity depends on the memory allocated to the JVM. But according to few people those list has a maximum capacity of Integer.MAX_VALUE.
    According to my experiment, those list has a maximum capacity of Integer.MAX_VALUE since the get method of List accept a parameter of int primitive type (index of element), therefore we can conclude that the maximum capacity of List is equal to Integer.MAX_VALUE.

    But what about the Map? get() method of map accepts object(the key of the map). So does it mean that the maximum capacity of Map depends on the memory allocated to our JVM? Or its maximum size is Integer.MAX_VALUE also just like Lists and Arrays? Is there a way to prove it? Is Map designed to hold infinite number of data (disregarding the heap memory space exception)?

    And also about Stack, Deque? is it also the same as Map (in terms of maximum capacity)?

    Thanks


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Maximum size of Collections

    The maximum size of every collection is different.
    Yes, the ArrayList probably has a maximum size of (Integer.MAX_VALUE - c), but the LinkedList might very well have infinite size.
    You are correct, that the get(int index) method takes an integer and thus you are capped by Integer.MAX_VALUE, but that does not mean that the list itself might not contain more objects then what you can get.
    You could iterate over all elements in the LinkedList and maybe get more then Integer.MAX_VALUE.

    Same is true for maps. Some maps might have a maximum size depending on Integer.MAX_VALUE while others dont. To know for sure you could look at the source code and / or the documentation.
    The interface itself does not make any restrictions, the implementations can deal with this as they please.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Maximum size of Collections

    This is a very good question. I'd say the maximum practical capacity of the data structures is less than or equal to the maximum value of the data type accepted or returned by their size-related methods (int size(), E get(int index), etc.). I say "practical" because even if the underlying implementation can store more elements than max-int, once these methods stop working as documented, the data structure ceases to be reliable (ignoring performance considerations in this context.)

    Note that the API docs do give clues on the maximum practical capacity. See:

    See also the following for further discussions on this topic:

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    160
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Default Re: Maximum size of Collections

    Quote Originally Posted by dicdic View Post
    According to my experiment, those list has a maximum capacity of Integer.MAX_VALUE since the get method of List accept a parameter of int primitive type (index of element), therefore we can conclude that the maximum capacity of List is equal to Integer.MAX_VALUE.
    Using get is not the only way to access members of a collection
    for (String str : list)
    // Iterates through each String element of the list

  5. #5
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Maximum size of Collections

    Using get is not the only way to access members of a collection
    I am not saying that the only way to access the element is the get() method. Of course there is Iterator interface that can iterate the element . What I'm pointing out is that the size might be equal to Integer.MAX_VALUE since get() method accepts only integer type.

    I am still in further study of what cornix and jashburn suggested to read before I post the response of another question.

Similar Threads

  1. [SOLVED] boolean array maximum size
    By dicdic in forum Java Theory & Questions
    Replies: 4
    Last Post: October 31st, 2013, 12:47 PM
  2. Jpanel preffered size exceed the nactual screen size
    By manish.ctae@gmail.com in forum AWT / Java Swing
    Replies: 2
    Last Post: October 31st, 2012, 01:29 AM
  3. Setting JFrame maximum size that pack() can not violate
    By Javabeginner in forum AWT / Java Swing
    Replies: 3
    Last Post: September 3rd, 2010, 05:53 AM
  4. Limit File Size or Request Size
    By tarek.mostafa in forum Java Servlet
    Replies: 3
    Last Post: June 12th, 2010, 04:28 PM
  5. Limit File Size or Request Size
    By tarek.mostafa in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: June 11th, 2010, 07:21 AM