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.
Related
This question already has answers here:
Do Java arrays have a maximum size?
(10 answers)
Closed 6 years ago.
How many integers can an array in java and python have? And if I want to enter more integers to an array and I reached that limit how to handle this?
Java arrays can have 2^31-1 element in theory, although some JVM cannot allocate an object larger than 2^31-8 elements due to OS limitations. (See the ArrayList code for an example of this limit in practice)
If you have more elements, you can either
have an array of arrays.
use a memory mapped file to store the data. This has the advantage of potentially greater than main memory and is persisted.
This question already has answers here:
Finding out the minimum difference between elements in an array
(9 answers)
Closed 7 years ago.
Well I have to find the smallest difference between any two elements of a large array. By large i mean really large like 10 million or more elements array. Although this is not a practical scenario i like to know the fastest possible ones. Getting an element, comparing with the rest of the elements , sorting the difference and getting the least difference is really unimaginable. Is there some efficient way to do this task? I would be happy.
How fast this can be done depends entirely on your notion of 'difference' - for example, if you just have an array of numbers and want the smallest difference between two numbers (that is, you have a total ordering available for your elements) then you can just sort and get the smallest difference between neighbors. If there is no total ordering, you'll have to minimize your checks by clustering your elements somehow (e.g. creating a spatial tree for an array of 3D points).
This question already has answers here:
Why start an ArrayList with an initial capacity?
(11 answers)
Closed 8 years ago.
Is it considered a best pratice to initialize Collections ( eg: Arraylist ) with initial capacity ?
From my observation through docjar, it looked like initialization reduced need for 'expansion' of initial array and also reduce the size, of internal datastructure due to resizing.
As the comment mentions, if you know the number of elements that will be eventually needed, then it will gain you some performance since you do not have to re-allocate the backing array when the number of elements increases to exceed the current capacity.
However, there is a tradeoff because if you can't estimate it well (within an order of magnitude), then you may waste memory by allocating too much at the beginning.
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.
Okay, so I'm making a dynamic 2D array in Java that implements the java.util.Collection interface. I made my array implement it because I wanted it to have the same functionality as a normal Collection. However, I cannot implement the size() method because in the interface it returns an integer and a 2D matrix could potentially overflow an integer type.
Here's a snippet of my class I'm trying to make:
public abstract class AbstractMatrix<E> implements Collection<E>{
#Override
public long size() {
return columns * rows;
}
}
Now, this won't work because "The return type is incompatible with Collection<E>.size()", and if I change the type to int, columns * rows could overflow.
I know I can't override the size method like this, but is there any way I can make sure the method returns the correct size while still implementing the Collection interface?
Yes, I know this is impractical and will likely never be an issue, but I was interested to know if there was a good solution for it.
Although your implementation of size is questionable, the contract for Collection#size is defined in the javadoc:
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
So you could calculate the size as a long, and return Integer.MAX_VALUE if it is larger than Integer.MAX_VALUE.
Alternatively, you could mimic the way it is implemented in LinkedList#add for example, where size is simply incremented and is allowed to overflow.
Assuming you were willing to have an 8GB array -- the minimum size of a two-dimensional array that would overflow an int with its total size -- and assuming you were willing to do anything interesting with that collection, like iterate over it (costing several minutes at least just for the iteration)...
I believe the typical approach is either to back down to implementing Iterable and not Collection, or to just return Integer.MAX_VALUE, as specified by the Javadoc:
Returns the number of elements in this collection. If this collection contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
If you are really concerned with large enough matrices that could overflow, you could ensure that does not happen by checking whether the resulting size (initialization or resize) would still be within the range of integer, and throw a (runtime) exception if that would be the case
I don't think so, you will need to use a workaround of some sort. You could maybe extend your size to return negative numbers and interpret them as unsigned 32 bit integers, which would give you a maximum of 4 billion and change.
Ask yourself though, do you really need to be able to support that many objects? Keep in mind that 4 billion and change 32 bit integers will take up 16GB of RAM. Using 64 bit java, a 4 billion long array of Object set to all null will take up 32GB because references on 64 bit java are 64 bits. That doesn't even take into account the memory used to actually instantiate that many classes, which will in all likelihood be much higher.