Java Huge data store - java

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.

Related

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(...);

Why does HashMap in java internally use array to store Entry Objects and not an ArrayList?

Why does HashMap in java internally use array to store Entry Objects and not an ArrayList ?
The reason for this is highly likely that HashMap needs to control how its internal table is resized according to the number of entries and the given loadFactor.
Since ArrayList doesn't expose methods to resize its internal array to specific sizes (HashMap uses powers of 2 for its size to optimize rehashing, but ArrayList multiplies capacity by 1.5), it simply wasn't an option to be considered.
Also, even if ArrayList did increase capacity in the same way, relying on this internal detail would tie these two classes together, leaving no room to change the internal implementation of ArrayList at a later date as it could break HashMap or at the very least make it less memory efficient.

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.

ArrayList in Java when storing large objects

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.

Java: Change String[][] Dynamically

I have this code:
newArray = new String[][]{{"Me","123"},{"You","321"},{"He","221"}};
And I want to do this dynamically.
Add more elements, things like it.
How do I do this?
PS: Without using Vector, just using String[][];
You can't change the size of an array. You have to create a new array and copy all content from the old array to the new array.
That's why it's much easier to use the java collection classes like ArrayList, HashSet, ...
You can't change the size of arrays. I think you have some options:
use a List<List<String>> to store a list of lists of strings
use a Map<String,String> if you're storing a key/value pair
Vector tends not to be used these days, btw. A Vector is synchronised on each method call, and thus there's a performance hit (negligible nowadays with modern VMs)
Java does not have the facility to resize arrays like some other languages.
But
You would not see a difference between a String array and a ArrayList<String> (javadoc) unless you are specifically required to do so (like in homework)
There are ways where you can declare a enormous array so that you dont run out of space but I would strongly recommend ArrayList for if you need dynamic changes to the size. And ArrayList provides some possibilities that are not (directly) possible with an array, as a bonus.
You can get away with using arrays if it's possible to calculate the size of arrays before using them. In your example, it seems that we need to know the size of the first array only. So you could impose some limit of how many records could be saved, or you could query user to know how many records it needs to save or something similar.
But again, it's easier to use Collections.

Categories

Resources