initialCapacity in ArrayList - java

I need to create an ArrayList and I know upfront that the max size for this array won't exceed more than 5 elements.
I wanted to know from the community whether initailCapacity specifying size as 5 will have any performance benefits.
List<String> list = new ArrayList<>(5);
or
List<String> list = new ArrayList<>();
Which one is better?

If you do expect that the list will usually be populated with at least some elements (but no more than 5), then yes, setting an initial capacity of 5 will likely be the most efficient way to go.
Lazy instantiation of backing array
However, if the most common / expected case is that not even a single item gets added to the list, then the constructor without any specified initial capacity may be more efficient. This is because (assuming a somewhat recent JDK version is used) the ArrayList will not yet allocate any backing array at all, but just use a reference shared among all empty, default-constructed ArrayLists. It will only actually allocate memory once you start adding items to it.
When you add your first element to the ArrayList, a new backing array is created. That new array defaults to a size of ten.
To see that a shared reference to a backing array is initially used, you can look at OpenJDK's github for example, and you can find the implementation of the no-args constructor here. It looks as follows:
public ArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
That reference that the backing array (elementData) is assigned to is declared higher up in the file as follows:
/**
* Shared empty array instance used for default sized empty instances. We
* distinguish this from EMPTY_ELEMENTDATA to know how much to inflate when
* first element is added.
*/
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
This does not only happen in OpenJDK, but also in Oracle's JDK. At least, it does in the version I have on my computer right here. I don't know if there are any easy-to-find links to Oracle's source code though, OpenJDK source code is easier to find online.

From ArrayList.java
/**
* Default initial capacity.
*/
private static final int DEFAULT_CAPACITY = 10;
The default capacity (used when you don't provide one) is ten. So specifying the size of five will mean less memory gets used.
If you know that you'll only ever have a certain number, perhaps an Array will be a more lightweight choice.

It depends on the use case. Suppose if you already know that a specific number of objects(in this case string objects) are going to be stored for sure, then if you would specify the size of the ArrayList while creating it would allocate the memory at that step.
List<String> list = new ArrayList<>(5);
Then once the elements are filled the size of the ArrayList will expand dynamically as you add more objects. Suppose if you are going to add 50 elements to the ArrayList for sure, then specify the initial capacity as 50 which would result in allocating that much space. This would be a more efficient approach as you wont be dynamically allocating the memory up to 50 elements after initialization. Allocating memory as the ArrayList expands will affect the performance. At the same time if you specify a large initial size then you are wasting valuable memory space.

yeah, it will improvethe performance, as not much memory default of 10 will be occupied

Yes it will improve performance but only by a tiny amount.
Good practice would be to set the list to a specific length if you know it will always be that long, but such uses tend to be rare so shouldn't be a common issue.
Furthermore, if it is a 5 member list then you will probably be better off just making a new object with 5 fields. This will make handling this data easier and faster as won't need to search the list.

Related

Advantages of creating an ArrayList with initial capacity of 0?

I am a somewhat experienced Java developer and I keep seeing things like this
List<Integer> l = new ArrayList<Integer>(0);
which I really can't understand. What's the point of creating an ArrayList with an initial capacity of 0, when you know it's going to grow beyond the capacity?
Are there any known benefits of doing this?
It keeps the size (in memory) of the ArrayList very small, and is a tactic for when you want the variable to be non-null and ready to use, but don't expect for the List to be populated immediately. If you expect it to be populated immediately, it's best to give it a larger initial value - any "growing" of the ArrayList is internally creating a new primitive array, and copying items over. Growth of an ArrayList is expensive, and should be minimized.
Or, if you're creating lots of instances of a class that each contain one of these List properties. If you don't immediately plan on filling them, you can save a bit of memory by not allocating the room just yet.
However: There is a better way: Collections.emptyList(). Normally you'll want to protect access to that list directly, and (as an example) in your class provide domain-specific method calls that operate on the internal List. For example, let's say you have a School class that contains a List of student names. (Keeping it simple, note this class is not thread safe.)
public class School {
private List<String> studentNames = Collections.emptyList();
public void addStudentName(String name) {
if (studentNames.isEmpty()) {
studentNames = new ArrayList<String>();
}
studentNames.add(name);
}
public void removeStudentName(String name) {
studentNames.remove(name);
if (studentNames.isEmpty()) {
studentNames = Collections.emptyList(); // GC will deallocate the old List
}
}
}
If you're willing to make the isEmpty() checks and perform the initialization/assignment, this is a better alternative to creating lots of empty ArrayList instances, as Collections.emptyList() is a static instance (only one exists) and is not modifiable.
For java 6 (or openjdk 7), not specifying an initial size gives you a list within initial size set to 10. So depending on many factors of your usage of the list, it could be very slightly more memory and/or performance efficient to initialize the list with size 0.
For java 7, specifying an initial size 0 is functionally equivalent to not specifying an initial size.
However it is actually less efficient, since the call to the constructor with argument 0 incurs a call to new Object[0], whereas if you specify the no-args constructor, the initial elementData for your list is set to a statically defined constant named EMPTY_ELEMENTDATA.
Relevant code from ArrayList source:
/**
* Shared empty array instance used for empty instances.
*/
private static final Object[] EMPTY_ELEMENTDATA = {};
In other words the use of new ArrayList<Integer>(0); seems superfluous, there are no benefits to doing so, and I would use new ArrayList<Integer>(); instead.
If additions to that ArrayList are really unlikely and if it's important to keep the size of the ArrayList at a minimum, then I can see that being useful.
Or if the only purpose of that ArrayList is to be a return value from a method, where returning an empty list is a special message to the function caller, like "no results found".
Otherwise, not really.
By default ArrayList has capacity of 10 and it is resized by +50% each time.
By using lower initial capacity you can sometimes(in theory) save memory. On the other hand each resize is time consuming. In most cases it is just a sign of preemptive optimization.
It's always better approach to give a large value(if you how much list will exceed) to array list, because it will reduce resizing of list and hence optimize your execution time.
Initializing array list with value 0 create Empty array list which reducing memory if you know your list will not present more then 10 content's.
Depending on the contract you can avoid NullPointerExceptions by not having nulls. It is good practice in certain situations, see Effective Java by Joshua Bloch Item 43: Return empty arrays or collections, not nulls

which one is better from memory point of view String[] / List<String>

Which one is better from memory point of view String[] / List<String>. I have 5000 or more objects need to store which i am getting as a response from service.
If depends on the List implementation you are asking about. If you are asking about String[] vs. ArrayList, they use the same amount of storage, as the ArrayList is backed by an array (the difference will depend on the length you initialize the array with vs. the initial capacity you start the ArrayList with).
In order to use String[], you must know the max number of Strings you'll need to store, since the array's legnth is fixed. The ArrayList will automatically copy the data to a larger array whenever necessary.
The simpler object is always better in memory. It is highly dependend on what list you are using. LinkedList is way worse in memory than ArrayList. The question you should ask yourself is how you will interact with the set.
If you want to add sth. in the middle you should use a LinkedList.
If just have a fixed set of Strings use the String[].
If you are just adding more and more Strings use the ArrayList.
You shouldn't really worry about memory usage, since there is plenty.
Probably you should go for ArrayList<String>.
The memory consumption depends on the type of List, and frankly, for only 5000 objects that still hardly matters.
ArrayList<String> is only slightly "worse" than String[]. ArrayList<String> simply wraps and manages an Object[], giving overhead per object and per growth threshold. How big that Object[] is doesn't matter. And each entry in an Object[] would usually be 4 bytes in size, no matter whether you use it or not, as Java is by reference. So that would be like 20 kiB. That hardly matters in most of the environments in which Java is used.
If you use LinkedList<String>, the memory consumption will be more, as for every entry, there will be an additional Node object. But still that hardly matters. Let's assume each Node object is 20 Bytes in size (hashCode, class, list, next, previous). Then we're talking of 100 kiB.
In the context of 1 GiB RAM on a phone it already doesn't matter for most applications on a phone. On a PC with 8 GiB or 16 GiB, you really don't want to mind.
I'd usually go for ArrayList<?>, except if I remove and add the current loop element frequently and do not need random access, then I go for LinkedList<?>, or for CopyOnWriteArrayList<?> if I need a list that I can read and modify from multiple threads at the same time and the list wouldn't grow too big or write is rare.
String[] and List<String> both taking same space in memory to store data.
String[] - you need to have all information how much elements get stored in your memory, and it should be not minimum or not maximum space then required space.
List<String> - It has its own benefits of resizing, specify certain size to your list in case specified size is get small in feature you no need to change the code. and List collection is easier for your further operations.

ArrayList.trimToSize() method

Can I use ArrayList.trimToSize() method in dynamic arrayList ?
If I use it , what will be happened ?
Can I get any benefits on using this method on dynamic ArrayList.
In which case, I should use this method.
Thanks in advance.
From the docs you link yourself:
Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.
Internally an ArrayList stores an array that holds all the items. At certain moments the ArrayList will "expand" this array by copying all values into a larger array. This happens whenever an item is being added and the required capacity is bigger than the current one. What happens at this point is the following line of code:
int newCapacity = oldCapacity + (oldCapacity >> 1);
elementData = Arrays.copyOf(elementData, newCapacity);
In essence this will create a new array that is 1.5 times the size of the current one. What this method does is resize the internal array so that it has no empty space left.
Nothing will happen that's visible to you. You won't lose any data, it's just smaller backing array. Adding data to the arraylist will again expand the array normally.
I assume you mean a "normal" ArrayList. If you use this, you will reduce the memory used but it will also be futile if you will still add data after that AND it is pretty useless for small lists. If you have an ArrayList of many, many items and you're sure you don't want to add anymore then you can call this method to reduce some memory footprint.
See above. I don't think it's very likely you'll ever use this.
trimToSize() from the source:
public void trimToSize() {
modCount++;
int oldCapacity = elementData.length;
if (size < oldCapacity) {
elementData = Arrays.copyOf(elementData, size);
}
}
Well, that's simple:
The ArrayList will be trimmed to its current size
Yes, you can minimize the storage of an ArrayList instance
When you want to trim the ArrayList and minimize its storage
But seriously: There are few occasions where this method should be called (I personally have never used it). It's related to how an ArrayList is implemented: As the name suggests, the ArrayList internally uses an array to store the data. When you add new elements to the ArrayList, the size of the array is increased as needed. When you add 1000000 elements to the ArrayList, then the internal Array will have a .length of at least (!) 1000000. When you afterwards remove 999999 elements from the ArrayList, then the internal array will still have a .length of at least 1000000. The call to trimToSize will then make sure that the internal array only has the required size (1, in this case). But again: This is hardly ever necessary or beneficial. You should usually not work on ArrayList instances anyhow, but on the (more general) List interface.

How to resize ArrayList

I come from a C++ background and I want to have a matrix of
ArrayList<arrayList<E>> javamatrix
In C++ I would just do
std::vector<std::vector<T> > cppmatrix;
std::vector<T>vcol(cols);
cppmatrix.resize(rows,vcol);
I can't seem to find a built-in resize() function for ArrayLists for this task, so should I use another collection? Is no way to do this except using for loops with javamatrix.add()?
P.S I want it to be initialized in the constructor with its size as that size might be queried before I edit elements or add or remove.
There is no resize equivalent that automatically constructs and adds elements. You must do this yourself. However, ensureCapacity is equivalent to vector's reserve. It will ensure you have room, but not change the actual size.
You shouldn't need to resize arraylists. The size you initially pass in is just its starting size. If you attempt to add items beyond its current size, it will automatically resize.
From the documentation:
Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not specified beyond the fact that adding an element has constant amortized time cost.
Mostly, a 'resize()' operation is not needed because (a) ArrayList's auto-resize as you add elements, and (b) it's unclear what values you would store in the ArrayList<>, e.g. 'null' is not very useful. E.g. in your case you'd probably need a loop anyway to create MatrixCell objects.
For those readers who want to know how to resize an ArrayList to make it smaller, it mystifies me why ArrayList was designed without a 'resize()' method. Perhaps it's because novice programmers are likely to see that method and then not realise that ArrayList<> auto-resizes.
In Java this idiom works to reduce the size of an ArrayList<>:
list.subList(n,list.size()).clear();
It works because the 'subList' returns a List backed by the original ArrayList<>, so therefore the 'clear()' operates on the original 'ArrayList<>'.
I know this question is very old already but this link may help java arraylist ensureCapacity not working , The code adds "Null" value in order to adjust the current size.
Instead of using purely ensureCapacity you can have ensureSize
public static void ensureSize(ArrayList<?> list, int size) {
list.ensureCapacity(size);
while (list.size() < size) {
list.add(null);
}
}

Java ArrayList<Double> IndexOutOfBoundsException Problem

I've got a Problem with ArrayList. I need it to store a result. Because I want to start with element n I tried to give the ArrayList a capacity with ensureCapacity(n+1) to use set(n,x) but I get an IndexOutOfBoundsException.
I tried to store n add(x) before the use of set and this works.
So I'd like to know why it doesn't work on my way and how to solve this because put n times a add(x) isn't a good style ;-)
When you change the capacity of an ArrayList it doesn't create any elements, it just reserves memory where there could be elements. You can check the size before and after adjusting the capacity and you will see that it does not change.
The purpose of changing the capacity is if you know in advance how many elements you will have, then you can avoid unnecessary repeated resizing as you add new elements, and you can avoid memory wastage from excess unused capacity.
If you don't like using your own loop and the list add method directly then there is another way. Create your ArrayList with the number of elements you want it directly like this:
final int MAX_ELEMENTS = 1000;
List<Integer> myList = new ArrayList<Integer>(
Collections.<Integer>nCopies(MAX_ELEMENTS, null));
Or, if you already have a list that you want to expand the size by n elements:
myList.addAll(Collections.<Integer>nCopies(n, null));
(Note, I assumed here that the list would be holding Integer objects, but you can change this to your custom type. If you are working with raw/pre-Java 5 types then just drop the generic declarations.)
As for your actual question: capacity != contents. An ArrayList internally has both a physical array and a count of what is actually in it. Increasing the capacity, changes the internal array so it can hold that many elements, however, the count does not change. You need to add elements to increase that count.
On the other hand, if you are just trying to set specific elements and know the maximum that you want to use, why not use an array directly? If you then need to pass this array to an API that takes Lists, then use Arrays.asList. The other classes could still change contents of your backing array but it would not be able to increase the size or capacity of it.
As others have answered, ensureCapacity() is just related to performance, is not frequently used by the common user.
From Bruce Eckel's Thinking in Java book:
In a private message, Joshua Bloch
wrote: "... I believe that we erred by
allowing implementation details (such
as hash table size and load factor)
into our APIs. The client should
perhaps tell us the maximum expected
size of a collection, and we should
take it from there. Clients can easily
do more harm than good by choosing
values for these parameters. As an
extreme example, consider Vector's
capacityIncrement. No one should ever
set this, and we shouldn't have
provided it. If you set it to any
non-zero value, the asymptotic cost of
a sequence of appends goes from linear
to quadratic. In other words, it
destroys your performance. Over time,
we're beginning to wise up about this
sort of thing. If you look at
IdentityHashMap, you'll see that it
has no low-level tuning parameters"
You are getting this exception because ensureCapacity() only makes sure that there is enough memory allocated for adding objects to an ArrayList, I believe this is in case you want to add multiple objects at once, without having to relocate memory.
To do what you want you would have to initiate the ArrayList with null elements first...
int n = 10; //capacity required
ArrayList foo = new ArrayList();
for( int i=0; i<=n; i++ ) {
foo.add(null);
}
Then you have objects in the List that you can reference via index and you wont receive the exception.
Perhaps you should rethink the choice of using List<Double>. It might be that a Map<Integer,Double> would be more appropriate if elements are to be added in an odd order.
Whether this is appropriate depends on knowledge about your usage that I don't have at the moment though.
Is the data structure eventually going to be completely filled, or is the data sparse?
what other people said about ensureCapacity() ...
you should write a class like DynamicArrayList extends ArrayList. then just overrride add(n,x) to do with for loop add(null) logic specified about.
ensureCapacity() has another purpose. It should be used in cases when you get to know the required size of the List after it has been constructed. If you know the size before it is constructor, just pass it as a an argument to the constructor.
In the former case use ensureCapacity() to save multiple copying of the backing array on each addition. However, using that method leaves the structure in a seemingly inconsistent state
the size of the backing array is increased
the size field on the ArrayList isn't.
This, however, is normal, since the capacity != size
Use the add(..) method, which is the only one that is increasing the size field:
ArrayList list = new ArrayList();
list.ensureCapacity(5); // this can be done with constructing new ArrayList(5)
for (int i = 0; i < list.size - 1; i ++) {
list.add(null);
}
list.add(yourObject);

Categories