This question already has answers here:
How much data can a List can hold at the maximum?
(8 answers)
Closed 9 years ago.
I am building a chat application. Current I have all messages in an ArrayList, which got me thinking - How many elements are the ArrayList design to hold? 100? 1.000? 10.000?
ArrayList can't hold more than Integer.MAX_VALUE elements.
So 2147483647 is the max.
The size of ArrayList is Integer.MAX_VALUE.
/**
* Returns the number of elements in this list. If this list contains
* more than <tt>Integer.MAX_VALUE</tt> elements, returns
* <tt>Integer.MAX_VALUE</tt>.
*
* #return the number of elements in this list
*/
int size();
It is because ArrayList uses array internally and theoretically an array can be of Integer.MAX_VALUE in size at maximum. For further information, you can see this.
ArrayList which is backed by an array, and is limited to the size of the array - i.e. Integer.MAX_VALUE.
A LinkedList isn't limited in the same way, though, and can contain any amount of elements.
see similar question max. length of List in Java
How many data a list can hold at the maximum
to have other aspects on max size of list
ArrayList can hold any number of elements up to Integer.MAX_VALUE - this is due to a design decision to use int datatype for indexes. However what's important is how you are allocating memory for it - the memory allocation is slow - and how you're processing/accessing elements. From the storage aspect alone, though, you're limited by MAX_VALUE. In Java, this is 2^31-1 = 2,147,483,647.
For any normal application this should be enough. Yet, if you need more, you can easily get the source code for it and modify it to use long as the index datatype - and then be limited by Long.MAX_VALUE.
Related
I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?
Yes. Assign null at some indices. Also, you can create the array without any objects stored in it, specifying the length.
Yes. Arrays can hold any number of items up to, but not including the length you have allocated for it. In Java, arrays of primitive types (int, bool, double, char, etc.) will be filled with the default value of that type for any non-initalizer-list array, and null references for any arrays of Object types.
However, questions like yours are more suited for classroom discussion, as you may still be learning computer science material, it seems like.
It depends on what you mean.
All arrays have fixed length, and each always contains a number of elements exactly equal to its length. In that sense, no, arrays cannot have excess capacity; they are always completely filled. You can, however, keep track externally of which elements contain valid data, and ignore the others.
Technically, no array contains objects, but many contain references (of various types; as opposed to primitive values). It is conventional to be a bit sloppy with our language by calling those arrays of objects, and that's how I interpret the question. The distinction becomes important, however, when we recognize that any element of an array of references may contain the value null, which does not refer to an object. Thus, an array of references with some elements null refers to fewer actual objects than its length. You might characterize that as the array containing fewer objects than its length.
Note that null elements are not limited to the end of an array. They may appear at any index, interspersed with non-null elements.
With all that said, however, I suspect you're looking for Lists, and specifically java.util.ArrayList. Lists are more flexible than arrays in many ways, including that they have adjustable size. And ArrayList indeed does have a distinction between its current capacity and its current size, though the capacity is expanded as needed, not fixed like an array's length. The class name reflects that it is implemented with use of arrays, and its performance characteristics reflect that.
Array is a data structure which stores a fixed-size sequential collection of elements of the same type. When you initialize an array, it can have at most the till the size of array, so for your answer it can have empty cells.
I need to create an array that can hold 10 objects. If I make this array will it be able to hold less then the 10 object. An example would be the array has 7 objects and room for 3 more. Is this possible?
In short, yes. It will be able to hold 0 to 10 objects. However it doesn't mean your array can shrink and grow from 0 to 10. It will be a fixed size of 10 (be it you are using it or not).
The long answer is, there will always be 10 elements populated with some values (be it you are actively using all elements or not). Those elements which you think you are not using at the moment will still be populated with default value according to the data type of the array (i.e. null of object, 0 for integer, false for boolean).
For example, creating an int array of size 10:
int array[] = new int[10];
By default, you will already have 10 arrays (all populated with default 0).
Index: 0 1 2 3 4 5 6 7 8 9
Array: [0][0][0][0][0][0][0][0][0][0]
You will always have these 10 elements at your disposal. It is now up to you how you want use these 10 elements.
If you are looking for something that can shrink and grow. You can look into using java.util.ArrayList which can shrink/grow as you remove/add items into the list.
This question already has answers here:
Java dynamic array sizes?
(19 answers)
Closed 8 years ago.
So I am assigned with a project where I have an array and as the user puts elements into this array it will have to double in length once it gets full. We are not permitted to use array lists or anything in the collections interface. What I was trying to do was to make a new array once the old one was full, and then I would copy the values over to the new array. The problem is I don't know how many times I will have to make a new array, so I was wondering how to go about solving this.
Arrays are fixed length. If you want a data structure that has variable length use an ArrayList
Since the poster explicitly stated the requirements of the homework exclude the use of Collections an alternative approach would be to use System.arraycopy(). In such an approach you only maintain a single array and as you add items to it you use System.arraycopy() to copy the old array into a new larger array. System.arraycopy() is relatively fast and is actually how ArrayList expands its size.
If you are concerned about the cost of using System.arraycopy() you could use it only when your array is full and create a new array with more space. E.g.
Create and array of size 20. When it gets full copy it into and array of size 40. When that gets full copy it into an array of size 60...
Interestingly ArrayList increases its size by
int newCapacity = (oldCapacity * 3)/2 + 1;
when the old array is full. Presumably the writers of this put some considerable thought into how much to grow the array by when needed. It might be worth doing the same.
What is the maximum size of HashSet, Vector, LinkedList? I know that ArrayList can store more than 3277000 numbers.
However the size of list depends on the memory (heap) size. If it reaches maximum the JDK throws an OutOfMemoryError.
But I don't know the limit for the number of elements in HashSet, Vector and LinkedList.
There is no specified maximum size of these structures.
The actual practical size limit is probably somewhere in the region of Integer.MAX_VALUE (i.e. 2147483647, roughly 2 billion elements), as that's the maximum size of an array in Java.
A HashSet uses a HashMap internally, so it has the same maximum size as that
A HashMap uses an array which always has a size that is a power of two, so it can be at most 230 = 1073741824 elements big (since the next power of two is bigger than Integer.MAX_VALUE).
Normally the number of elements is at most the number of buckets multiplied by the load factor (0.75 by default). However, when the HashMap stops resizing, then it will still allow you to add elements, exploiting the fact that each bucket is managed via a linked list. Therefore the only limit for elements in a HashMap/HashSet is memory.
A Vector uses an array internally which has a maximum size of exactly Integer.MAX_VALUE, so it can't support more than that many elements
A LinkedList doesn't use an array as the underlying storage, so that doesn't limit the size. It uses a classical doubly linked list structure with no inherent limit, so its size is only bounded by the available memory. Note that a LinkedList will report the size wrongly if it is bigger than Integer.MAX_VALUE, because it uses a int field to store the size and the return type of size() is int as well.
Note that while the Collection API does define how a Collection with more than Integer.MAX_VALUE elements should behave. Most importantly it states this the size() documentation:
If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
Note that while HashMap, HashSet and LinkedList seem to support more than Integer.MAX_VALUE elements, none of those implement the size() method in this way (i.e. they simply let the internal size field overflow).
This leads me to believe that other operations also aren't well-defined in this condition.
So I'd say it's safe to use those general-purpose collections with up to Integer.MAX_VLAUE elements. If you know that you'll need to store more than that, then you should switch to dedicated collection implementations that actually support this.
In all cases, you're likely to be limited by the JVM heap size rather than anything else. Eventually you'll always get down to arrays so I very much doubt that any of them will manage more than 231 - 1 elements, but you're very, very likely to run out of heap before then anyway.
It very much depends on the implementation details.
A HashSet uses an array as an underlying store which by default it attempt to grow when the collection is 75% full. This means it will fail if you try to add more than about 750,000,000 entries. (It cannot grow the array from 2^30 to 2^31 entries)
Increasing the load factor increases the maximum size of the collection. e.g. a load factor of 10 allows 10 billion elements. (It is worth noting that HashSet is relatively inefficient past 100 million elements as the distribution of the 32-bit hashcode starts to look less random, and the number of collisions increases)
A Vector doubles its capacity and starts at 10. This means it will fail to grow above approx 1.34 billion. Changing the initial size to 2^n-1 gives you slightly more head room.
BTW: Use ArrayList rather than Vector if you can.
A LinkedList has no inherent limit and can grow beyond 2.1 billion. At this point size() could return Integer.MAX_VALUE, however some functions such as toArray will fail as it cannot put all objects into an array, in will instead give you the first Integer.MAX_VALUE rather than throw an exception.
As #Joachim Sauer notes, the current OpenJDK could return an incorrect result for sizes above Integer.MAX_VALUE. e.g. it could be a negative number.
The maximum size depends on the memory settings of the JVM and of course the available system memory. Specific size of memory consumption per list entry also differs between platforms, so the easiest way might be to run simple tests.
As stated in other answers, an array cannot reach 2^31 entries. Other data types are limited either by this or they will likely misreport their size() eventually. However, these theoretical limits cannot be reached on some systems:
On a 32 bit system, the number of bytes available never exceeds 2^32 exactly. And that is assuming that you have no operating system taking up memory. A 32 bit pointer is 4 bytes. Anything which does not rely on arrays must include at least one pointer per entry: this means that the maximum number of entries is 2^32/4 or 2^30 for things that do not utilize arrays.
A plain array can achieve it's theoretical limit, but only a byte array, a short array of length 2^31-1 would use up about 2^32+38 bytes.
Some java VMs have introduced a new memory model that uses compressed pointers. By adjusting pointer alignment, slightly more than 2^32 bytes may be referenced with 32 byte pointers. Around four times more. This is enough to cause a LinkedList size() to become negative, but not enough to allow it to wrap around to zero.
A sixty four bit system has sixty four bit pointers, making all pointers twice as big, making non array lists a bunch fatter. This also means that the maximum capacity supported jumps to 2^64 bytes exactly. This is enough for a 2D array to reach its theoretical maximum. byte[0x7fffffff][0x7fffffff] uses memory apporximately equal to 40+40*(2^31-1)+(2^31-1)(2^31-1)=40+40(2^31-1)+(2^62-2^32+1)
Java hashcode is an integer (size 2 pow 32)
When we create a hashtable/hashmap it creates buckets with size equals Initial Capacity of the Map. In other words, it creates an array of size "Initial Capacity"
Question
1. How does it map the hashcode of a key (java object) to the bucket index?
2. Since size of hashmap can grow, can the size of hashmap be equal to 2 pow 32? If answer is yes, it is wise to have an array of size 2 pow 32?
Here is a link to the current source code: http://www.docjar.com/html/api/java/util/HashMap.java.html
The answers to your questions are (in part) implementation specific.
1) See the code. Note that your assumption about how initialCapacity is implemented is incorrect ... for Oracle Java 6 and 7 at least. Specifically, initialCapacity is not necessarily the hashmap's array size.
2) The size of a HashMap is the number of entries, and that can exceed 2^32! I assume that your are actually talking about the capacity. The size of the HashMap's array is theoretically limited to 2^31 - 1 (the largest size for a Java array). For the current implementations, MAX_CAPACITY is actually 2^30; see the code.
3) "... it is wise to have an array of size 2^32?" It is not possible with Java as currently defined, and it is unwise to try to do something that is impossible.
If you are really asking about the design of hash table data structures in Java, then there is a trade-off between efficiency for normal sized hash tables, and ones that are HUGE; i.e. maps with significantly more than 2^30 elements. The HashMap implementations are tuned to work best for normal sized maps. If you routinely had to deal with HUGE maps, and performance was critical, then you should be looking to implement a custom map class that is tuned to your specific requirements.
The size of a Java array is actually limited to Integer.MAX_VALUE elements, 2^31-1.
HashMap uses power of two array sizes, so presumably the largest it could use is 2^31. You would need a large physical memory to make that reasonable.
HashMap does a series of shift and xor operations to reduce some sources of collisions before doing a simple bitwise AND to get the bucket index.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How many data a list can hold at the maximum
What is the maximum number of elements a list can hold?
Assuming you mean implementations of the java.util.List interface, methods like get() and size() use int, so the upper theoretical boundary would be Integer.MAX_VALUE entries. You might run out of memory before you reach this limit though!
The index type in Java arrays is int too, so you're definitely limited to Integer.MAX_VALUE entries for regular arrays.
If you're talking about an ArrayList, they're indexed using integers (which are always signed in Java), so they can theoretically hold 2^31 elements (not 2^32). At that point you're probably going to have memory issues anyway.
LinkedList is also an implementation of List which stores the elements as a Linked List. So theoretically its size is equivalent to the amount of memory you can allocate.