ArrayList in Java when storing large objects - java

I came across this interview question about arraylist in Java, and I feel it is quite interesting but no clue how to answer it:
What attention should be paid when using arrayList to store large object?
I wonder if we should answer this question in the regard of time/space coplexity?
Thanks

All objects in Java are stored as references, in containers and variables, etc, so in C++ terms, all containers only store pointers to the objects. In such a situation, the size of the object should be irrelevant for most if not all use cases.

Internally ArrayList uses Object[]. Once it reaches max capacity, it creates a new array of size 1.5 times the original and will copy from old array to new array. May be interviewer wanted to check about the cost of this copy with large objects
ArrayList: how does the size increase?
check ensureCapacity() - http://www.docjar.com/html/api/java/util/ArrayList.java.html

ArrayList supports dynamic arrays that can grow as needed.
In Java, arrays has a fixed length this means after the arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. But, sometimes, you may dont know the size until the runtimeso that in this situation we used ArrayList.
ArrayList can dynamically increase or decrease in size. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.
also, be aware that Arraylist store only objects.

Related

How Array/ArrayList goes direct to the index of object? Or How ArrayList get to know that the object is stored at this place?

in the interview room,the interviewer asked me a question that how arraylist is so fast,i said that it implements RandomAccess, but he asked how random access beneficial for searching the object in memory area?
Do you want to say that objects are stored in line in the memory and it goes to the 10th index for example
An array is just the starting point of a chunk of memory along with a data type (int, boolean, String, etc.). The data type is used to determine how far apart the elements are spaced.
A Java ArrayList is similar to an array, but with additional features.
When using an array (or any array-related data structure), individual read/write operations are fast and completely unrelated to the total size of the array. If you want the one millionth array element, it's a single calculation to determine where that element is (one million * <size of each element>) – no scanning or searching involved.
There are lots of great resources online where you can read more about arrays. It's worth building a solid understanding of arrays, not just for job interview purposes but also for general understanding of how computers work.
Because ArrayList is a resizable array implementation of the List interface.
Please refer below link for understanding it well:
ArrayList Internal Working

Random Access in Array

I was reading this, advantages of java, where it states that random access is an advantage of arrays in java. I do not understand how can accessing a random element of an array is an advantage. Shouldn't it be a disadvantage?
Why is java allowing to access elements of an array randomly, if the data is stored continuously, shouldn't the data be accessed in an orderly manner?
Random(direct) access implies the ability to access any entry in a array in constant time (independent of its position in the array and of array's size). And that is big advantage.
It is typically contrasted to sequential access. Datastructure has sequential access if we can only visit the values it contains in one particular order.
Java array is an object the contains elements of similar data type. It is a data structure where we store similar elements. We can store only fixed set of elements in a java array.
Advantage of Java Array
Code Optimization: It makes the code optimized, we can retrieve or sort the data easily.
Random access: We can get any data located at any index position.
Disadvantage of Java Array
Size Limit: We can store only fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in java.
It means any element in an array has constant access time O(1). Arrays store it's elements in contiguous memory locations. Arrays store objects with fixed size and any object can be accessed by calculating the offset which is (size*index) instead of traversing the entire array sequentially.
Depends on your use, if you want to access data again, i recommend to see Maps ou HashMaps using , it's the most simple way to work.
If you want to sort an array you can use Arrays.sort(...);

Does appending/removing entries to a Java list reallocate memory?

This is low-level memory question about how Java performs .add and .remove on an ArrayList or other types of lists. I would think that Java would have to do a reallocation of memory to append/remove items to a list, but it could be doing something I'm not thinking of to avoid this. Does anyone know?
If by "regular list" you mean java.util.List, that is an interface. It does not specify anything about whether or when any memory is allocated in association with adding or removing elements -- those are details of specific implementations.
As for java.util.ArrayList in particular, its docs say:
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.
In other words, Java does not specify the answer to your question.
If I were to speculate based on the available documentation, I would guess that java.util.ArrayList.remove() never performs any memory allocation or reallocation. It seems to follow from the docs overall that java.util.ArrayList.add() allocates additional space at least sometimes (in the form of a new, longer internal array). In order to achieve constant amortized cost for element additions, however, I don't see how it could reallocate on every element addition. Almost certainly, it reallocates only when its capacity is insufficient, and then it scales the capacity by a constant factor -- e.g. doubles it.
All list implementations require storage of some information about the objects in the list and the order of those objects. Larger lists require more such information because there is some information for each object in the list. Thus adding to a list must, on average, result in allocation of more storage for this information.
Adding an element to a list does not copy the object that was added to the list. Indeed, no Java statements cause an additional copy of an object to be visible to your program (you have to explicitly use a copy constructor or a clone method to do that). This is because Java objects are never accessed directly, but are always accessed through a reference. Adding an object to a collection really means adding a new reference to the object to the collection.

how does an ArrayList compare to a dynamic array

Is an ArrayList is just the interface for a dynamic array? Or are they the same thing?
like: ArrayList corresponds to dynamic array, HashMap corresponds to Map ?
except I don't see any Java API for something like a dynamic array, unless it is ArrayList?
Yes. In short. A longer explanation is that an ArrayList is a collection that uses arrays for storage, rather than a linked list, doubly linked list or similar. This means that it gives all the benefits of using an Array, whilst Java looks after the mechanics of sizing the Array for you (dynamically).
I seem to remember that the initial array is created with a default maximum size (which can be specified by the user). Should the collection run out of space, then a larger array is created and the contents of the original array copied into the new one. The increment in size is set to prevent this happening too often, as the operation is fairly costly.
Java also offers the Vector collection which is similar, but is also thread safe, see: What are the differences between ArrayList and Vector?.
ArrayList is the resizable-array implementation of the List interface.
So that's probably what you are looking for if you need a dynamic array.
ArrayList is not a dynamic array, it's not an array type dynamic or not, it's just one of the implementations of the List interface. Understand the difference between classes and interfaces. On the other hand arrays are container objects with the fixed size.
If in the dynamic sense you mean an array which can change in size then a List is an interface for a dynamic array. It is named ArrayList because it uses an array internally that's all.
Your analogy does not fit in the java collections framework since you can say that an ArrayList is a dynamic array but Map (or HashMap for that matter) does not have a "primitive" counterpart.
If by 'dynamic array' you mean an array in C++, then all arrays in Java are dynamic and stored on heap. ArrayList is a resizable wrapper for it. It also provides simple consistency checks - i.e. that you don't modify your array from outside during iteration.

Java Huge data store

I have a requirement to store huge amount of data in Java/collection API. Which would be suitable for that Array or Arraylist. And why
Arrays are typed safe collection and once you created it can not grow or shrink so better to use ArrayList but again if you know the collection is fixed length and type of objects are same then you can prefer Array.
You can use LinkedList also if you have modify you collection frequently.
It totally depends on your requirement and type of data to be stored.

Categories

Resources